lib/open_classes/array.rb in tbpgr_utils-0.0.16 vs lib/open_classes/array.rb in tbpgr_utils-0.0.17

- old
+ new

@@ -62,20 +62,22 @@ ret end # Arrays loop together select. # + # together_select has aliases [:tselect, :together_find_all, :tfindall] + # # if you want to single Array return # firsts = [1, 2, 3, 4] # seconds = [4, 2, 3, 1] # ret = [firsts, seconds].together_select{|first, second|first == second} # print ret # => output [[2, 3], [2, 3]] # # if you want to multi Array return # firsts = [1, 2, 3, 4] # seconds = [4, 2, 3, 1] - # ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?} + # ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?]} # print ret # => output [[1, 3], [4, 2]] def together_select if_not_contain_array_rails_type_error ret = [] first.each_with_index do |i_v, i| @@ -84,12 +86,56 @@ ret = set_together_each_return_select(ret, each_ret, i) end ret end + # Arrays loop together reduce. + # + # together_reduce has aliases [:treduce, :together_inject, :tinject] + # + # if you want to single return + # firsts = [1, 2, 3, 4] + # seconds = [4, 2, 3, 1] + # ret = [firsts, seconds].together_reduce{|memo, first, second|memo + first + second} + # print ret # => output 20 + # + # if you want to single return with init value + # firsts = [1, 2, 3, 4] + # seconds = [4, 2, 3, 1] + # ret = [firsts, seconds].together_reduce(10){|memo, first, second|memo + first + second} + # print ret # => output 30 + # + # if you want to single return with init string value + # firsts = %w{a b c} + # seconds = %w{1 2 3} + # ret = [firsts, seconds].together_reduce('start-'){|memo, first, second|memo + first + second} + # print ret # => output 'start-a1b2c3' + # + # if you want to single return with init Array value + # firsts = [1, 2, 3, 4] + # seconds = [4, 2, 3, 1] + # ret = [firsts, seconds].together_reduce([]){|memo, first, second|memo << first + second} + # print ret # => output [5, 4, 6, 5] + # + # if you want to single return with init Hash value + # firsts = [1, 2, 3, 4] + # seconds = [4, 2, 3, 1] + # ret = [firsts, seconds].together_reduce({}){|memo, first, second|memo[first] = second;memo} + # print ret # => output {1=>4, 2=>2, 3=>3, 4=>1} + def together_reduce(init = nil) + if_not_contain_array_rails_type_error + memo = initial_memo init + first.each_with_index do |i_v, i| + eval_each_str = get_args_str_for_together i + memo = instance_eval "yield(memo, #{eval_each_str})" + end + memo + end + alias_methods [:together_collect, :tmap, :tcollect], :together_map alias_methods [:together_find_all, :tselect, :tfindall], :together_select + alias_methods [:together_inject, :treduce, :tinject], :together_reduce private def if_not_contain_array_rails_type_error each { |f|fail TypeError, "you have to use [Array1, Array2, ...] | #{f.class} is invalid" unless f.class == Array } @@ -123,9 +169,14 @@ each_ret = tmp_each_ret end size.times { |i|ret << [] } if index == 0 (0..(size - 1)).each { |i|ret[i] << self[i][index] if each_ret[i] } ret + end + + def initial_memo(init) + return init unless init.nil? + first.first.is_a?(Numeric) ? 0 : first.first.is_a?(String) ? '' : nil end def together_return_multi?(list) (list.class == Array && list.size == size).to_bool end