LoginSignup
0

More than 3 years have passed since last update.

.nameみたいレコードのプロパティがいきなり呼ばれてるのは匿名関数の変形でした

Last updated at Posted at 2020-03-24

急に出てくるとびっくりするシリーズ。
Elmにはそういう記法ほとんどないですが、そのうちの1つ。

例えば、
https://github.com/evancz/elm-sortable-table/blob/3ca42219552a862e5126e4390c28bd4683e6f9ee/examples/1-presidents.elm#L86
のような箇所。

これは下記を変形した形。

toId = (\person -> person.name)

上記はモジュール利用例だが、よく使うパターンとしては、List.mapでレコードから特定のプロパティを抜き出す時に使う。

type alias Person =
  { name: String
  , age: Int
  }

persons =
 [ Person "alice" 20
 , Person "bob" 30
 , Person "carol" 40
 ]

List.map .age persons
-- 結果は[20, 30, 40]
-- List.mapの第一引数である(a -> b)は(\person -> person.age)であり、
-- これが変形されて.ageで表現されている

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