Sha256: 367fdbf7758ac5e97fd5a5bf8abee662d3c604f24d285f65cc8ecd25e94903d3

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module Monad
  module Maybe
    class List
      include Enumerable
  
      def initialize(enum)
        @enum = enum.map { |v| v.maybe? ? v : v.to_maybe }
      end
  
      def inspect
        "#{to_a}"
      end
  
      def maybe?
        true
      end

      def <<(obj)
        @enum << obj if obj.just?
        self
      end
  
      def to_a
        @enum.to_a
      end
  
      def to_maybe
        first.to_maybe
      end
  
      def each
        @enum.each do |x|
          yield(x)
        end
      end
  
      def map
        e = []
        each do |x|
          e << yield(x)
        end
        List.new(e)
      end
      alias maybe_map map
  
      def select
        e = []
        each do |x|
          is_true = yield(x)
          e << x if is_true  
        end
        List.new(e)
      end
  
      def reject
        select { |x| !yield(x) }
      end
  
      def select_just
        select { |x| x.just? }
      end
  
      def unwrap_map(default, &blk)
        to_a.map { |x| blk ? blk.call(x.unwrap(default)) : x.unwrap(default) }
      end
  
      def value_map(&blk)
        to_a.map { |x| blk ? blk.call(x.value) : x.value }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
monad-maybe-0.9.9 lib/monad/maybe/list.rb