final case class Put[S](value: S) extends AnyVal with Keyword[Put[S], Unit] with Product with Serializable
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.
Author:
杨博 (Yang Bo)
- Source
- Put.scala
The following example creates a function that accepts a string parameter and returns the upper-cased last character of the parameter.
def upperCasedLastCharacter: 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 beO
.// Output: 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.def formatter: 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")
- See also
- Alphabetic
- By Inheritance
- Put
- Serializable
- Serializable
- Product
- Equals
- Keyword
- AnyVal
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Instance Constructors
- new Put(value: S)
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
##(): Int
- Definition Classes
- Any
- def +(other: String): String
- def ->[B](y: B): (Put[S], B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
apply[Domain](handler: (Unit) ⇒ Domain)(implicit dsl: Dsl[Put[S], Domain, Unit]): Domain
An alias to cpsApply.
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
final
def
cpsApply[Domain](handler: (Unit) ⇒ Domain)(implicit dsl: Dsl[Put[S], Domain, Unit]): Domain
- Definition Classes
- Keyword
- Annotations
- @inline()
- def ensuring(cond: (Put[S]) ⇒ Boolean, msg: ⇒ Any): Put[S]
- def ensuring(cond: (Put[S]) ⇒ Boolean): Put[S]
- def ensuring(cond: Boolean, msg: ⇒ Any): Put[S]
- def ensuring(cond: Boolean): Put[S]
- def formatted(fmtstr: String): String
-
def
getClass(): Class[_ <: AnyVal]
- Definition Classes
- AnyVal → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def unary_!: Unit
- val value: S
- def →[B](y: B): (Put[S], B)