Class: Immutable::Queue
- Inherits:
-
Object
- Object
- Immutable::Queue
- Includes:
- Headable
- Defined in:
- lib/immutable/queue.rb
Overview
Immutable::Queue is an implementation of real-time queues described in "Purely Functional Data Structures" by Chris Okasaki.
Direct Known Subclasses
Class Method Summary (collapse)
-
+ (Queue) [](*elements)
Creates a new queue populated with the given objects.
-
+ (Queue) empty
Returns an empty queue.
Instance Method Summary (collapse)
-
- (true, false) empty?
Returns whether self is empty.
-
- (Object) head
Returns the first element of self.
-
- (Queue) initialize(front, rear, schedule)
constructor
Queue.new is for internal use only.
-
- (Queue) snoc(x)
(also: #push)
Adds a new element at the end of self.
-
- (Queue) tail
Returns the elements after the head of self.
Methods included from Headable
#==, #[], #each, #each_index, #eql?, #fetch, #find, #first, #foldl, #foldl1, #foldr, #foldr1, #hash, #index, #inspect, #null?, #rindex, #shift, #to_list
Methods included from Foldable
#foldl, #length, #product, #sum
Constructor Details
Class Method Details
+ (Queue) [](*elements)
Creates a new queue populated with the given objects.
28 29 30 |
# File 'lib/immutable/queue.rb', line 28 def self.[](*elements) elements.inject(empty, &:snoc) end |
+ (Queue) empty
Returns an empty queue.
20 21 22 |
# File 'lib/immutable/queue.rb', line 20 def self.empty new(Stream.empty, Nil, Stream.empty) end |
Instance Method Details
- (true, false) empty?
Returns whether self is empty.
35 36 37 |
# File 'lib/immutable/queue.rb', line 35 def empty? @front.empty? end |
- (Object) head
Returns the first element of self. If self is empty, Immutable::EmptyError is raised.
53 54 55 |
# File 'lib/immutable/queue.rb', line 53 def head @front.head end |
- (Queue) snoc(x) Also known as: push
Adds a new element at the end of self.
43 44 45 |
# File 'lib/immutable/queue.rb', line 43 def snoc(x) queue(@front, Cons[x, @rear], @schedule) end |
- (Queue) tail
Returns the elements after the head of self. If self is empty, Immutable::EmptyError is raised.
61 62 63 64 65 66 67 |
# File 'lib/immutable/queue.rb', line 61 def tail if @front.empty? raise EmptyError else queue(@front.tail, @rear, @schedule) end end |