Subscribed unsubscribe Subscribe Subscribe

F# Monkey

about

ConcurrentDictionaryの使い方

F#

こんな感じ

open System.Collections.Generic
open System.Collections.Concurrent

let dic : ConcurrentDictionary<string,int> = new ConcurrentDictionary< string, int >()

// appleははじめてなので 100
dic.AddOrUpdate("apple",100, fun _ price -> price + 99 )
dic.Values |> printfn "%A" // seq [100]

// appleは2回目のなので 100 + 99
dic.AddOrUpdate("apple",100, fun _ price -> price + 99 )
dic.Values |> printfn "%A" // seq [199]

// appleはすでに登録されているので 199
dic.GetOrAdd("apple",999)
|> printfn "%A" // 199

// bananaははじめてなので 9999
dic.GetOrAdd("banana",9999)
|> printfn "%A" // 9999

// cherryははじめてなので 99999
dic.GetOrAdd( "cherry", fun _ -> 99999)
|> printfn "%A" // 99999


dic.Keys |> printfn "%A"
// seq ["banana"; "cherry"; "apple"]

dic.Values |> printfn "%A"
// seq [9999; 99999; 199]

dic.Item("cherry") |> printfn "%A" // 99999

dic.ContainsKey("apple") |> printfn "%A" // true

dic.Count |> printfn "%A" // 3

dic.ToArray() |> printfn "%A"
// [|[banana, 9999]; [cherry, 99999]; [apple, 199]|]

dic |> printfn "%A"
// seq [[banana, 9999]; [cherry, 99999]; [apple, 199]]

Remove all ads