Sha256: 4692c125c561a732edf6fd2cbd603fe15d4891a90208fc845af45c29d7783d5e

Contents?: true

Size: 1.95 KB

Versions: 115

Compression:

Stored size: 1.95 KB

Contents

# encoding: utf-8
require 'open_classes/object'
require 'open_classes/module'
require 'open_classes/array/together_helper'

# Array
class Array
  include TogetherHelper

  # 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

  def initial_memo(init)
    return init unless init.nil?
    first.first.is_a?(Numeric) ? 0 : first.first.is_a?(String) ? '' : nil
  end

  alias_methods [:together_inject, :treduce, :tinject], :together_reduce
end

Version data entries

115 entries across 115 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.151 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.150 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.149 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.148 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.147 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.146 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.145 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.144 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.143 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.142 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.141 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.140 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.139 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.138 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.137 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.136 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.135 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.134 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.133 lib/open_classes/array/together_reduce.rb
tbpgr_utils-0.0.132 lib/open_classes/array/together_reduce.rb