Skip to content

Instantly share code, notes, and snippets.

@drstevens
Forked from travisbrown/trampolined-state.scala
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save drstevens/3ea464446ee59463af1e to your computer and use it in GitHub Desktop.

Select an option

Save drstevens/3ea464446ee59463af1e to your computer and use it in GitHub Desktop.

Revisions

  1. drstevens revised this gist Jun 16, 2014. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion trampolined-state.scala
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,17 @@ val s = (1 to 10000).foldLeft(state[List[Int], Unit](()).lift[Free.Trampoline])
    case (st, i) => st *> setS(i).lift[Free.Trampoline]
    }

    s(Nil).run
    s(Nil).run

    // Purposely choosing not to traverse here for demonstration purposes
    val s2 = (1 to 10000).foldLeft(state[List[Int], List[Unit]](List.empty).lift[Free.Trampoline]) {
    case (st, i) => (st |@| setS(i).lift[Free.Trampoline])((xs, x) => x :: xs)
    }

    s2(Nil).run

    val s3 = (1 to 10000).toList.traverseU {
    i => setS(i).lift[Free.Trampoline]
    }

    s3(Nil).run
  2. drstevens revised this gist Jun 10, 2014. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions trampolined-state.scala
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    import scalaz._, Scalaz._

    def setS(i: Int): State[List[Int], Unit] = modify(i :: _)

    val s = (1 to 10000).foldLeft(state[List[Int], Unit](()).lift[Free.Trampoline]) {
    case (st, i) => st.flatMap(_ => setS(i).lift[Free.Trampoline])
    case (st, i) => st *> setS(i).lift[Free.Trampoline]
    }

    s(Nil)
    s(Nil).run
  3. @travisbrown travisbrown created this gist May 31, 2014.
    9 changes: 9 additions & 0 deletions trampolined-state.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    import scalaz._, Scalaz._

    def setS(i: Int): State[List[Int], Unit] = modify(i :: _)

    val s = (1 to 10000).foldLeft(state[List[Int], Unit](()).lift[Free.Trampoline]) {
    case (st, i) => st.flatMap(_ => setS(i).lift[Free.Trampoline])
    }

    s(Nil)