Monads/Writer monad: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(5 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 617 ⟶ 668:
added 1 -> 3.23607
divided by 2 -> 1.61803</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.function.Function;
 
public final class MonadWriter {
 
public static void main(String[] aArgs) {
Monad<Double> initial = Monad.unit(5.0, "Initial value");
Monad<Double> result = initial.bind(MonadWriter::root).bind(MonadWriter::addOne).bind(MonadWriter::half);
System.out.println("The Golden Ratio is " + result.getValue() + System.lineSeparator());
System.out.println("This was derived as follows:" + System.lineSeparator() + result.getText());
}
private static Monad<Double> root(double aD) {
return Monad.unit(Math.sqrt(aD), "Took square root");
}
 
private static Monad<Double> addOne(double aD) {
return Monad.unit(aD + 1.0, "Added one");
}
 
private static Monad<Double> half(double aD) {
return Monad.unit(aD / 2.0, "Divided by two");
}
}
final class Monad<T> {
public static <T> Monad<T> unit(T aValue, String aText) {
return new Monad<T>(aValue, aText);
}
public Monad<T> bind(Function<T, Monad<T>> aFunction) {
Monad<T> monad = aFunction.apply(value);
monad.text = text + monad.text;
return monad;
}
public T getValue() {
return value;
}
public String getText() {
return text;
}
private Monad(T aValue, String aText) {
value = aValue;
text = String.format("%-21s%s%n", " " + aText, ": " + aValue);
}
private T value;
private String text;
}
</syntaxhighlight>
{{ out }}
<pre>
The Golden Ratio is 1.618033988749895
 
This was derived as follows:
Initial value : 5.0
Took square root : 2.23606797749979
Added one : 3.23606797749979
Divided by two : 1.618033988749895
</pre>
 
=={{header|JavaScript}}==
Line 1,060 ⟶ 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,067 ⟶ 1,187:
import os
 
from typing import Any
from typing import Callable
from typing import Generic
Line 1,076 ⟶ 1,195:
 
T = TypeVar("T")
U = TypeVar("U")
 
 
Line 1,087 ⟶ 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,101 ⟶ 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,113 ⟶ 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")
lambda x: x + 1, "add one"
)
 
half: Callable[[Union[int, float]], Writer[float]] = lift(
lambda x: x / 2, "div two"
)
 
print(Writer(5, "initial") >> square_root >> add_one >> half)
Line 1,416 ⟶ 1,542:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
class Mwriter {
2,130

edits