Class: Hamster::LazyList
Overview
A ‘LazyList` takes a block that returns a `List`, i.e. an object that responds to `#head`, `#tail` and `#empty?`. The list is only realized (i.e. the block is only called) when one of these operations is performed.
By returning a ‘Cons` that in turn has a LazyList as its tail, one can construct infinite `List`s.
Constant Summary
Constants included from List
Instance Method Summary collapse
- #cached_size? ⇒ Boolean
- #empty? ⇒ Boolean
- #head ⇒ Object (also: #first)
-
#initialize(&block) ⇒ LazyList
constructor
A new instance of LazyList.
- #size ⇒ Object (also: #length)
- #tail ⇒ Object
Methods included from List
#<<, [], #add, #append, #at, #break, #chunk, #clear, #combination, #cycle, #delete, #delete_at, #drop, #drop_while, #dup, #each, #each_chunk, empty, #eql?, #fill, #flat_map, #flatten, from_enum, #group_by, #hash, #indices, #init, #inits, #insert, #inspect, #intersperse, #last, #map, #merge, #merge_by, #partition, #permutation, #pop, #pretty_print, #respond_to?, #reverse, #rotate, #sample, #select, #slice, #sort, #sort_by, #span, #split_at, #subsequences, #tails, #take, #take_while, #to_list, #transpose, #union, #uniq, #zip
Methods included from Enumerable
#<=>, #==, #compact, #each_index, #grep, #grep_v, #group_by, #inspect, #join, #partition, #pretty_print, #product, #reject, #sort_by, #sum, #to_set
Methods included from Enumerable
Constructor Details
#initialize(&block) ⇒ LazyList
Returns a new instance of LazyList.
1311 1312 1313 1314 1315 1316 |
# File 'lib/hamster/list.rb', line 1311 def initialize(&block) @head = block # doubles as storage for block while yet unrealized @tail = nil @atomic = Concurrent::AtomicReference.new(0) # haven't yet run block @size = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Hamster::List
Instance Method Details
#cached_size? ⇒ Boolean
1339 1340 1341 |
# File 'lib/hamster/list.rb', line 1339 def cached_size? @size != nil end |
#empty? ⇒ Boolean
1329 1330 1331 1332 |
# File 'lib/hamster/list.rb', line 1329 def empty? realize if @atomic.get != 2 @size == 0 end |
#head ⇒ Object Also known as: first
1318 1319 1320 1321 |
# File 'lib/hamster/list.rb', line 1318 def head realize if @atomic.get != 2 @head end |
#size ⇒ Object Also known as: length
1334 1335 1336 |
# File 'lib/hamster/list.rb', line 1334 def size @size ||= super end |
#tail ⇒ Object
1324 1325 1326 1327 |
# File 'lib/hamster/list.rb', line 1324 def tail realize if @atomic.get != 2 @tail end |