Skip to content

Instantly share code, notes, and snippets.

@Sehnsucht-Fr
Created November 3, 2015 20:30
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 Sehnsucht-Fr/f21f222217b58e1a9458 to your computer and use it in GitHub Desktop.
Save Sehnsucht-Fr/f21f222217b58e1a9458 to your computer and use it in GitHub Desktop.
Linq Challenge #1 (F#)
open System
// type aliases
type Dt = DateTime
type Ts = TimeSpan
// utility function
let flip f a b = f b a
let dump name obj = obj.Dump (name : string)
let peek name obj = dump name obj; obj
let split (sep : string) (str : string) = str.Split ([|sep|], StringSplitOptions.RemoveEmptyEntries)
let age (birthday : Dt) =
let today = Dt.Today
let age = today.Year - birthday.Year
if birthday > today.AddYears -age then age - 1 else age
"Davis, Clyne, Fonte, Hooiveld, Shaw, Davis, Schneiderlin, Cork, Lallana, Rodriguez, Lambert"
|> split ", "
|> Seq.mapi ((+) 1 >> sprintf "%d. %s")
|> String.concat ", "
|> dump "Numbering Players"
"Jason Puncheon, 26/06/1986; Jos Hooiveld, 22/04/1983; Kelvin Davis, 29/09/1976; Luke Shaw, 12/07/1995; Gaston Ramirez, 02/12/1990; Adam Lallana, 10/05/1988"
|> split "; "
|> Seq.map (split ", " >> function [|name; birthday|] -> name, age <| Dt.Parse birthday | _ -> failwith "invalid input")
|> Seq.sortBy snd
|> dump "Sort by Age"
"4:12,2:43,3:51,4:29,3:24,3:14,4:46,3:25,4:52,3:27"
|> split ","
|> Seq.map ((+) "0:" >> Ts.Parse)
|> Seq.reduce (+)
|> dump "Album Duration"
seq { for x in 0 .. 2 do for y in 0 .. 2 -> sprintf "%d,%d" x y }
|> String.concat " "
|> dump "Coordinates"
[0 .. 8]
|> Seq.map (fun x -> sprintf "%d,%d" (x / 3) (x % 3))
|> String.concat " "
|> dump "Coordinates (Alternate)"
"00:45,01:32,02:18,03:01,03:44,04:31,05:19,06:01,06:47,07:35"
|> (+) "00:00,"
|> split ","
|> Seq.map ((+) "0:" >> Ts.Parse)
|> Seq.pairwise
|> Seq.map ((<||) <| flip (-))
|> dump "Swim Length Durations"
"2,5,7-10,11,17-18"
|> split ","
|> Seq.collect (fun str ->
match split "-" str with
[| str |] -> [int str]
| [| str1; str2 |] -> [int str1 .. int str2]
| _ -> failwith "invalid input")
|> dump "Ranges"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment