Sha256: b5cb1e18d6db8002d2d2866d0e3b5465b836241dfa17bf7764c9c05667519838

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 KB

Contents

module Finitio
  #
  # The Seq type generator allows capturing sequences of values. For example,
  # a (implicitely temporal) series of temperature measures could be written
  # as:
  #
  #     Measures = [Temperature]
  #
  # This class allows capturing those sequence types, e.g.:
  #
  #     Temperature = BuiltinType.new(Float)
  #     Measures    = SeqType.new(Temperature)
  #
  # An array of values is used as concrete representation for such sequences:
  #
  #     R(Measures) = Array[R(Temperature)] = Array[Float]
  #
  # Accordingly, the `dress` transformation function has the signature below.
  # It expects it's Alpha/Object argument to be a object responding to
  # `each` (with the ruby idiomatic semantics that such a `each` returns
  # an Enumerator when invoked without block).
  #
  #     dress :: Alpha  -> Measures      throws TypeError
  #     dress :: Object -> Array[Float]  throws TypeError
  #
  class SeqType < Type
    include CollectionType

    def representator
      [elm_type]
    end

    def include?(value)
      value.is_a?(::Array) and value.all?{|v| elm_type.include?(v) }
    end

    # Apply the element type's `dress` transformation to each element of
    # `value` (expected to respond to `each`). Return converted values in a
    # ruby Array.
    def dress(value, handler = DressHelper.new)
      handler.failed!(self, value) unless value.respond_to?(:each)

      array = []
      handler.iterate(value) do |elm, index|
        array << elm_type.dress(elm, handler)
      end
      array
    end

    def default_name
      "[#{elm_type.name}]"
    end

  end # class SeqType
end # module Finitio

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
finitio-0.12.0 lib/finitio/type/seq_type.rb
finitio-0.11.4 lib/finitio/type/seq_type.rb
finitio-0.11.3 lib/finitio/type/seq_type.rb
finitio-0.11.2 lib/finitio/type/seq_type.rb
finitio-0.11.1 lib/finitio/type/seq_type.rb
finitio-0.10.0 lib/finitio/type/seq_type.rb
finitio-0.9.1 lib/finitio/type/seq_type.rb
finitio-0.9.0 lib/finitio/type/seq_type.rb
finitio-0.8.0 lib/finitio/type/seq_type.rb