Skip to content

Commit

Permalink
Code examples for List#merge, #merge_by
Browse files Browse the repository at this point in the history
  • Loading branch information
dubek committed Dec 31, 2014
1 parent 26cd76d commit 1e95c82
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/hamster/list.rb
Expand Up @@ -795,6 +795,12 @@ def indices(object = Undefined, i = 0, &block)
# determined to be "lowest" according to the comparator will be the first in
# the merged `List`.
#
# @example
# list_1 = Hamster::List[1, -3, -5]
# list_2 = Hamster::List[-2, 4, 6]
# Hamster::List[list_1, list_2].merge { |a,b| a.abs <=> b.abs }
# # => Hamster::List[1, -2, -3, 4, -5, 6]
#
# @return [List]
def merge(&comparator)
return merge_by unless block_given?
Expand All @@ -813,6 +819,12 @@ def merge(&comparator)
# list. Whichever nested list's `#head` has the "lowest" sort key (according to
# their natural order) will be the first in the merged `List`.
#
# @example
# list_1 = Hamster::List[1, -3, -5]
# list_2 = Hamster::List[-2, 4, 6]
# Hamster::List[list_1, list_2].merge_by { |x| x.abs }
# # => Hamster::List[1, -2, -3, 4, -5, 6]
#
# @return [List]
def merge_by(&transformer)
return merge_by { |item| item } unless block_given?
Expand Down

0 comments on commit 1e95c82

Please sign in to comment.