miciek / grokkingfp-examples

All examples and exercises from the Grokking Functional Programming book
https://www.manning.com/books/grokking-functional-programming?utm_source=michal&utm_medium=affiliate&utm_campaign=book_khan_grokking_6_26_19&a_aid=michal&a_bid=7c041142
134 stars 51 forks source link

Fix the eager/lazy evaluation example in chapter8 #153

Closed miciek closed 1 year ago

miciek commented 1 year ago

As @gitgithan noticed in #152, there is a problem in examples for lazy/eager evaluation. The problem appears because IO.orElse takes a block of code lazily so it should not be used as an example for eager evaluation 🤦 .

Here are the correct examples:

IO.delay(throw new Exception).orElse(IO.pure(2022)) // lazy, doesn't throw, returns 2022 when run
IO.pure(throw new Exception).orElse(IO.pure(2022)) // eager, throws before creating a value

Closes #152