Module: Hamster

Extended by:
Forwardable
Defined in:
lib/hamster/set.rb,
lib/hamster/hash.rb,
lib/hamster/list.rb,
lib/hamster/trie.rb,
lib/hamster/deque.rb,
lib/hamster/vector.rb,
lib/hamster/version.rb,
lib/hamster/immutable.rb,
lib/hamster/undefined.rb,
lib/hamster/sorted_set.rb,
lib/hamster/enumerable.rb,
lib/hamster/mutable_hash.rb,
lib/hamster/read_copy_update.rb,
lib/hamster/experimental/mutable_set.rb,
lib/hamster/experimental/mutable_queue.rb

Defined Under Namespace

Modules: EmptyList, Enumerable, List Classes: Deque, Hash, MutableHash, MutableQueue, MutableSet, Set, SortedSet, Vector

Constant Summary

EmptySet =

The canonical empty Set. Returned by Hamster.set and Set[] when invoked with no arguments; also returned by Set.empty. Prefer using this one rather than creating many empty sets using Set.new.

Hamster::Set.empty
EmptyHash =

The canonical empty Hash. Returned by Hamster.hash and Hash[] when invoked with no arguments; also returned by Hash.empty. Prefer using this one rather than creating many empty hashes using Hash.new.

Hamster::Hash.empty
EmptyDeque =

The canonical empty Deque. Returned by Hamster.deque and Deque[] when invoked with no arguments; also returned by Deque.empty. Prefer using this one rather than creating many empty deques using Deque.new.

Hamster::Deque.empty
EmptyVector =

The canonical empty Vector. Returned by Hamster.vector and Vector[] when invoked with no arguments; also returned by Vector.empty. Prefer using this one rather than creating many empty vectors using Vector.new.

Hamster::Vector.empty
VERSION =
"1.0.1-rc2"
EmptySortedSet =

The canonical empty SortedSet. Returned by Hamster.sorted_set and SortedSet[] when invoked with no arguments; also returned by SortedSet.empty. Prefer using this one rather than creating many empty sorted sets using SortedSet.new.

Hamster::SortedSet.empty

Class Method Summary collapse

Class Method Details

.deque(*items) ⇒ Object



6
7
8
# File 'lib/hamster/deque.rb', line 6

def self.deque(*items)
  items.empty? ? EmptyDeque : Deque.new(items)
end

.enumerate(enum) ⇒ List

Turn an Enumerator into a Hamster::List. The result is a lazy collection where the values are memoized as they are generated.

If your code uses multiple threads, you need to make sure that the returned lazy collection is realized on a single thread only. Otherwise, a FiberError will be raised. After the collection is realized, it can be used from other threads as well.

Examples:

def rg; loop { yield rand(100) }; end
Hamster.enumerate(to_enum(:rg)).take(10)


106
107
108
109
110
111
112
113
114
# File 'lib/hamster/list.rb', line 106

def enumerate(enum)
  LazyList.new do
    begin
      Cons.new(enum.next, enumerate(enum))
    rescue StopIteration
      EmptyList
    end
  end
end

.hash(pairs = nil, &block) ⇒ Object



10
11
12
# File 'lib/hamster/hash.rb', line 10

def self.hash(pairs = nil, &block)
  (pairs.nil? && block.nil?) ? EmptyHash : Hash.new(pairs, &block)
end

.interval(from, to) ⇒ List

Construct a list of consecutive integers.

Examples:

Hamster.interval(5,9)
# => [5, 6, 7, 8, 9]


49
50
51
52
# File 'lib/hamster/list.rb', line 49

def interval(from, to)
  return EmptyList if from > to
  interval_exclusive(from, to.next)
end

.iterate(item) {|The| ... } ⇒ List

Create an infinite list where each item is derived from the previous one, using the provided block

Examples:

Hamster.iterate(0) { |i| i.next }.take(5)
# => [0, 1, 2, 3, 4]

Yield Parameters:

  • The (Object)

    previous value

Yield Returns:

  • (Object)

    The next value



88
89
90
# File 'lib/hamster/list.rb', line 88

def iterate(item, &block)
  LazyList.new { Cons.new(item, iterate(yield(item), &block)) }
end

.list(*items) ⇒ List

Create a list containing the given items.

Examples:

list = Hamster.list(:a, :b, :c)
# => [:a, :b, :c]


22
23
24
# File 'lib/hamster/list.rb', line 22

def list(*items)
  items.to_list
end

.mutable_hash(pairs = {}, &block) ⇒ Object



6
7
8
# File 'lib/hamster/mutable_hash.rb', line 6

def self.mutable_hash(pairs = {}, &block)
  MutableHash.new(hash(pairs, &block))
end

.mutable_queue(*items) ⇒ Object



6
7
8
# File 'lib/hamster/experimental/mutable_queue.rb', line 6

def self.mutable_queue(*items)
  MutableQueue.new(deque(*items))
end

.mutable_set(*items) ⇒ Object



6
7
8
# File 'lib/hamster/experimental/mutable_set.rb', line 6

def self.mutable_set(*items)
  MutableSet.new(set(*items))
end

.repeat(item) ⇒ List

Create an infinite list repeating the same item indefinitely

Examples:

Hamster.repeat(:chunky).take(4)
=> [:chunky, :chunky, :chunky, :chunky]


62
63
64
# File 'lib/hamster/list.rb', line 62

def repeat(item)
  LazyList.new { Cons.new(item, repeat(item)) }
end

.replicate(number, item) ⇒ List

Create a list that contains a given item a fixed number of times

Examples:

Hamster.replicate(3).(:hamster)
#=> [:hamster, :hamster, :hamster]


73
74
75
# File 'lib/hamster/list.rb', line 73

def replicate(number, item)
  repeat(item).take(number)
end

.set(*items) ⇒ Object



9
10
11
# File 'lib/hamster/set.rb', line 9

def self.set(*items)
  items.empty? ? EmptySet : Set.new(items)
end

.sorted_set(*items, &block) ⇒ Object



6
7
8
# File 'lib/hamster/sorted_set.rb', line 6

def self.sorted_set(*items, &block)
  (items.empty? && block.nil?) ? EmptySortedSet : SortedSet.new(items, &block)
end

.stream(&block) ⇒ List

Create a lazy, infinite list.

The given block is called as necessary to return successive elements of the list.

Examples:

Hamster.stream { :hello }.take(3)
# => [:hello, :hello, :hello]


35
36
37
38
# File 'lib/hamster/list.rb', line 35

def stream(&block)
  return EmptyList unless block_given?
  LazyList.new { Cons.new(yield, stream(&block)) }
end

.vector(*items) ⇒ Object



6
7
8
# File 'lib/hamster/vector.rb', line 6

def self.vector(*items)
  items.empty? ? EmptyVector : Vector.new(items.freeze)
end