1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
class Object
def returning o; yield o; o end # k-combinator
end
module Enumerable
def count_of(&b); select(&b).size end
def max_of(&b); map(&b).max end
def min_of(&b); map(&b).min end
end
class Array
def uniq_by; inject({}) { |h, o| h[yield(o)] = o; h }.values end
end
class NilClass
def blank?; true end
end
module Enumerable
def map_with_index # sigh...
ret = []
each_with_index { |e, i| ret << yield(e, i) }
ret
end
def argfind
each { |e| x = yield(e); return x if x }
nil
end
def group_by
inject({}) do |groups, element|
(groups[yield(element)] ||= []) << element
groups
end
end if RUBY_VERSION < '1.9'
end
class Array
def first_duplicate
sa = sort
(1 .. sa.length).argfind { |i| (sa[i] == sa[i - 1]) && sa[i] }
end
def to_h
Hash[*flatten]
end
def flatten_one_level
inject([]) do |ret, e|
case e
when Array
ret + e
else
ret << e
end
end
end
end
|