View Problem

Retrieve a value from a map

Given a map pets {joe:cat,mary:turtle,bill:canary} print the pet for "joe" ("cat")
DiskEdit
python
print pets['joe']
ExpandDiskEdit
fsharp
if (Map.mem "joe" pets) then printfn "%s" (Map.find "joe" pets)
ExpandDiskEdit
fsharp
if (pets |> Map.exists (fun key _ -> key = "joe")) then printfn "%s" (Map.find "joe" pets)
ExpandDiskEdit
fsharp
let key = "joe"
match (pets |> Map.tryfind key) with
| Some(value) -> printfn "%s" value
| None -> printfn "Key %s not found" key
ExpandDiskEdit
fsharp
if pets.ContainsKey("joe") then printfn "%s" pets.["joe"]
ExpandDiskEdit
fsharp
if pets.ContainsKey("joe") then printfn "%s" (pets.["joe"] :?> string)

Submit a new solution for python or fsharp
There are 22 other solutions in additional languages (clojure, cpp, erlang, fantom ...)