Created
March 16, 2015 19:01
-
-
Save tom-galvin/2309d4f7b93631c99f22 to your computer and use it in GitHub Desktop.
Miscellaneous F# Utilities
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
// Don't know why these functions aren't part of the language but there you are. | |
module Util | |
module String = | |
let splitArray (delimiters: string list) (s: string) = | |
s.Split((Array.ofList delimiters), System.StringSplitOptions.None) | |
let splitArrayMax (delimiters: string list) max (s: string) = | |
s.Split((Array.ofList delimiters), max, System.StringSplitOptions.None) | |
let split delimiters s = splitArray delimiters s |> List.ofArray | |
let splitMax delimiters max s = splitArrayMax delimiters max s |> List.ofArray | |
let splitSeq delimiters s = splitArray delimiters s |> List.ofSeq | |
let splitSeqMax delimiters max s = splitArrayMax delimiters max s |> List.ofSeq | |
module List = | |
let distinct source = | |
let folder acc item = | |
if List.exists ((=) item) acc then | |
acc | |
else | |
item :: acc | |
match source with | |
| [] -> [] | |
| [i] -> [i] | |
| first :: rest -> List.fold folder [first] rest |> List.rev | |
let distinctBy projection source = | |
let folder acc item = | |
let itemKey = projection item | |
if List.exists (fun (_, key) -> key = itemKey) acc then | |
acc | |
else | |
(item, itemKey) :: acc | |
match source with | |
| [] -> [] | |
| [i] -> [i] | |
| first :: rest -> List.fold folder [first, projection first] rest |> List.rev |> List.map fst |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment