Skip to content

Commit

Permalink
docs: upload presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tassiluca committed Mar 20, 2024
1 parent 7ab8248 commit a900a61
Show file tree
Hide file tree
Showing 33 changed files with 372 additions and 299 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.tassiLuca.analyzer.lib

import io.github.tassiLuca.dse.boundaries.either
import io.github.tassiLuca.dse.boundaries.either.?
import sttp.client3.{HttpClientSyncBackend, Response, basicRequest}
import sttp.model.Uri

def aggregate(xs: List[Uri]): Either[String, List[String]] =
either: // boundary
xs.map(doRequest(_).?) // `?` break if doRequest returns a Left

def doRequest(endpoint: Uri): Either[String, String] =
HttpClientSyncBackend().send(basicRequest.get(endpoint)).body
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.tassiLuca.dse.blog

import gears.async.Async
import io.github.tassiLuca.dse.blog.core.{PostsModel, simulates}
import io.github.tassiLuca.dse.boundaries.either.leave
import io.github.tassiLuca.dse.boundaries.either.fail
import io.github.tassiLuca.dse.boundaries.{CanFail, either}

/** The component exposing blog posts repositories. */
Expand Down Expand Up @@ -34,7 +34,7 @@ trait PostsRepositoryComponent:
private var posts: Set[Post] = Set()

override def save(post: Post)(using Async, CanFail): Post =
if exists(post.title) then leave("A post with same title has already been saved")
if exists(post.title) then fail("A post with same title has already been saved")
"PostsRepository".simulates(s"saving post '${post.title}'")
synchronized { posts = posts + post }
post
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.github.tassiLuca.dse.blog
import gears.async.{Async, Future}
import io.github.tassiLuca.dse.blog.core.{PostsModel, simulates}
import io.github.tassiLuca.dse.boundaries.EitherConversions.given
import io.github.tassiLuca.dse.boundaries.either.{?, leave}
import io.github.tassiLuca.dse.boundaries.either.{?, fail}
import io.github.tassiLuca.dse.boundaries.CanFail

import java.util.Date
Expand Down Expand Up @@ -39,7 +39,7 @@ trait PostsServiceComponent:
) extends PostsService:

override def create(authorId: AuthorId, title: Title, body: Body)(using Async, CanFail): Post =
if context.repository.exists(title) then leave(s"A post entitled $title already exists")
if context.repository.exists(title) then fail(s"A post entitled $title already exists")
val (post, author) = Async.group:
val content = Future(verifyContent(title, body))
val author = Future(authorBy(authorId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object either:
boundary(Right(body))

/** Quickly break to the enclosing boundary with a [[Left]] filled with [[l]]. */
inline def leave[L, R](l: L)(using Label[Left[L, R]]): R = break(Left(l))
inline def fail[L, R](l: L)(using Label[Left[L, R]]): R = break(Left(l))

extension [L, R](e: Either[L, R])
/** @return this [[Right]] value or break to the enclosing boundary with the [[Left]] value. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import gears.async.AsyncOperations.sleep
import gears.async.default.given
import gears.async.{Async, AsyncOperations, Future}
import io.github.tassiLuca.dse.boundaries.EitherConversions.given
import io.github.tassiLuca.dse.boundaries.either.?
import io.github.tassiLuca.dse.boundaries.either.{?, fail}
import io.github.tassiLuca.dse.boundaries.{CanFail, either}

import scala.util.boundary.Label
Expand All @@ -25,12 +25,24 @@ object EffectsShowcase extends App:

@main def useEffectfulComputation(): Unit =
Async.blocking:
print:
println:
either:
f

@main def useG(): Unit =
Async.blocking:
print:
println:
either:
g(f)

def failing(using CanFail): Unit =
fail("Error!")

@main def useFailing(): Unit =
Async.blocking:
println:
either:
Future(failing).awaitResult
println:
either:
Future(failing).await

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.tassiLuca.dse.examples.boundaries

import io.github.tassiLuca.dse.boundaries.{CanFail, either}
import io.github.tassiLuca.dse.boundaries.either.fail

import scala.util.boundary
import scala.util.boundary.{Label, break}

trait ShowcasingCanFail:

type User
type UserId
type PaymentMethod

def userBy(id: UserId): User
def verifyUser(id: User): Boolean
def paymentMethodOf(user: User): Option[PaymentMethod]
def verifyMethod(address: PaymentMethod): Boolean

def getUser(id: UserId)(using CanFail): User =
val user = userBy(id)
if verifyUser(user) then user else fail("Incorrect user")
// fail is a shorthand for `break(Left("Incorrect user"))`

def getPayment(user: User)(using CanFail): PaymentMethod =
paymentMethodOf(user) match
case Some(a) if verifyMethod(a) => a
case Some(_) => fail("The payment method is not valid")
case _ => fail("Missing payment method")

def paymentData(id: UserId) = either:
val user = getUser(id)
val address = getPayment(user)
(user, address)
Loading

0 comments on commit a900a61

Please sign in to comment.