Class: Quick::Sampler::DSL::Fluidiom

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/quick/sampler/dsl/fluidiom.rb

Overview

A Quick::Sampler wrapper providing a fluid DSL that can be used in a sampler definition passed to Quick::Sampler.compile.

Instance Method Summary collapse

Constructor Details

#initialize(sampler) ⇒ Fluidiom

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

wraps a sampler into a Fluidiom instance so it has extra methods while inside the block passed to Quick::Sampler.compile



13
14
15
16
# File 'lib/quick/sampler/dsl/fluidiom.rb', line 13

def initialize sampler
  sampler = Base.new(sampler) unless sampler.is_a? Base
  super(sampler)
end

Instance Method Details

#map {|sample| ... } ⇒ Quick::Sampler

spawn a mapping sampler

The produced sampler will yield each original sample to the block and return block result instead.

Yield Parameters:

  • sample (Anything)

    the original sample

Yield Returns:

  • (Anything)

    the mapped sample to return

Returns:

  • (Quick::Sampler)

    a sampler that maps each original sample through the block



56
57
58
# File 'lib/quick/sampler/dsl/fluidiom.rb', line 56

def map &block
  spawn(unwrap.map(&block))
end

#spawn(sampler) ⇒ Quick::Sampler

Returns a new fluidiom-wrapped sampler.

Returns:



26
27
28
# File 'lib/quick/sampler/dsl/fluidiom.rb', line 26

def spawn sampler
  self.class.new(sampler)
end

#such_that(max_iterations: 1000) {|sample| ... } ⇒ Quick::Sampler

spawn a filtering sampler

Parameters:

  • max_iterations (Integer) (defaults to: 1000)

    try at most this many values before giving up

Yield Parameters:

  • sample (Anything)

    a sampled value to be tested

Yield Returns:

  • (Boolean)

    true to pass the value through

Returns:

  • (Quick::Sampler)

    a sampler that passes through only samples that satisfy the predicate given as block



41
42
43
# File 'lib/quick/sampler/dsl/fluidiom.rb', line 41

def such_that max_iterations: 1000, &predicate
  spawn(unwrap.take(max_iterations).select(&predicate))
end

#unwrapQuick::Sampler

Returns the unwrapped original sampler.

Returns:



20
21
22
# File 'lib/quick/sampler/dsl/fluidiom.rb', line 20

def unwrap
  __getobj__
end