Sha256: e647c17cecd6fe04954ebd342ce726069462b3b4c8cee40e9957dfef502f8490

Contents?: true

Size: 785 Bytes

Versions: 1

Compression:

Stored size: 785 Bytes

Contents

require 'rumonade/monad'

module Rumonade
  # TODO: Document use of Array as a Monad
  module ArrayExtensions
    module ClassMethods
      def unit(value)
        [value]
      end

      def empty
        []
      end
    end

    module InstanceMethods
      # Preserve native +map+ method for performance
      METHODS_TO_REPLACE_WITH_MONAD = Monad::DEFAULT_METHODS_TO_REPLACE_WITH_MONAD - [:map]

      def bind(lam = nil, &blk)
        inject(self.class.empty) do |arr, elt|
          res = (lam || blk).call(elt)
          arr + (res.respond_to?(:to_a) ? res.to_a : [res])
        end
      end
    end
  end
end

Array.send(:extend, Rumonade::ArrayExtensions::ClassMethods)
Array.send(:include, Rumonade::ArrayExtensions::InstanceMethods)
Array.send(:include, Rumonade::Monad)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rumonade-0.4.1 lib/rumonade/array.rb