Skip to content

Commit

Permalink
update some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
alterationx10 committed Nov 13, 2024
1 parent 4652416 commit bf6a592
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
11 changes: 11 additions & 0 deletions README.md
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 example/src/main/scala/app/wishingtree/LazyAppExample2.scala
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())
}

}
21 changes: 10 additions & 11 deletions site/src/lzy/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Lzy

*Lzy* is somewhere between a tiny Effect System, and lazy Futures.
*Lzy* is somewhere between lazy Futures, and a tiny Effect System.

## A Prelude

Expand All @@ -12,6 +12,7 @@ value, but then the blueprint can be run again!
Let's compare the following Future code to Lazy code:

```scala
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))
Expand All @@ -21,9 +22,9 @@ def fRandomSum: Future[Int] = for {
b <- f2
} yield (a + b)
// fRandomSum will be the same every time it's called
println(Await(fRandomSum))
println(Await(fRandomSum))
println(Await(fRandomSum))
println(Await.result(fRandomSum, Duration.Inf))
println(Await.result(fRandomSum, Duration.Inf))
println(Await.result(fRandomSum, Duration.Inf))
```

```scala
Expand All @@ -37,6 +38,8 @@ def lzyRandomSum: Lazy[Int] = for {
} 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)
```

This description/lazy evaluation approach lets you structure you're programs in descriptive ways, but also has the
Expand All @@ -47,20 +50,16 @@ to one of out lazy function and add a way to `.recover` a failure.
def myLazyOp(arg: Int): Lazy[Int] =
Lazy.fn(42 / arg)

myLazyOp(0).runSync
// -> Failure(Arithmetic Exception)
myLazyOp(0).runSync()
// -> Failure(java.lang.ArithmeticException: / by zero)

myLazyOp(0).recover(_ => Lazy.value(0)).runSync
myLazyOp(0).recover(_ => Lazy.fn(0)).runSync()
// => Success(0)
```

The recovery doesn't have to be part of our definition of `myLazyOp`, and can be applied where needed, and different
recovery strategies used in various places.

## Building an Effect System

...

## Other Libraries

If you like *Lzy*, you should check out [ZIO](https://zio.dev/)

0 comments on commit bf6a592

Please sign in to comment.