最近こんなことが…
lists:ukeysort/2を使ったらtupleが消えることがある。
それはkeyが 同じ 場合だ。
例えば次のような場合である。
shell1
1> lists:ukeysort(1, [{same_key, value1}, {same_key, value2}]).
[{same_key, value1}]
2>
ただ、これには注意すべき点がある。
次のような場合だ。
shell2
1> lists:ukeysort(1, [{1, value1}, {1.0, value2}]).
[{1, value1}]
2>
!?
[{1, value1}, {1.0, value2}]
じゃないの!?と。
これは lists:ukeysort/2
の 同じ(comparing equal) が次の大小に従っているためだと考えられる。
The arguments may be of different data types. The following order is defined:
number < atom < reference < fun < port < pid < tuple < list < bit string
参考URL: Erlang -- Expressions # 8.11 Term Comparisons
ちなみに…
proplists
のドキュメントには次のような記述があった。
Two keys are considered equal if they match (=:=). In other words, numbers are compared literally rather than by value, so that, for instance, 1 and 1.0 are different keys.
proplists
では integer()
と float()
が区別されていることが明示されている。
なのでproplists
を比較するとき等にlists:ukeysort/2
をそのまま使うことはできない。