Skip to content

Instantly share code, notes, and snippets.

@makevoid
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save makevoid/4101f3584fc86acb8dfe to your computer and use it in GitHub Desktop.

Select an option

Save makevoid/4101f3584fc86acb8dfe to your computer and use it in GitHub Desktop.
# main method / fn
#
def combined_sorted_enum(enums)
out = []
enum_ended = nil
while true do
enums.sort_by! do |enum|
begin
enum.peek
rescue StopIteration
enum_ended = enum
-1
end
end
enums.delete enum_ended if enum_ended
break if enums.empty?
out << enums.first.next
end
out.to_enum
end
# test data
enum0 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34].to_enum # fib
enum1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].to_enum # 1..10
enum2 = [2].to_enum
# setup
enums = [enum0, enum1, enum2]
# usage
enum = combined_sorted_enum enums
# output
puts enum.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment