Skip to content

Instantly share code, notes, and snippets.

@wklm
Last active September 24, 2017 16:05
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 wklm/d66b7594271bfd23cafb9b1ad0a3dfc8 to your computer and use it in GitHub Desktop.
Save wklm/d66b7594271bfd23cafb9b1ad0a3dfc8 to your computer and use it in GitHub Desktop.
fibonacci async
let rec fib = function
| 0L | 1L as n -> n
| n -> (fib (n - 1L) + fib (n - 2L))
let memoize f =
let memo = ref Map.empty
fun arg ->
if Map.containsKey arg !memo then Map.find arg !memo
else
let result = f arg
memo := Map.add arg result !memo
result
let rec fibm =
memoize <| fun n ->
match n with
| 0L | 1L as n -> n
| n -> fibm (n - 1L) + fibm (n - 2L)
let go (f : int64 -> int64) (range : int64) =
seq { for i in [0L..range] do yield async {return f i} }
|> Async.Parallel
|> Async.RunSynchronously
[<EntryPoint>]
let main argv =
let res = match argv.[0] |> string with
| "fib" -> fib (int64 argv.[1])
| "fibm" -> fibm (int64 argv.[1])
| _ -> -1L
printfn "%i" res
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment