Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is orddict:fetch/2 being crazy?

Tags:

erlang

At the Erlang shell:

> orddict:fetch(b, [{d, 2}, {a, 1}, {b,5}, {c,9}]).
** exception error: no function clause matching 
                    orddict:fetch(b,[{d,2},{a,1},{b,5},{c,9}])

but

> orddict:fetch(b, [{a, 1}, {b,5}, {c,9}]).
5

What am I missing here?

References: orddict:fetch/2

The orddict docs describe an orddict as orddict() = [{Key :: term(), Value :: term()}].

like image 256
nmichaels Avatar asked Nov 21 '11 18:11

nmichaels


1 Answers

The key to the solution to this problem is in the docs:

The list is ordered after the keys.

Use orddict:from_list/1 to convert from a regular list of {key, value} pairs.

> orddict:fetch(b, orddict:from_list([{d, 2}, {a, 1}, {b,5}, {c,9}])).
5
like image 194
nmichaels Avatar answered Oct 06 '22 00:10

nmichaels