Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c28d392
0.9.0 dependencies in README.md
Mar 10, 2019
60b6d78
Add imports to Eval examples
Mar 25, 2019
31b4c7b
Having a paragraph after the example, make dokka generate the snippet
Mar 25, 2019
276765b
Remove old documentation for Eval
Mar 25, 2019
2314edc
Change links to new Eval documentation
Mar 25, 2019
43ef096
Modify Eval link in sidebar
Mar 26, 2019
6384423
Merge branch 'master' into feature-jmendez-migrate-doc-item-1
vejeta Mar 26, 2019
b5524c9
Restore README.md
Mar 26, 2019
97307c5
Merge branch 'feature-jmendez-migrate-doc-item-1' of github.com:vejet…
Mar 26, 2019
b6b390a
add playground label to ank
AdrianRaFo Apr 24, 2019
460a10d
Merge branch 'master' into feature-jmendez-migrate-doc-item-1
vejeta May 9, 2019
0c54e6c
Merge branch 'master' into feature-jmendez-migrate-doc-item-1
vejeta May 15, 2019
bad965c
Adding boilerplate needed to run playground code
May 15, 2019
525aa34
Adapt the rest of the examples for playground support
May 16, 2019
e555b59
Update modules/docs/arrow-docs/docs/docs/datatypes/intro/README.md
vejeta May 16, 2019
158a201
Update modules/docs/arrow-docs/docs/docs/quickstart/libraries/README.md
vejeta May 16, 2019
12bae68
Update modules/docs/arrow-docs/docs/docs/arrow/typeclasses/monad/READ…
vejeta May 16, 2019
be71047
Update modules/docs/arrow-docs/docs/docs/arrow/typeclasses/monad/READ…
vejeta May 16, 2019
c91118a
Update modules/docs/arrow-docs/docs/docs/arrow/typeclasses/foldable/R…
vejeta May 16, 2019
5a8cfc6
Update sampleStart sampleEnd lines
May 16, 2019
67e90c8
Merge branch 'feature-jmendez-migrate-doc-item-1' of github.com:vejet…
May 16, 2019
738561e
Needed space to pass ktlint checks
May 16, 2019
09a432f
Merge branch 'master' into feature-jmendez-migrate-doc-item-1
vejeta May 16, 2019
37b0587
Remove unneeded diagram macro
May 17, 2019
e23b71e
Merge branch 'feature-jmendez-migrate-doc-item-1' of github.com:vejet…
May 17, 2019
921f4bf
Merge branch 'master' into feature-jmendez-migrate-doc-item-1
pakoito May 17, 2019
521fc4e
Merge branch 'master' into feature-jmendez-migrate-doc-item-1
pakoito May 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions modules/core/arrow-core-data/src/main/kotlin/arrow/core/Eval.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import arrow.higherkind
fun <A> EvalOf<A>.value(): A = this.fix().value()

/**
* ank_macro_hierarchy(arrow.core.Eval)
*
* Eval is a monad which controls evaluation of a value or a computation that produces a value.
*
* Three basic evaluation strategies:
Expand All @@ -29,6 +31,32 @@ fun <A> EvalOf<A>.value(): A = this.fix().value()
* Eval instances whose computation involves calling .value on another
* Eval instance -- this can defeat the trampolining and lead to stack
* overflows.
*
* Example of stack safety:
*
* ```kotlin:ank:playground
* import arrow.core.Eval
*
* //sampleStart
* fun even(n: Int): Eval<Boolean> =
* Eval.always { n == 0 }.flatMap {
* if(it == true) Eval.now(true)
* else odd(n - 1)
* }
*
* fun odd(n: Int): Eval<Boolean> =
* Eval.always { n == 0 }.flatMap {
* if(it == true) Eval.now(false)
* else even(n - 1)
* }
*
* // if not wrapped in eval this type of computation would blow the stack and result in a StackOverflowError
* fun main() {
* println(odd(100000).value())
* }
* //sampleEnd
* ```
*
*/
@higherkind
sealed class Eval<out A> : EvalOf<A> {
Expand All @@ -45,10 +73,64 @@ sealed class Eval<out A> : EvalOf<A> {

fun <A> just(a: A): Eval<A> = now(a)

/**
* Creates an Eval instance from an already constructed value but still defers evaluation when chaining expressions with `map` and `flatMap`
*
* @param a is an already computed value of type [A]
*
* ```kotlin:ank:playground
* import arrow.core.*
*
* fun main() {
* //sampleStart
* val eager = Eval.now(1).map { it + 1 }
* println(eager.value())
* //sampleEnd
* }
* ```
*
* It will return 2.
*/
fun <A> now(a: A) = Now(a)

/**
* Creates an Eval instance from a function deferring it's evaluation until `.value()` is invoked memoizing the computed value.
*
* @param f is a function or computation that will be called only once when `.value()` is invoked for the first time.
*
* ```kotlin:ank:playground
* import arrow.core.*
*
* fun main() {
* //sampleStart
* val lazyEvaled = Eval.later { "expensive computation" }
* println(lazyEvaled.value())
* //sampleEnd
* }
* ```
*
* "expensive computation" is only computed once since the results are memoized and multiple calls to `value()` will just return the cached value.
*/
fun <A> later(f: () -> A) = Later(f)

/**
* Creates an Eval instance from a function deferring it's evaluation until `.value()` is invoked recomputing each time `.value()` is invoked.
*
* @param f is a function or computation that will be called every time `.value()` is invoked.
*
* ```kotlin:ank:playground
* import arrow.core.*
*
* fun main() {
* //sampleStart
* val alwaysEvaled = Eval.always { "expensive computation" }
* println(alwaysEvaled.value())
* //sampleEnd
* }
* ```
*
* "expensive computation" is computed every time `value()` is invoked.
*/
fun <A> always(f: () -> A) = Always(f)

fun <A> defer(f: () -> Eval<A>): Eval<A> = Defer(f)
Expand Down
2 changes: 1 addition & 1 deletion modules/docs/arrow-docs/docs/_data/sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ options:
url: /docs/arrow/data/coproduct/

- title: Eval
url: /docs/arrow/core/eval/
url: /docs/apidocs/arrow-core-data/arrow.core/-eval/

- title: OptionT
url: /docs/arrow/data/optiont/
Expand Down
87 changes: 0 additions & 87 deletions modules/docs/arrow-docs/docs/docs/arrow/core/eval/README.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Right associative lazy fold on `F` using the provided function.

This method evaluates `lb` lazily, and returns a lazy value to support laziness in a stack-safe way avoiding StackOverflows.

For more detailed information about how this method works see the documentation for [`Eval<A>`]({{ '/docs/arrow/core/eval' | relative_url }}).
For more detailed information about how this method works see the documentation for [`Eval<A>`]({{ '/docs/apidocs/arrow-core-data/arrow.core/-eval' | relative_url }}).

```kotlin:ank:silent
fun <F> concatenateStringFromRight(strKind: Kind<F, String>, FO: Foldable<F>): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ Some(5).mproduct {
#### followedBy/followedByEval

Executes sequentially two elements that are independent from one another.
The [`Eval`]({{ '/docs/arrow/core/eval' | relative_url }}) variant allows you to pass lazily calculated values.
The [`Eval`]({{ '/docs/apidocs/arrow-core-data/arrow.core/-eval' | relative_url }}) variant allows you to pass lazily calculated values.

```kotlin:ank
import arrow.core.extensions.option.monad.followedBy

Some(1).followedBy(Some(2))
```

Expand All @@ -108,7 +108,7 @@ IO.just(1).effectM(::logValue).fix().unsafeRunSync()
#### forEffect/forEffectEval

Executes sequentially two elements that are independent from one another, ignoring the value of the second one.
The [`Eval`]({{ '/docs/arrow/core/eval' | relative_url }}) variant allows you to pass lazily calculated values.
The [`Eval`]({{ '/docs/apidocs/arrow-core-data/arrow.core/-eval' | relative_url }}) variant allows you to pass lazily calculated values.

```kotlin:ank
import arrow.core.extensions.option.monad.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ so they are always required.

- [`Either`]({{ '/docs/arrow/core/either/' | relative_url }}) - an if/else branch in execution

- [`Eval`]({{ '/docs/arrow/core/eval/' | relative_url }}) - lazy evaluation of functions with stack safety and memoization
- [`Eval`]({{ '/docs/apidocs/arrow-core-data/arrow.core/-eval' | relative_url }}) - lazy evaluation of functions with stack safety and memoization

- `TupleN` - a heterogeneous grouping of 2-9 values without creating a named class

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ beginner
The smallest set of [datatypes]({{ '/docs/datatypes/intro/' | relative_url }}) necessary to start in FP, and that other libraries can build upon.
The focus here is on API design and abstracting small code patterns.

Datatypes: [`Either`]({{ '/docs/arrow/core/either/' | relative_url }}), [`Option`]({{ '/docs/arrow/core/option/' | relative_url }}), [`Try`]({{ '/docs/arrow/core/try/' | relative_url }}), [`Eval`]({{ '/docs/arrow/core/eval/' | relative_url }}), [`Id`]({{ '/docs/arrow/core/id/' | relative_url }}), `TupleN`, `Function0`, `Function1`, `FunctionK`
Datatypes: [`Either`]({{ '/docs/arrow/core/either/' | relative_url }}), [`Option`]({{ '/docs/arrow/core/option/' | relative_url }}), [`Try`]({{ '/docs/arrow/core/try/' | relative_url }}), [`Eval`]({{ '/docs/apidocs/arrow-core-data/arrow.core/-eval' | relative_url }}), [`Id`]({{ '/docs/arrow/core/id/' | relative_url }}), `TupleN`, `Function0`, `Function1`, `FunctionK`

### arrow-syntax

Expand Down