Sha256: 94bcfbb1a70e25bc768210750803081c17297cc1e96251c4e0e2b783c98c7991

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 KB

Contents

module Pair

  def pair(first, second)
    Pair.new(first, second)
  end

  def from_map(a_map)
    Pair.from_map(a_map)
  end

  class Pair

    def initialize(first, second)
      @first = -> { first }
      @second = -> { second }
    end

    def first
      @first.call
    end

    alias key first

    def second
      @second.call
    end

    def first=(v)
     @first = -> { v }
    end
    alias key= first=

    def second=(v)
      @second = -> { v }
    end
    alias value= second=

    def each(&block)
      [first,second].each do |i|
        block.call(i)
      end
    end

    alias value second

    def to_map
      {first => second}
    end

    def self.from_map(a_map)
      sequence(a_map).map { |k, v| Pair.new(k, v) }
    end

    def to_s
      Type.responds_all(sequence(first, second), :to_s)
      {first.to_s => second.to_s}
    end

    def to_i
      Type.responds_all(sequence(first, second), :to_i)
      {first.to_i => second.to_i}
    end

    def to_f
      Type.responds_all(sequence(first, second), :to_f)
      {first.to_f => second.to_f}
    end

    def values
      sequence(first, second)
    end

  end


end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
totally_lazy-0.0.16 lib/pair.rb
totally_lazy-0.0.15 lib/pair.rb
totally_lazy-0.0.11 lib/pair.rb
totally_lazy-0.0.10 lib/pair.rb
totally_lazy-0.0.9 lib/pair.rb
totally_lazy-0.0.8 lib/pair.rb
totally_lazy-0.0.7 lib/pair.rb
totally_lazy-0.0.6 lib/pair.rb