Monads/Maybe monad: Difference between revisions
Added FreeBASIC
(Added FreeBASIC) |
|||
(6 intermediate revisions by 3 users not shown) | |||
Line 770:
→ ❌ (#f . 0)
</syntaxhighlight>
=={{header|FreeBASIC}}==
{{trans|Wren}}
<syntaxhighlight lang="vbnet">Type mmaybe▼
As Integer value
End Type
Function Bindf(m As mmaybe, f As Function(As mmaybe) As mmaybe) As mmaybe
Return f(m)
End Function
Function Unit(i As Integer) As mmaybe
Dim As mmaybe m
m.value = i
Return m
End Function
Function Decrement(mm As mmaybe) As mmaybe
Dim As mmaybe result
result.value = Iif(mm.value = 0, 0, mm.value - 1)
Return Unit(result.value)
End Function
Function Triple(mm As mmaybe) As mmaybe
Dim As mmaybe result
result.value = Iif(mm.value = 0, 0, 3 * mm.value)
Return Unit(result.value)
End Function
Dim As Integer values(3) = {3, 4, 0, 5}
Dim As Function(As mmaybe) As mmaybe Ptr decrementPtr = @Decrement
Dim As Function(As mmaybe) As mmaybe Ptr triplePtr = @Triple
For i As Integer = Lbound(values) To Ubound(values)
Dim As mmaybe m1 = Unit(values(i))
Dim As mmaybe m2 = Bindf(Bindf(m1, decrementPtr), triplePtr)
Dim As String s1 = Iif(m1.value = 0, "none", Str(m1.value))
Dim As String s2 = Iif(m2.value = 0, "none", Str(m2.value))
Print Using "\ \ -> \ \"; s1; s2
Next i
Sleep</syntaxhighlight>
{{out}}
<pre> 3 -> 6
4 -> 9
none -> none
5 -> 12</pre>
=={{header|F_Sharp|F#}}==
Line 992 ⟶ 1,039:
=={{header|Java}}==
Java has a built-in generic "Maybe" monad in form of the Optional<T> class.
The class has static methods, "of" and "ofNullable", which act as the unit function
▲<syntaxhighlight>
for wrapping nullable and non-nullable values respectively.
The class instance method, "flatMap", acts as the bind function.
<syntaxhighlight lang="java">
import java.util.Optional;
public final class MonadMaybe {
public static void main(String[] aArgs) {
System.out.println(doubler(5).flatMap(MonadMaybe::stringify).get());
System.out.println(doubler(2).flatMap(MonadMaybe::stringifyNullable).get());
Optional<String> option = doubler(0).flatMap(MonadMaybe::stringifyNullable);
String result = option.isPresent() ? option.get() : "Result is Null";
System.out.println(result);
}
private static Optional<Integer> doubler(int aN) {
return Optional.of(2 * aN);
}
private static Optional<String> stringify(int aN) {
return Optional.of("A".repeat(aN));
}
private static Optional<String> stringifyNullable(int aN) {
return ( aN > 0 ) ? Optional.ofNullable("A".repeat(aN)) : Optional.ofNullable(null);
}
}
</syntaxhighlight>
{{ out }}
<pre>
AAAAAAAAAA
AAAA
Result is Null
</pre>
Line 1,558 ⟶ 1,640:
=={{header|Python}}==
The <code>Maybe</code> class constructor is effectively the <code>unit</code> function. Note that I've used <code>>></code> as the bind operator. Trying to chain <code>__irshift__</code> (<code>>>=</code>) would be a syntax error.
<syntaxhighlight lang="python">
"""A Maybe monad. Requires Python >= 3.7 for type hints."""
from __future__ import annotations
from typing import Callable
from typing import Generic
Line 1,575 ⟶ 1,654:
T = TypeVar("T")
U = TypeVar("U")
Line 1,584 ⟶ 1,664:
self.value = value
def __rshift__(self, func: Callable[[Optional[T]], Maybe[
return self.bind(func)
def bind(self, func: Callable[[Optional[T]], Maybe[
return func(self.value)
Line 1,596 ⟶ 1,676:
def plus_one(value: Optional[int]) -> Maybe[int]:
if value is not None:
return Maybe
return Maybe
def currency(value: Optional[int]) -> Maybe[str]:
if value is not None:
return Maybe
return Maybe
Line 1,610 ⟶ 1,690:
for case in test_cases:
# or..
# result =
print(f"{str(case):<4} -> {result}")
</syntaxhighlight>
Line 2,033 ⟶ 2,114:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
class Maybe {
Line 2,062 ⟶ 2,143:
var s1 = (m1.value) ? "%(m1.value)" : "none"
var s2 = (m2.value) ? "%(m2.value)" : "none"
}</syntaxhighlight>
|