Put

final case
class Put[S](value: S) extends AnyVal

Put is a Keyword to replace the value of the current context.

Purely functional programming languages usually do not support native first-class mutable variables. In those languages, mutable states can be implemented in state monads.

Put and Get are the Dsl-based replacements of state monads.

We use unary function as the domain of mutable state. The parameter of the unary function can be read from the Get keyword, and changed by the Put keyword.

Authors

杨博 (Yang Bo)

See also
Example

The following example creates a function that accepts a string parameter and returns the upper-cased last character of the parameter.

import com.thoughtworks.dsl.bangnotation.{unary_!, reset}
def upperCasedLastCharacter = reset[String => Char] {
 val initialValue = !Get[String]()
 !Put(initialValue.toUpperCase)
 val upperCased = !Get[String]()
 Function.const(upperCased.last)
}

For example, given a string of foo, the upper-cased last character should be O.

upperCasedLastCharacter("foo") should be('O')

Put and Get support multiple states. The following code creates a formatter that Put parts of content into a Vector[Any] of string buffers.

import com.thoughtworks.dsl.bangnotation.{reset, unary_!}
def formatter = reset[Double => Int => Vector[Any] => String] {
 !Put(!Get[Vector[Any]]() :+ "x=")
 !Put(!Get[Vector[Any]]() :+ !Get[Double]())
 !Put(!Get[Vector[Any]]() :+ ",y=")
 !Put(!Get[Vector[Any]]() :+ !Get[Int]())
 !Return((!Get[Vector[Any]]()).mkString)
}
formatter(0.5)(42)(Vector.empty) should be("x=0.5,y=42")
Companion
object
trait Serializable
trait Product
trait Equals
class AnyVal
trait Matchable
class Any

Value members

Inherited methods

def productElementNames: Iterator[String]
Inherited from
Product
def productIterator: Iterator[Any]
Inherited from
Product