-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4652416
commit bf6a592
Showing
3 changed files
with
72 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Branch | ||
|
||
*Branch* is a zero-dependency framework for Scala 3 on Java 21+. | ||
|
||
Why zero-dependency? *For fun!* Fun, **and** to illustrate how much you can get done with Scala without relying on | ||
bigger Scala frameworks. *Branch* will not be the fastest, most performant solution, **but** it will (hopefully) let you | ||
get things done quickly! Think of it as the framework for your side-project, not your job. | ||
|
||
It's only ~1 month old, and very much an evolving work-in-progress at the moment. | ||
|
||
The docs (so far ™️) are on : https://github.com/wishingtreedev/branch |
51 changes: 51 additions & 0 deletions
51
example/src/main/scala/app/wishingtree/LazyAppExample2.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package app.wishingtree | ||
|
||
import dev.wishingtree.branch.lzy.{Lazy, LazyApp, LazyRuntime} | ||
|
||
import scala.concurrent.duration.Duration | ||
import scala.concurrent.{Await, ExecutionContext, Future} | ||
import scala.util.Random | ||
|
||
object LazyAppExample2 { | ||
|
||
def main(args: Array[String]): Unit = { | ||
given ExecutionContext = LazyRuntime.executionContext | ||
|
||
val f1: Future[Int] = Future(Random.nextInt(10)) | ||
// f1 is already running, kicked off by an implicit ExecutionContext | ||
val f2: Future[Int] = Future(Random.nextInt(10)) | ||
|
||
// f2 is now running... | ||
def fRandomSum: Future[Int] = for { | ||
a <- f1 | ||
b <- f2 | ||
} yield (a + b) | ||
|
||
// fRandomSum will be the same every time it's called | ||
println(Await.result(fRandomSum, Duration.Inf)) | ||
println(Await.result(fRandomSum, Duration.Inf)) | ||
println(Await.result(fRandomSum, Duration.Inf)) | ||
|
||
val l1: Lazy[Int] = Lazy.fn(Random.nextInt(10)) | ||
// l1 is a description of what you want to do, nothing is running yet | ||
val l2: Lazy[Int] = Lazy.fn(Random.nextInt(10)) | ||
|
||
def lzyRandomSum: Lazy[Int] = for { | ||
a <- l1 | ||
b <- l2 | ||
} yield (a + b) | ||
// lzyRandomSum will be different each time, because the whole blueprint is evaluated on each call | ||
println(lzyRandomSum.runSync()) | ||
println(lzyRandomSum.runSync()) | ||
println(lzyRandomSum.runSync()) | ||
|
||
def myLazyOp(arg: Int): Lazy[Int] = | ||
Lazy.fn(42 / arg) | ||
|
||
println(myLazyOp(0).runSync()) | ||
// -> Failure(Arithmetic Exception) | ||
|
||
println(myLazyOp(0).recover(_ => Lazy.fn(0)).runSync()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters