Skip to content

Instantly share code, notes, and snippets.

@mlms13
Created May 9, 2018 18:54
Show Gist options
  • Save mlms13/90e5e02f2c6f0eec5d3f4fc6cf98510d to your computer and use it in GitHub Desktop.
Save mlms13/90e5e02f2c6f0eec5d3f4fc6cf98510d to your computer and use it in GitHub Desktop.
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