Slime recipe
Slime recipe

Reputation: 2263

F# Adding value to map result in KeyNotFoundException

type bytesLookup = Map<byte,int list>
type lookupList = bytesLookup list

let maps:bytesLookup = Map.empty

let printArg arg = printfn(Printf.TextWriterFormat<unit>(arg))

let array1 = [|byte(0x02);byte(0xB1);byte(0xA3);byte(0x02);byte(0x18);byte(0x2F)|]

let InitializeNew(maps:bytesLookup,element,index) =
    maps.Add(element,List.empty<int>)(*KeyNotFoundException*)
    maps.[element]

let MapArray (arr:byte[],maps:bytesLookup ) =
   for i in 0..arr.Length do
       match maps.TryFind(arr.[i]) with
        | Some(e) -> i::e
        | None -> InitializeNew(maps,arr.[i],i)

MapArray(array1,maps);

printArg( maps.Count.ToString())

Exception

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at Microsoft.FSharp.Collections.MapTreeModule.find[TValue,a](IComparer1 comparer, TValue k, MapTree2 m) at Microsoft.FSharp.Collections.FSharpMap2.get_Item(TKey key) at FSI_0012.MapArray(Byte[] arr, FSharpMap2 maps) in Script1.fsx:line 16 at .$FSI_0012.main@() in Script1.fsx:line 20

In the function I'm trying to initialize a new element in the map with a list of int. I also try to push a new int value into the list at the same time.

What am I doing wrong?

Upvotes: 3

Views: 454

Answers (1)

TheInnerLight
TheInnerLight

Reputation: 12184

F# Map is an immutable data structure, the Add method doesn't modify the existing data structure, it returns a new Map with the additions you've requested.

Observe:

let ex1 = 
    let maps = Map.empty<byte, int list>
    maps.Add(1uy, [1]) // compiler warning here!
    maps.[1uy]

Two things about this code:

  1. It throws System.Collections.Generic.KeyNotFoundException when you run it

  2. It gives you a compiler warning that the line maps.Add... should have type unit but actually has type Map<byte,int list>. Don't ignore the warning!

Now try this:

let ex2 =
    let maps = Map.empty<byte, int list>
    let maps2 = maps.Add(1uy, [1])
    maps2.[1uy]

No warning. No exception. Code works as expected, returning the value [1].

Upvotes: 6

Related Questions