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/tuple.rb,
lib/hamster/stack.rb,
lib/hamster/queue.rb,
lib/hamster/sorter.rb,
lib/hamster/vector.rb,
lib/hamster/version.rb,
lib/hamster/immutable.rb,
lib/hamster/undefined.rb,
lib/hamster/enumerable.rb,
lib/hamster/core_ext/io.rb,
lib/hamster/mutable_hash.rb,
lib/hamster/read_copy_update.rb,
lib/hamster/core_ext/enumerator.rb,
lib/hamster/core_ext/enumerable.rb,
lib/hamster/experimental/mutable_set.rb,
lib/hamster/experimental/mutable_stack.rb,
lib/hamster/experimental/mutable_queue.rb

Defined Under Namespace

Modules: CoreExt, EmptyList, Enumerable, Immutable, List, ReadCopyUpdate, Undefined Classes: Hash, MutableHash, MutableQueue, MutableSet, MutableStack, Queue, Sequence, Set, Sorter, Stack, Stream, Trie, Tuple, Vector

Constant Summary

EmptySet =
Hamster::Set.new
EmptyHash =
Hamster::Hash.new
EmptyTrie =
Hamster::Trie.new(0)
EmptyStack =
Hamster::Stack.new
EmptyQueue =
Hamster::Queue.new
EmptyVector =
Hamster::Vector.new
VERSION =
"1.0.1-rc.1"

Class Method Summary (collapse)

Class Method Details

+ (Stream) enumerate(enum)

Turn an enumerator into a Hamster list

The result is a lazy collection where the values are memoized as they are generated.

Examples:

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


112
113
114
115
116
117
118
119
120
# File 'lib/hamster/list.rb', line 112

def enumerate(enum)
  Stream.new do
    begin
      Sequence.new(enum.next, enumerate(enum))
    rescue StopIteration
      EmptyList
    end
  end
end

+ (Object) hash(pairs = {}, &block)



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

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

+ (Hamster::List) interval(from, to)

Construct a list of consecutive integers

Examples:

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


56
57
58
59
# File 'lib/hamster/list.rb', line 56

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

+ (Object) iterate(item) {|The| ... }

Create an infinite list where each item is based on the previous one

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



95
96
97
# File 'lib/hamster/list.rb', line 95

def iterate(item, &block)
  Stream.new { Sequence.new(item, iterate(yield(item), &block)) }
end

+ (Hamster::List) list(*items)

Create a list containing the given items

Examples:

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


25
26
27
# File 'lib/hamster/list.rb', line 25

def list(*items)
  items.to_list
end

+ (Object) mutable_hash(pairs = {}, &block)



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

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

+ (Object) mutable_queue(*items)



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

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

+ (Object) mutable_set(*items)



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

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

+ (Object) mutable_stack(*items)



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

def self.mutable_stack(*items)
  MutableStack.new(stack(*items))
end

+ (Object) queue(*items)



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

def self.queue(*items)
  items.reduce(EmptyQueue) { |queue, item| queue.enqueue(item) }
end

+ (Object) repeat(item)

Create an infinite list repeating the same item indefinitely

Examples:

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


69
70
71
# File 'lib/hamster/list.rb', line 69

def repeat(item)
  Stream.new { Sequence.new(item, repeat(item)) }
end

+ (Object) replicate(number, item)

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

Examples:

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


80
81
82
# File 'lib/hamster/list.rb', line 80

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

+ (Object) set(*items)



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

def self.set(*items)
  items.reduce(EmptySet) { |set, item| set.add(item) }
end

+ (Object) stack(*items)



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

def self.stack(*items)
  items.reduce(EmptyStack) { |stack, item| stack.push(item) }
end

+ (Hamster::List) stream(&block)

Create a lazy, infinite list

The given block is repeatedly called to yield the elements of the list.

Examples:

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


40
41
42
43
# File 'lib/hamster/list.rb', line 40

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

+ (Object) vector(*items)



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

def self.vector(*items)
  items.reduce(EmptyVector) { |vector, item| vector.add(item) }
end