Sha256: 8afb32f879d200530f47237e5532c02c1e185b45e384928c180fceb7358d5040

Contents?: true

Size: 709 Bytes

Versions: 3

Compression:

Stored size: 709 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) { |arr, elt| arr + (lam || blk).call(elt).to_a }
      end
    end
  end
end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rumonade-0.4.3 lib/rumonade/array.rb
rumonade-0.4.2 lib/rumonade/array.rb
rumonade-0.4.0 lib/rumonade/array.rb