Learn something about your values before they are computed!
import com.koterpillar.foreshadowing._
val isVowel = Set('a', 'e', 'i', 'o', 'u')
val Eventually(res) = for {
len <- Divination[String, Int](_.length)
vowels <- Divination[String, Int](_.count(isVowel))
} yield s"This string has $len characters. Of those, $vowels are vowels."
// res: String = This string has 55 characters. Of those, 12 are vowels.
res.length
// 55
res.count(isVowel)
// 12
Divination power is not very strong yet. Asking for the impossible will lead
one onto the infinite road through time and space @tailrec
fixed the space
bit.
Scala cannot divine up enough types, somewhat breaking the immersion.
This is indirectly inspired by a colleauge mentioning that for
comprehensions are sequencing the computation. Of course they do... normally.
Also see:
- Tardis monad transformer, illustrating cases where there is more than one sequence of operations.
- XKCD diagram that this code should be able to generate.
- GEB, just because.