Skip to content

Instantly share code, notes, and snippets.

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 shanecharles/7b65d3182378c0924cbaa4d4c81b0454 to your computer and use it in GitHub Desktop.
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.
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