Sha256: 1cb5d1e3a4d34b9a945d8337f0b69f0c500bb643912a20246a962fe53139e5ed

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module Monad
  module Maybe
    class List
      include Enumerable
  
      def initialize(enum)
        @enum = enum.map { |v| v.maybe? ? v : v.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.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

2 entries across 2 versions & 1 rubygems

Version Path
monad-maybe-0.8.1 lib/monad/maybe/list.rb
monad-maybe-0.8.0 lib/monad/maybe/list.rb