ライブラリ
- ビルトイン (223)
キーワード
- chunk (11)
- compact (3)
-
each
_ entry (22) -
each
_ with _ index (22) - lazy (11)
- max (44)
-
max
_ by (44) - min (44)
-
sort
_ by (22)
検索結果
先頭5件
-
Enumerable
# sort _ by -> Enumerator (25.0) -
ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。
...mlist[例][ruby]{
class Array
def sort_by
self.map {|i| [yield(i), i] }.
sort {|a, b| a[0] <=> b[0] }.
map {|i| i[1]}
end
end
//}
Enumerable#sort と比較して sort_by が優れている点として、
比較条件が複雑な場合の速度が挙げられます。
sort_by......y]{
class Integer
def count
$n += 1
self
end
end
ary = []
1.upto(1000) {|v| ary << rand(v) }
$n = 0
ary.sort {|a,b| a.count <=> b.count }
p $n # => 18200
$n = 0
ary.sort_by {|v| v.count }
p $n # => 1000
//}
Enumerable#sort_by は安定ではありません (unstable......できます。
//emlist[][ruby]{
i = 0
ary.sort_by {|v| [v, i += 1] }
//}
※ 比較結果が同じ要素は元の順序通りに並ぶソートを
「安定なソート (stable sort)」と言います。
ブロックを省略した場合は Enumerator を返します。
@see Enumerable#sort... -
Enumerable
# sort _ by {|item| . . . } -> [object] (25.0) -
ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。
...mlist[例][ruby]{
class Array
def sort_by
self.map {|i| [yield(i), i] }.
sort {|a, b| a[0] <=> b[0] }.
map {|i| i[1]}
end
end
//}
Enumerable#sort と比較して sort_by が優れている点として、
比較条件が複雑な場合の速度が挙げられます。
sort_by......y]{
class Integer
def count
$n += 1
self
end
end
ary = []
1.upto(1000) {|v| ary << rand(v) }
$n = 0
ary.sort {|a,b| a.count <=> b.count }
p $n # => 18200
$n = 0
ary.sort_by {|v| v.count }
p $n # => 1000
//}
Enumerable#sort_by は安定ではありません (unstable......できます。
//emlist[][ruby]{
i = 0
ary.sort_by {|v| [v, i += 1] }
//}
※ 比較結果が同じ要素は元の順序通りに並ぶソートを
「安定なソート (stable sort)」と言います。
ブロックを省略した場合は Enumerator を返します。
@see Enumerable#sort... -
Enumerable
# each _ entry -> Enumerator (13.0) -
ブロックを各要素に一度ずつ適用します。
...配列として渡されます。
//emlist[例][ruby]{
class Foo
include Enumerable
def each
yield 1
yield 1,2
end
end
Foo.new.each_entry{|o| print o, " -- "}
# => 1 -- [1, 2] --
//}
ブロックを省略した場合は Enumerator が返されます。
@see Enumerable#slice_before... -
Enumerable
# each _ entry {|obj| block} -> self (13.0) -
ブロックを各要素に一度ずつ適用します。
...配列として渡されます。
//emlist[例][ruby]{
class Foo
include Enumerable
def each
yield 1
yield 1,2
end
end
Foo.new.each_entry{|o| print o, " -- "}
# => 1 -- [1, 2] --
//}
ブロックを省略した場合は Enumerator が返されます。
@see Enumerable#slice_before... -
Enumerable
# each _ with _ index(*args) -> Enumerator (13.0) -
要素とそのインデックスをブロックに渡して繰り返します。
...][ruby]{
[5, 10, 15].each_with_index do |n, idx|
p [n, idx]
end
# => [5, 0]
# [10, 1]
# [15, 2]
//}
//emlist[引数ありの例][ruby]{
require 'stringio'
StringIO.new("foo|bar|baz").each_with_index("|") do |s, i|
p [s, i]
end
# => ["foo|", 0]
# ["bar|", 1]
# ["baz", 2]
//}
@see En... -
Enumerable
# each _ with _ index(*args) {|item , index| . . . } -> self (13.0) -
要素とそのインデックスをブロックに渡して繰り返します。
...][ruby]{
[5, 10, 15].each_with_index do |n, idx|
p [n, idx]
end
# => [5, 0]
# [10, 1]
# [15, 2]
//}
//emlist[引数ありの例][ruby]{
require 'stringio'
StringIO.new("foo|bar|baz").each_with_index("|") do |s, i|
p [s, i]
end
# => ["foo|", 0]
# ["bar|", 1]
# ["baz", 2]
//}
@see En... -
Enumerable
# max {|a , b| . . . } -> object | nil (13.0) -
ブロックの評価結果で各要素の大小判定を行い、最大の要素、もしくは最大の n 要素が入った降順の配列を返します。 引数を指定しない形式では要素が存在しなければ nil を返します。 引数を指定する形式では、空の配列を返します。
...に発生します。
//emlist[例][ruby]{
class Person
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
people = [
Person.new("sato", 55),
Person.new("sato", 33),
Person.new("sato", 11),
Person.new("suzuki", 55),
Person.new("suzuki", 33),... -
Enumerable
# max(n) {|a , b| . . . } -> Array (13.0) -
ブロックの評価結果で各要素の大小判定を行い、最大の要素、もしくは最大の n 要素が入った降順の配列を返します。 引数を指定しない形式では要素が存在しなければ nil を返します。 引数を指定する形式では、空の配列を返します。
...に発生します。
//emlist[例][ruby]{
class Person
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
people = [
Person.new("sato", 55),
Person.new("sato", 33),
Person.new("sato", 11),
Person.new("suzuki", 55),
Person.new("suzuki", 33),... -
Enumerable
# max _ by -> Enumerator (13.0) -
各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。
...を返します。
該当する要素が複数存在する場合、どの要素を返すかは不定です。
Enumerable#max と Enumerable#max_by の
違いは Enumerable#sort と Enumerable#sort_by の違いと同じです。
ブロックを省略した場合は Enumerator を返します。
@p......n)は、重み付きランダムサンプリングを実装するために使用できます。次の実装例は、Enumerable#wsampleを使用します。][ruby]{
module Enumerable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampling with a res......rvoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n) {|v| rand ** (1.0/yield(v)) }
end
end
e = (-20..20).to_a*10000
a = e.wsample(20000) {|x|
Math.exp(-(x/5.0)**2) # normal distribution
}
# a is 20000 samples from e.
p a.length #=> 2... -
Enumerable
# max _ by {|item| . . . } -> object | nil (13.0) -
各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。
...を返します。
該当する要素が複数存在する場合、どの要素を返すかは不定です。
Enumerable#max と Enumerable#max_by の
違いは Enumerable#sort と Enumerable#sort_by の違いと同じです。
ブロックを省略した場合は Enumerator を返します。
@p......n)は、重み付きランダムサンプリングを実装するために使用できます。次の実装例は、Enumerable#wsampleを使用します。][ruby]{
module Enumerable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampling with a res......rvoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n) {|v| rand ** (1.0/yield(v)) }
end
end
e = (-20..20).to_a*10000
a = e.wsample(20000) {|x|
Math.exp(-(x/5.0)**2) # normal distribution
}
# a is 20000 samples from e.
p a.length #=> 2...