Skip to content

Instantly share code, notes, and snippets.

@jwosty
Last active August 29, 2015 14:16
Show Gist options
  • Save jwosty/c594bcb1a1e54faf6cf2 to your computer and use it in GitHub Desktop.
Save jwosty/c594bcb1a1e54faf6cf2 to your computer and use it in GitHub Desktop.
Simple, messy run-length encoder
let inline f tolerance data =
let two = LanguagePrimitives.GenericOne + LanguagePrimitives.GenericOne
data |> List.mapi (fun i c -> i, c)
|>function
| (_,c)::tl ->
tl
|> List.fold (fun (rest, (min, max), x) (i, c) ->
if abs (x-c) <= tolerance
then rest, (min, max+1), (x+c)/two
else ((min,max),x) :: rest, (i, i), c)
([],(0,0),c)
|> function | d, (min, max), x -> ((min, max), x) :: d
| [] -> []
|> List.rev
f 0.5 <| List.map (fun n -> n / 3.) [0. .. 30.]
f 0.5 [1.6; 1.24; 1.99; 2.32; 2.48; 2.53; 2.38; 2.12; 4.97; 5.18; 9.4; 3.14; 3.44] // [((0, 1), 1.42); ((2, 7), 2.2609375); ((8, 9), 5.075); ((10, 10), 9.4); ((11, 12), 3.29)]
f 0.5 List.empty<float> // []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment