LoginSignup
0
0

More than 5 years have passed since last update.

Sortしたらtupleが消える!?

Last updated at Posted at 2014-07-19

最近こんなことが…

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をそのまま使うことはできない。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0