This repository has been archived by the owner on May 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmaybe.erlt
65 lines (51 loc) · 2.24 KB
/
maybe.erlt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
%% Copyright (c) Facebook, Inc. and its affiliates.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(maybe).
-export_type([maybe/1]).
%% TRANSFORM
-export([with_default/2, with_default/1, map/2, map/1, map2/3, map2/1, and_then/2, and_then/1]).
%% FOR INTERNAL USE ONLY (Original Elm comment)
-export([is_just/1, destruct/3]).
-enum maybe(A) :: (just{A}, nothing).
-spec with_default(A, maybe(A)) -> A.
with_default(_Default, maybe.just{Value}) -> Value;
with_default(Default, maybe.nothing) -> Default.
-spec with_default(A) -> fun((maybe(A)) -> A).
with_default(Default) ->
fun(Maybe) -> with_default(Default, Maybe) end.
-spec map(fun((A) -> B), maybe(A)) -> maybe(B).
map(F, maybe.just{Value}) -> maybe.just{F(Value)};
map(_F, maybe.nothing) -> maybe.nothing.
-spec map(fun((A) -> B)) -> fun((maybe(A)) -> maybe(B)).
map(F) ->
fun(Maybe) -> map(F, Maybe) end.
-spec map2(fun((A, B) -> C), maybe(A), maybe(B)) -> maybe(C).
map2(F, maybe.just{A}, maybe.just{B}) -> maybe.just{F(A, B)};
map2(_F, _Ma, _Mb) -> maybe.nothing.
-spec map2(fun((A, B) -> C)) -> fun((maybe(A), maybe(B)) -> maybe(C)).
map2(F) ->
fun(Ma, Mb) -> map2(F, Ma, Mb) end.
-spec and_then(fun((A) -> maybe(B)), maybe(A)) -> maybe(B).
and_then(Callback, maybe.just{Value}) -> Callback(Value);
and_then(_Callback, maybe.nothing) -> maybe.nothing.
-spec and_then(fun((A) -> maybe(B))) -> fun((maybe(A)) -> maybe(B)).
and_then(Callback) ->
fun(Ma) -> and_then(Callback, Ma) end.
%% FOR INTERNAL USE ONLY (Original Elm comment)
-spec is_just(maybe(_)) -> boolean().
is_just(maybe.just{_}) -> true;
is_just(maybe.nothing) -> false.
-spec destruct(B, fun((A) -> B), maybe(A)) -> B.
destruct(_Default, Func, maybe.just{A}) -> Func(A);
destruct(Default, _Func, maybe.nothing) -> Default.