Created
May 9, 2018 18:54
-
-
Save mlms13/90e5e02f2c6f0eec5d3f4fc6cf98510d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
type t('a) = | |
| NonEmpty('a, list('a)); | |
let singleton = (x: 'a) => NonEmpty(x, []); | |
let fromList = (l: list('a)): option(t('a)) => switch l { | |
| [] => None | |
| [x, ...xs] => Some(NonEmpty(x, xs)) | |
}; | |
let toList = (NonEmpty(x, xs)) => [x, ...xs]; | |
let head = (NonEmpty(x, _)) => x; | |
let tail = (NonEmpty(_, xs)) => xs; | |
let map = (f: 'a => 'b, (NonEmpty(x, xs))) => | |
NonEmpty(f(x), Belt.List.map(xs, f)); | |
let foldl = (f: ('b => 'a => 'b), init: 'b, data: t('a)): 'b => | |
Belt.List.reduce(toList(data), init, f); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment