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.
The behavior of the above code is equivalent to the following code based on native Scala var:
def varBasedState(initialValue: String): Int = {
var v = initialValue
v should be("initial value")
v = "changed value"
v should be("changed value")
return0
}
varBasedState("initial value") should be(0)
,
The parameter of a scala.Function1 can be read from Get keyword, and changed by Put keyword.
def dslBasedState: String=>Int = {
!Get[String]() should be("initial value")
!Put("changed value")
!Get[String]() should be("changed value")
!Return(0)
}
dslBasedState("initial value") should be(0)
The implementation of Get and Put keywords does not use native Scala var,
though its behavior is similar to var.
A Keyword to put the value to the 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.
Author:
杨博 (Yang Bo)
Put and Get support multiple states. The following code creates a formatter that Put parts of content into a
List[Any]
of string buffers.The behavior of the above code is equivalent to the following code based on native Scala
var
:The parameter of a scala.Function1 can be read from Get keyword, and changed by Put keyword.
The implementation of Get and Put keywords does not use native Scala
var
, though its behavior is similar tovar
.Get