Skip to content

Instantly share code, notes, and snippets.

@zoren
Created December 9, 2016 13:11
Show Gist options
  • Save zoren/b3ceb8210d235420388dc155baa14388 to your computer and use it in GitHub Desktop.
Save zoren/b3ceb8210d235420388dc155baa14388 to your computer and use it in GitHub Desktop.
My hacker rank prelude for F#
open System
open System.Collections.Generic
let rl = Console.ReadLine
let splsp (s:string) = s.Split([|' '|], StringSplitOptions.RemoveEmptyEntries)
let getTwo (a:_[]) = a.[0], a.[1]
let getThree (a:_[]) = a.[0], a.[1], a.[2]
let callN n action = seq { for i = 1 to n do yield action()} |> Seq.toArray
let iterrl f =
let rec loop () =
let l = rl()
if l = null then () else f l; loop()
loop()
[<AutoOpen>]
module DictionaryHelper =
type Dictionary<'TK, 'TV> with
member this.TryGetValueOpt k =
match this.TryGetValue k with
| true, v -> Some v
| _ -> None
let mkHist s =
let d = Dictionary<_, int>()
let add x = defaultArg (d.TryGetValueOpt x) 0 |> fun c -> d.[x] <- c + 1
Seq.iter add s
d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment