Skip to content

Instantly share code, notes, and snippets.

@swlaschin
Created April 26, 2019 15:20
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 swlaschin/4e4d1b3323d120983299d21c6203e2dc to your computer and use it in GitHub Desktop.
Save swlaschin/4e4d1b3323d120983299d21c6203e2dc to your computer and use it in GitHub Desktop.
Utility.fs
namespace global // note use of GLOBAL namespace
type NonEmptyList<'a> = {
First:'a
Rest: 'a list
}
module NonEmptyList =
// aList:'a list -> NonEmptyList<'a> option
let ofList aList =
match aList with
| [] -> None
| head::tail -> Some {First=head; Rest=tail}
// aNonEmptyList:NonEmptyList<'a> -> 'a list
let toList aNonEmptyList =
aNonEmptyList.First :: aNonEmptyList.Rest
let length aNonEmptyList =
aNonEmptyList.Rest.Length + 1
let map f aNonEmptyList = {
First = f aNonEmptyList.First
Rest = List.map f aNonEmptyList.Rest
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment