Skip to content

Instantly share code, notes, and snippets.

@betafcc
Created January 25, 2019 08:57
Show Gist options
  • Save betafcc/4ebd38b15e8270ed7d6e59de477e44cf to your computer and use it in GitHub Desktop.
Save betafcc/4ebd38b15e8270ed7d6e59de477e44cf to your computer and use it in GitHub Desktop.
Maybe::Just() {
printf 'Maybe::Just(%s)' "${1}"
}
Maybe::Nothing() {
printf 'Maybe::Nothing'
}
Maybe::map() {
case "${2}" in
'Maybe::Just('*')') Maybe::Just "$(${1} "$(args "${2}")")";;
'Maybe::Nothing') Maybe::Nothing;;
esac
}
ctor() {
printf '%s' "${1%%\(*}"
}
args() {
local args="${1#$(ctor "${1}")}"
printf '%s' "${args:1:-1}"
}
inc() {
printf $(($1 + 1))
}
if [[ ${BASH_SOURCE[0]} = ${0} ]]; then
echo ' [in]: Maybe::map inc "$(Maybe::Just 10)"'
printf '[out]: '
Maybe::map inc "$(Maybe::Just 10)"
echo
echo
printf ' [in]: '
printf 'm=$(Maybe::Just 10)
...: mm=$(Maybe::Just ${m})
...: echo mm is "${mm}"
...: Maybe::map "Maybe::map inc" "${mm}"'
echo
echo
echo '[out]: '
m=$(Maybe::Just 10)
mm=$(Maybe::Just ${m})
echo mm is "${mm}"
Maybe::map "Maybe::map inc" "${mm}"
echo
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment