Skip to content

Instantly share code, notes, and snippets.

@latkin latkin/code.fsx
Last active Aug 29, 2015

Embed
What would you like to do?
Seq.cache repro
open System.Collections.Generic
let someDataSource = seq{ 1 .. 100 }
let ordersForTask = Dictionary<int, seq<int>>()
let getOrdersForTask task =
match ordersForTask.TryGetValue task with
| true, orders -> orders
| false, _ ->
let orders =
someDataSource
|> Seq.filter (fun n -> printfn "filtering..."; n % 2 = 0)
|> Seq.cache
ordersForTask.Add(task, orders)
orders
(* trying it out
> getOrdersForTask 3 ;;
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
val it : seq<int> = seq [2; 4; 6; 8; ...]
> getOrdersForTask 5 ;;
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
val it : seq<int> = seq [2; 4; 6; 8; ...]
> getOrdersForTask 3 ;;
val it : seq<int> = seq [2; 4; 6; 8; ...]
> getOrdersForTask 3 ;;
val it : seq<int> = seq [2; 4; 6; 8; ...]
> getOrdersForTask 3 ;;
val it : seq<int> = seq [2; 4; 6; 8; ...]
> getOrdersForTask 7 ;;
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
val it : seq<int> = seq [2; 4; 6; 8; ...]
> getOrdersForTask 7 ;;
val it : seq<int> = seq [2; 4; 6; 8; ...]
> getOrdersForTask 7 |> Seq.length ;;
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
filtering...
val it : int = 50
> getOrdersForTask 7 |> Seq.length ;;
val it : int = 50
> getOrdersForTask 7 |> Seq.length ;;
val it : int = 50
> getOrdersForTask 7 |> Seq.length ;;
val it : int = 50
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.