Deleted articles cannot be recovered. Draft of this article would be also deleted. Are you sure you want to delete this article?
class Person attr_accessor :name, :sex def initialize(name, sex) @name = name @sex = sex end end people = [Person.new("John", "M"), Person.new("Kid", "F")] p people # => [#<Person:0x007f8cf58aacd0 @name="John", @sex="M">, #<Person:0x007f8cf58aac58 @name="Kid", @sex="F">] p people.map { |person| person.name } # => ["John", "Kid"] p people.map { |person| person.sex } # => ["M", "F"] p people.select
PythonやHaskellやErlangにはリスト内包表記と呼ばれる リストの中で新たなリストを生成する構文があるよ 例えばRubyでリストの要素の値を倍にしたい場合は Array#mapを使うよね l = [*1..10] l.map { |i| i*2 } # => [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] これをErlangのリスト内包表記では以下のように書けるんだ L = lists:seq(1,10). [X*2 || X <- L]. % => [2,4,6,8,10,12,14,16,18,20] リストLからXを選び出しそれに2を掛けたものを返す つまり || の左辺には出力となる式を 右辺には限定子を書く X <- LはErlangではGeneratorと呼ぶらしいよ 次にリストから偶数だけを選んで それらを倍にしたい場合を考えるよ Ru
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く