Guest User

Untitled

a guest
Jul 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.98 KB | None | 0 0
  1. // *** Iterators / Iteratees *** //
  2.  
  3. // Mutable variant:
  4. trait Iterator[E] {
  5.   def next: E
  6.   def hasNext: Boolean
  7. }
  8.  
  9. // Immutable variant:
  10. trait Iterator[E] {
  11.   def next: Option[(E, Iterator[E])]
  12. }
  13.  
  14. // Free variant:
  15. case class Get[E, A](f: Option[E] => A)
  16. trait Iteratee[E, A] = Free[({type λ[x] = Get[E, x]})#λ, A]
  17.  
  18. // Why would you ever use Iteratee when you have State with Iterator?
  19.  
  20.  
  21. // *** Consumers / Lists *** /
  22.  
  23. // Mutable variant, for use with Reader.
  24. trait Consumer[A] {
  25.   def feed(a: A): Unit
  26.   def eof: Unit
  27. }
  28.  
  29. // Immutable variant, for use with State.
  30. trait Consumer[-A] {
  31.   def feed(a: A): Consumer[A]
  32.   def eof: Consumer[A]
  33. }
  34.  
  35. // Equivalent little DSL, for use with Free.
  36. trait Producer[S, +A]
  37. case class Produce[S, A](s: S, a: A)
  38.  
  39. type IntProducer[A] = Producer[Int, A]
  40.  
  41. // Note that Free[IntProducer, Unit] is equivalent to List[Int]
  42.  
  43. // The question is:
  44. //   Why would we ever use List[Int] when State[Consumer[Int], A] works just fine?
Add Comment
Please, Sign In to add comment