Monads/Writer monad: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(3 intermediate revisions by 3 users not shown)
Line 450:
half → 1
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|Go}}
<syntaxhighlight lang="vbnet">Type mwriter
value As Double
log_ As String
End Type
 
Function Unit(v As Double, s As String) As mwriter
Dim As mwriter mw
mw.value = v
mw.log_ = " " & s & ": " & Str(v) & Chr(10)
Return mw
End Function
 
Function Root(mw As mwriter) As mwriter
mw.value = Sqr(mw.value)
mw.log_ = mw.log_ & " Took square Root: " & Str(mw.value) & Chr(10)
Return mw
End Function
 
Function addOne(mw As mwriter) As mwriter
mw.value = mw.value + 1
mw.log_ = mw.log_ & " Added one : " & Str(mw.value) & Chr(10)
Return mw
End Function
 
Function Half(mw As mwriter) As mwriter
mw.value = mw.value / 2
mw.log_ = mw.log_ & " Divided by two : " & Str(mw.value) & Chr(10)
Return mw
End Function
 
Dim As mwriter mw1
mw1 = Unit(5, "Initial value ")
mw1 = Root(mw1)
mw1 = addOne(mw1)
mw1 = Half(mw1)
Print "The Golden Ratio is "; mw1.value
Print !"\nThis was derived as follows:-"
Print mw1.log_
 
Sleep</syntaxhighlight>
{{out}}
<pre>The Golden Ratio is 1.618033988749895
 
This was derived as follows:-
Initial value : 5
Took square Root: 2.23606797749979
Added one : 3.23606797749979
Divided by two : 1.618033988749895</pre>
 
=={{header|F_Sharp|F#}}==
Line 619 ⟶ 670:
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
<syntaxhighlight>
 
import java.util.function.Function;
 
Line 1,130 ⟶ 1,179:
=={{header|Python}}==
 
<syntaxhighlight lang="python">"""A Writer Monad. Requires Python >= 3.7 for type hints."""
"""A Writer Monad. Requires Python >= 3.7 for type hints."""
from __future__ import annotations
 
Line 1,137 ⟶ 1,187:
import os
 
from typing import Any
from typing import Callable
from typing import Generic
Line 1,146 ⟶ 1,195:
 
T = TypeVar("T")
U = TypeVar("U")
 
 
Line 1,157 ⟶ 1,207:
self.msgs = list(f"{msg}: {self.value}" for msg in msgs)
 
def bind(self, func: Callable[[T], Writer[AnyU]]) -> Writer[AnyU]:
writer = func(self.value)
return Writer(writer, *self.msgs)
 
def __rshift__(self, func: Callable[[T], Writer[AnyU]]) -> Writer[AnyU]:
return self.bind(func)
 
Line 1,171 ⟶ 1,221:
 
 
def lift(func: Callable[[T], U], msg: str) -> Callable[[AnyT], Writer[AnyU]]:
"""Return a writer monad version of the simple function `func`."""
 
@functools.wraps(func)
def wrapped(value: T) -> Writer[U]:
return Writer(func(value), msg)
 
Line 1,183 ⟶ 1,233:
if __name__ == "__main__":
square_root = lift(math.sqrt, "square root")
 
add_one = lift(lambda x: x + 1, "add one")
add_one: Callable[[Union[int, float]], Writer[Union[int, float]]] = lift(
half = lift(lambda x: x / 2, "div two")
add_one = lift( lambda x: x + 1, "add one")
)
 
half: Callable[[Union[int, float]], Writer[float]] = lift(
half = lift( lambda x: x / 2, "div two")
)
 
print(Writer(5, "initial") >> square_root >> add_one >> half)
Line 1,486 ⟶ 1,542:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
class Mwriter {
2,130

edits