Module: Deterministic

Defined in:
lib/deterministic/match.rb,
lib/deterministic.rb,
lib/deterministic/enum.rb,
lib/deterministic/monad.rb,
lib/deterministic/either.rb,
lib/deterministic/option.rb,
lib/deterministic/result.rb,
lib/deterministic/result.rb,
lib/deterministic/version.rb,
lib/deterministic/protocol.rb,
lib/deterministic/core_ext/result.rb

Overview

TODO: remove dead code

Defined Under Namespace

Modules: CoreExt, Enum, Monad, PatternMatching, Prelude, Protocol Classes: Either, EnumBuilder, InstanceBuilder, Option, ProtocolBuilder, Result

Constant Summary collapse

VERSION =
"0.16.0"

Class Method Summary collapse

Class Method Details

.enum(&block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/deterministic/enum.rb', line 112

def enum(&block)
  mod = Class.new do # the enum to be built
    class << self; private :new; end

    def self.match(obj, &block)
      caller_ctx = block.binding.eval 'self'

      matcher = self::Matcher.new(obj)
      matcher.instance_eval(&block)

      variants_in_match = matcher.matches.collect {|e| e[1].name.split('::')[-1].to_sym}.uniq.sort
      variants_not_covered = variants - variants_in_match
      raise Enum::MatchError, "Match is non-exhaustive, #{variants_not_covered} not covered" unless variants_not_covered.empty?

      type_matches = matcher.matches.select { |r| r[0].is_a?(r[1]) }

      type_matches.each { |match|
        obj, type, block, args, guard = match

        if args.count == 0
          return caller_ctx.instance_eval(&block)
        else
          if args.count != obj.args.count
            raise Enum::MatchError, "Pattern (#{args.join(', ')}) must match (#{obj.args.join(', ')})"
          end
          guard_ctx = guard_context(obj, args)

          if guard
            if guard_ctx.instance_exec(obj, &guard)
              return caller_ctx.instance_exec(* obj.wrapped_values, &block)
            end
          else
            return caller_ctx.instance_exec(* obj.wrapped_values, &block)
          end
        end
      }

      raise Enum::MatchError, "No match could be made"
    end

    def self.variants; constants - [:Matcher, :MatchError]; end

    private
    def self.guard_context(obj, args)
      if obj.is_a?(Deterministic::EnumBuilder::DataType::Binary)
        Struct.new(*(args)).new(*(obj.value.values))
      else
        Struct.new(*(args)).new(obj.value)
      end
    end
  end
  enum = EnumBuilder.new(mod)
  enum.instance_eval(&block)

  type_variants = mod.constants

  matcher = Class.new {
    def initialize(obj)
      @obj = obj
      @matches = []
      @vars = []
    end

    attr_reader :matches, :vars

    def where(&guard)
      guard
    end

    type_variants.each { |m|
      define_method(m) { |guard = nil, &block|
        raise ArgumentError, "No block given to `#{m}`" if block.nil?
        params_spec = block.parameters
        if params_spec.any? {|spec| spec.size < 2 }
          raise ArgumentError, "Unnamed param found in block parameters: #{params_spec.inspect}"
        end
        if params_spec.any? {|spec| spec[0] != :req && spec[0] != :opt }
          raise ArgumentError, "Only :req & :opt params allowed; parameters=#{params_spec.inspect}"
        end
        args = params_spec.map {|spec| spec[1] }

        type = Kernel.eval("#{mod.name}::#{m}")

        if guard && !guard.is_a?(Proc)
          guard = nil
        end

        @matches << [@obj, type, block, args, guard]
      }
    }
  }

  mod.const_set(:Matcher, matcher)

  type_variants.each { |variant|
    mod.singleton_class.class_exec {
      define_method(variant) { |*args|
        const_get(variant).new(*args)
      }
    }
  }
  mod
end

.impl(enum_type, &block) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/deterministic/enum.rb', line 216

def impl(enum_type, &block)
  enum_type.variants.each { |v|
    name = "#{enum_type.name}::#{v.to_s}"
    type = Kernel.eval(name)
    type.class_eval(&block)
  }
end

.instance(protocol, type, &block) ⇒ Object



94
95
96
# File 'lib/deterministic/protocol.rb', line 94

def instance(protocol, type, &block)
  InstanceBuilder.new(protocol, type, block).build
end

.Left(value) ⇒ Object



28
29
30
# File 'lib/deterministic/either.rb', line 28

def Left(value)
  Either.new(Array[value], [])
end

.protocol(typevar, &block) ⇒ Object



88
89
90
91
92
# File 'lib/deterministic/protocol.rb', line 88

def protocol(typevar, &block)
  protocol = ProtocolBuilder.new(typevar, block).build
  p_module = block.binding.eval('self')
  p_module.const_set(:Protocol, protocol)
end

.Right(value) ⇒ Object



32
33
34
# File 'lib/deterministic/either.rb', line 32

def Right(value)
  Either.new([], Array[value])
end