Entry 3324

state

   

Submitted by dflemstr on March 11, 2010 at 6:28 p.m.
Language: Scala. Code size: 856 bytes.

package org.purview.core.process

import org.purview.core.analysis.Analyser

object State {
  def apply[@specialized("Int,Float,Boolean") A](v: A): State[A] = new State[A] {
    protected val value = v
  }
  @inline def unit[@specialized("Int,Float,Boolean") A](v: A) = apply(v)

  def get[@specialized("Int,Float,Boolean") A](s: State[A]) = s.value
}

sealed trait State[@specialized("Int,Float,Boolean") A] {
  protected val value: A

  def map[B](f: A => B)(implicit stageName: String = "function") = new State[B] {
    protected val value = {
      Analyser.statistics.reportStage(stageName)
      f(State.this.value)
    }
  }

  @inline final def fmap[B](f: A => B, stageName: String) = map(f)(stageName)

  def flatMap[B](f: A => State[B]): State[B] = f(value)

  @inline final def >>=[B](f: A => State[B]) = flatMap(f)
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).