maybe monad and friends
maybe - return a tuple of (Nothing) or (Just,value) maybemap - apply map function when maybe has a value and wrap in another maybe; else when nothing return nothing maybevalue - return value of maybe; else when nothing return optional default args
This commit is contained in:
parent
9c238777e1
commit
912b3f2491
2 changed files with 69 additions and 0 deletions
40
src/fun.sh
40
src/fun.sh
|
@ -358,3 +358,43 @@ call() {
|
|||
local args=$@
|
||||
tup $f $args
|
||||
}
|
||||
|
||||
maybe() {
|
||||
if [[ $# -eq 0 ]]; then
|
||||
local arg
|
||||
read arg
|
||||
maybe "$arg"
|
||||
else
|
||||
local x="$*"
|
||||
local value=$(echo $x | strip)
|
||||
if [[ ${#value} -eq 0 ]]; then
|
||||
tup Nothing
|
||||
else
|
||||
tup Just "$value"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
maybemap() {
|
||||
local x
|
||||
read x
|
||||
if [[ $(tupl $x) = "Nothing" ]]; then
|
||||
echo $x
|
||||
else
|
||||
local y=$(tupr "$x")
|
||||
local r=$(echo "$y" | map "$@")
|
||||
maybe "$r"
|
||||
fi
|
||||
}
|
||||
|
||||
maybevalue() {
|
||||
local default="$*"
|
||||
local x
|
||||
read x
|
||||
if [[ $(tupl $x) = "Nothing" ]]; then
|
||||
echo "$default"
|
||||
else
|
||||
echo $(tupr $x)
|
||||
fi
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue