Skip to content

Instantly share code, notes, and snippets.

@swawa-yu
Last active March 26, 2020 17:00
Show Gist options
  • Save swawa-yu/3526907efd15e97fe6b06ab05e41a234 to your computer and use it in GitHub Desktop.
Save swawa-yu/3526907efd15e97fe6b06ab05e41a234 to your computer and use it in GitHub Desktop.
open System.Linq
// aabbbcdddd -> [(a,2), (b,3), (c,1), (d,4)]
let runLength (s: string) =
let heads = { 1 .. s.Length - 1 }.Where(fun i -> s.[i - 1] <> s.[i]).Prepend(0).Append(s.Length).ToArray()
{ 0 .. heads.Length - 2 }.Select(fun i -> s.[heads.[i]], heads.[i + 1] - heads.[i]).ToArray()
// 使用例
input()
|> runLength
|> printfn "%A"
@swawa-yu
Copy link
Author

F# Run-Length Encoding
with LINQ

string -> (char*int)[]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment