Skip to content

Instantly share code, notes, and snippets.

@cohama
Created September 12, 2014 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cohama/98b1eeaedc0f3cd636a2 to your computer and use it in GitHub Desktop.
Save cohama/98b1eeaedc0f3cd636a2 to your computer and use it in GitHub Desktop.
let g:Maybe = {"v": []}
function! g:MaybeReturn(v)
let maybe = deepcopy(g:Maybe)
let maybe.v = [a:v]
return maybe
endfunction
function! Just(x)
return MaybeReturn(a:x)
endfunction
function! Nothing()
let maybe = deepcopy(g:Maybe)
let maybe.v = []
return maybe
endfunction
function! g:Maybe.bind(F)
if len(self.v) ==# 1
let [x] = self.v
return a:F(x)
else
return Nothing()
endif
endfunction
function! GetMaybe1(a)
if a:a % 2 ==# 0
return Just(a:a)
else
return Nothing()
endif
endfunction
function! GetMaybe2(a)
if a:a % 3 ==# 0
return Just(a:a)
else
return Nothing()
endif
endfunction
echo MaybeReturn(6).bind(lambda('return GetMaybe1(a:1)')).bind(lambda('return GetMaybe2(a:1)'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment