Created
September 12, 2014 15:19
-
-
Save cohama/98b1eeaedc0f3cd636a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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