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
- Product
- Equals
- Keyword
- AnyVal
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- Protected
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
- Definition Classes
- Keyword
- Annotations
- @inline()
- 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
- def productElementNames: Iterator[String]
- Definition Classes
- Product
- final def unary_!: Unit
- Definition Classes
- Keyword
- Annotations
- @shift() @compileTimeOnly("This method requires the compiler plugin: `addCompilerPlugin(\"com.thoughtworks.dsl\" %% \"compilerplugins-bangnotation\" % \"latest.release\")` and must only be called inside a code block annotated as `@reset`.")
- val value: S
Deprecated Value Members
- def →[B](y: B): (Put[S], B)
- Implicit
- This member is added by an implicit conversion from Put[S] toArrowAssoc[Put[S]] performed by method ArrowAssoc in scala.Predef.
- Definition Classes
- ArrowAssoc
- Annotations
- @deprecated
- Deprecated
(Since version 2.13.0) Use
->
instead. If you still wish to display it as one character, consider using a font with programming ligatures such as Fira Code.