Skip to content

Commit

Permalink
readme: randoms
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil committed Sep 17, 2023
1 parent 3915df0 commit b249e38
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
68 changes: 62 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,23 @@ Kyo's set of pending effects is a contravariant type parameter. This encoding pe

```scala
// An 'Int' with an empty effect set (`Any`)
val a: Int > Any = 1
val a: Int > Any =
1

// Widening the effect set from empty (`Any`)
// to include `Options`
val b: Int > Options = a
val b: Int > Options =
a

// Further widening the effect set to include
// both `Options` and `Tries`
val c: Int > (Options with Tries) = b
val c: Int > (Options with Tries) =
b

// Directly widening a pure value to have
// `Options` and `Tries`
val d: Int > (Options with Tries) = 42
val d: Int > (Options with Tries) =
42
```

This contravariant encoding enables a fluent API for effectful code. Methods can accept parameters with a specific set of pending effects while also permitting those with fewer or no effects.
Expand Down Expand Up @@ -321,8 +325,10 @@ trait Cache {
val a: Unit > (Envs[Database] with Envs[Cache] with IOs) =
Envs[Database].get.map { db =>
db.count.map {
case 0 => Envs[Cache].get.map(_.clear)
case _ => ()
case 0 =>
Envs[Cache].get.map(_.clear)
case _ =>
()
}
}
```
Expand Down Expand Up @@ -578,6 +584,56 @@ val c: Instant > IOs =
Clocks.run(Clock.default)(a)
```

### Randoms: Random Values

```scala
import kyo.randoms._

// Generate a random 'Int'
val a: Int > Randoms = Randoms.nextInt

// Generate a random 'Int' within a bound
val b: Int > Randoms = Randoms.nextInt(42)

// A few method variants
val c: Long > Randoms = Randoms.nextLong
val d: Double > Randoms = Randoms.nextDouble
val e: Boolean > Randoms = Randoms.nextBoolean
val f: Float > Randoms = Randoms.nextFloat
val g: Double > Randoms = Randoms.nextGaussian

// Obtain a random value from a sequence
val h: Int > Randoms =
Randoms.nextValue(List(1, 2, 3))
```

### Loggers: Logging

```scala
import kyo.loggers._

// Initialize a 'Logger' instance
val a: Logger =
Loggers.init("exampleLog")

// It's also possible to specify a class
val b: Logger =
Loggers.init(this.getClass)

// A 'Logger' provides trace, debug, info,
// warn, and error method variants. Example:
val c: Unit > IOs =
b.error("example")

// Each variant also has a method overload
// that takes a 'Throwable' as a second param
val d: Unit > IOs =
b.error("example", new Exception)
```

> Important: The `Loggers` effect chooses to consider the initialization of a `Logger` instance as being pure even though it may perform side effects. `Logger` instances need to be stored in constant fields for good performance, something not trivial to achieve if `Loggers.init` required an `IOs` suspension.
### Lists:

License
-------
Expand Down
12 changes: 6 additions & 6 deletions kyo-core/shared/src/main/scala/kyo/randoms.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,25 @@ object randoms {
def run[T, S](f: => T > (Randoms with S))(implicit r: Random): T > (IOs with S) =
run[T, S](r)(f)

def nextInt: Int > Randoms =
val nextInt: Int > Randoms =
envs.get.map(_.nextInt)

def nextInt[S](n: Int > S): Int > (S with Randoms) =
n.map(n => envs.get.map(_.nextInt(n)))

def nextLong: Long > Randoms =
val nextLong: Long > Randoms =
envs.get.map(_.nextLong)

def nextDouble: Double > Randoms =
val nextDouble: Double > Randoms =
envs.get.map(_.nextDouble)

def nextBoolean: Boolean > Randoms =
val nextBoolean: Boolean > Randoms =
envs.get.map(_.nextBoolean)

def nextFloat: Float > Randoms =
val nextFloat: Float > Randoms =
envs.get.map(_.nextFloat)

def nextGaussian: Double > Randoms =
val nextGaussian: Double > Randoms =
envs.get.map(_.nextGaussian)

def nextValue[T, S](seq: Seq[T] > S): T > (S with Randoms) =
Expand Down

0 comments on commit b249e38

Please sign in to comment.