Sha256: 45db4e2e0bb2f255699982189a5e58a834ac4954e8a67858774b1198c837f738

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require 'rumonade/monad'

module Rumonade
  # TODO: Document use of Hash as a Monad
  module HashExtensions
    module ClassMethods
      def unit(value)
        raise ArgumentError, "argument not a 2-element Array for Hash.unit" unless (value.is_a?(Array) && value.size == 2)
        Hash[*value]
      end

      def empty
        {}
      end
    end

    module InstanceMethods
      # Preserve native +map+ and +flatten+ methods for compatibility
      METHODS_TO_REPLACE_WITH_MONAD = Monad::DEFAULT_METHODS_TO_REPLACE_WITH_MONAD - [:map, :flatten]

      def bind(lam = nil, &blk)
        inject(self.class.empty) { |hsh, elt| hsh.merge((lam || blk).call(elt)) }
      end

      # @return [Option] a Some containing the value associated with +key+, or None if not present
      def get(key)
        Option(self[key])
      end
    end

    module InstanceOverrides
      def can_flatten_in_monad?
        false
      end
    end
  end
end

Hash.send(:extend, Rumonade::HashExtensions::ClassMethods)
Hash.send(:include, Rumonade::HashExtensions::InstanceMethods)
Hash.send(:include, Rumonade::Monad)
Hash.send(:include, Rumonade::HashExtensions::InstanceOverrides)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rumonade-0.4.4 lib/rumonade/hash.rb
rumonade-0.4.3 lib/rumonade/hash.rb