Skip to content

Instantly share code, notes, and snippets.

@divs1210
Last active December 15, 2018 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divs1210/e9390bc7f6394c9f903b96a053667229 to your computer and use it in GitHub Desktop.
Save divs1210/e9390bc7f6394c9f903b96a053667229 to your computer and use it in GitHub Desktop.
def ordered_infix(*nums_and_ops)
initial_val = nums_and_ops[0]
op_num_pairs = nums_and_ops.drop(1).each_slice(2).to_a
op_num_pairs.reduce(initial_val) do |accumulator, op_num_pair|
op, num = op_num_pair
accumulator.send(op, num)
end
end
ordered_infix 10, '/', 2, '-', 1, '*', 2
# => 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment