Created
July 23, 2017 13:26
-
-
Save shanecharles/7b65d3182378c0924cbaa4d4c81b0454 to your computer and use it in GitHub Desktop.
F# script file of refactoring a curried function into a higher order function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let datapoints = [1 .. 400000] | |
let keys = [1; -4; 45000; 2501; 56000] | |
// Enable some evaluation statistics. Execute #time again to disable. | |
#time | |
let singleKeyLookup fn = fn datapoints 67 |> ignore | |
let multiKeyLookup (fn : int seq -> int -> bool) = | |
keys |> List.map (fn datapoints) | |
// Default currying and partial application. | |
let seekKey (keys : seq<int>) (key : int) : bool = | |
printfn "Creating index" | |
let index = Set.ofSeq keys | |
printfn "Seeking key: %d" key | |
index |> Seq.contains key | |
singleKeyLookup seekKey | |
multiKeyLookup seekKey | |
let seek = seekKey datapoints | |
// Explicitly returning a function | |
let seekKey' (keys : seq<int>) : int -> bool = | |
printfn "Creating index" | |
let index = Set.ofSeq keys | |
(fun key -> | |
printfn "Seeking key: %d" key | |
index |> Seq.contains key) | |
singleKeyLookup seekKey' | |
multiKeyLookup seekKey' | |
let seek' = seekKey' datapoints | |
// Adding a lazy computation for index creation | |
let seekKey'' (keys : seq<int>) : int -> bool = | |
let index = lazy ( | |
printfn "Creating index" | |
Set.ofSeq keys) | |
(fun key -> | |
printfn "Seeking key: %d" key | |
index.Value |> Seq.contains key) | |
singleKeyLookup seekKey'' | |
multiKeyLookup seekKey'' | |
let seek'' = seekKey'' datapoints |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment