Moore
A Moore machine: a current output together with a step function that consumes an input and yields the next machine.
Useful as a resumable left fold whose intermediate result is always observable. Encoded as K3 with a phantom Unit so that it fits Profunctor<T, F>.
Instances: MooreFunctor, MooreApplicative, MooreCopointed, MooreComonad, MooreProfunctor.
val sum = Moore.unfold(0, { s -> s }) { s, i: Int -> s + i }
sum.run(listOf(1, 2, 3)) // 6
sum.scan(listOf(1, 2, 3)) // [0, 1, 3, 6]
(sum lmap String::length).run(listOf("ab", "c")) // 3Content copied to clipboard