fork(1) download
  1. open System.Collections.Generic
  2.  
  3. type IDictionary<'TKey, 'TValue> with
  4. member this.GetOrAdd (key:'TKey, fun':'TKey -> 'TValue) =
  5. match this.ContainsKey key with
  6. | true -> this.Item key
  7. | false -> let val' = fun' key
  8. this.Add (key, val')
  9. val'
  10.  
  11. let dd =
  12. let d = Dictionary()
  13. d.Add (1, 2)
  14. d.Add (3, 4)
  15. d
  16.  
  17. printfn "%A" dd
  18. dd.GetOrAdd (5, (fun x -> 2 * x)) |> printfn "%A :: %d" dd
  19. dd.GetOrAdd (5, (fun x -> 9 * x)) |> printfn "%A :: %d" dd
  20.  
Success #stdin #stdout 0.22s 25360KB
stdin
Standard input is empty
stdout
seq [[1, 2]; [3, 4]]
seq [[1, 2]; [3, 4]; [5, 10]] :: 10
seq [[1, 2]; [3, 4]; [5, 10]] :: 10