Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
F# script file of refactoring a curried function into a higher order function.
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