Class: Enumerator

Inherits:
Object show all
Defined in:
lib/raskell/f.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to_stream(enum) ⇒ Object



1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
# File 'lib/raskell/f.rb', line 1359

def self.to_stream(enum)
  enumerator_to_use = enum.clone.lazy
  enumerator_to_store = enumerator_to_use.clone

  next_fn = ->(state) {
    idx = state[0]
    enum_frozen = state[1]
    enum_next = state[2]

    next_state = [idx+1, enum_frozen, enum_next]
    next_tag = :yield
    begin
      begin
      next_item = enum_next.next
   
      rescue StopIteration => e
        next_tag = :done
      end
    rescue IOError => e
      next_state = [idx, enum_frozen, enum_frozen.clone.drop(i)]
      next_tag = :skip
    end
    ## this {@@type||= "enumerator" is a dirty hack - there has to be a better way to restore state after calling next_item}
    next_tag == :done  ?  [:done]  :  [next_tag] + (next_tag == :yield ? [next_item] : []) + [Stream.new(next_fn, next_state, {type: "enumerator"})]

  }
  Stream.new(next_fn, [0, enumerator_to_store, enumerator_to_use], {type: "enumerator"})
end

Instance Method Details

#to_streamObject



1388
1389
1390
# File 'lib/raskell/f.rb', line 1388

def to_stream
  self.class.to_stream(self)
end