Skip to content

Instantly share code, notes, and snippets.

@jeffbowman
Last active August 2, 2016 16:56
Show Gist options
  • Save jeffbowman/483d9b2318c0ba0ea86402ae7ae0f649 to your computer and use it in GitHub Desktop.
Save jeffbowman/483d9b2318c0ba0ea86402ae7ae0f649 to your computer and use it in GitHub Desktop.
G+ response to some F# code
open System.Text.RegularExpressions
open FsUnit
open NUnit.Framework
let parseLot lot =
match lot with
| x when Regex.Match(x, @"^\d+$").Success -> [ x |> int ]
| x when Regex.Match(x, @"^\d+-\d+$").Success ->
let parts = x.Split('-')
[ parts.[0] |> int..parts.[1] |> int ]
| _ -> []
let splitLots (lots : string) =
lots.Replace(" ", "").Split(',')
|> Array.toList
|> List.fold (fun acc l -> acc @ (parseLot l)) []
[<Test>]
let ``Given string of digits, splitLots2 should return list of digits``() =
splitLots "1" |> should equal [ 1 ]
splitLots "1, 2, 3, 5, 7, 10" |> should equal [ 1; 2; 3; 5; 7; 10 ]
[<Test>]
let ``Given string range of digits, splitLots2 should return list of digits``() =
splitLots "1-3" |> should equal [ 1; 2; 3 ]
[<Test>]
let ``Given string with digits and range, splitLots2 should return a list of digits``() =
splitLots "1, 2, 5-7, 10-12, 15"
|> should equal [ 1; 2; 5; 6; 7; 10; 11; 12; 15 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment