require 'patternmatching/version' require 'patternmatching/pattern' module PatternMatching # The module is only for showing example docs. #== Using module # # If installed from rubygems # require "rubygems" # gem "patternmatching" # # # for use # require "patternmatching" # #== Structured data example # val = 200 # code = PatternMatching.build {plus(mul(100, 100), val)} # #== Partial func example # class Calc # extend PatternMatching # # func(:calcm).seems as {plus(:a, :b)} do # calcm(a) + calcm(b) # end # func(:calcm).seems as {mul(:a, :b)} do # calcm(a) * calcm(b) # end # func(:calcm).seems as {:value} do # value # end # end # # p Calc.new.calcm(code) # #== Another partial style method example # class CalcX # extend PatternMatching # # func(:calcx) do # seems as {plus(:a, :b)} do # calcx(a) + calcx(b) # end # seems as {mul(:a, :b)} do # calcx(a) * calcx(b) # end # end # func(:calcx).seems as {:value} do # value # end # end # # p CalcX.new.calcx(code) # #== Pattern matching example # include PatternMatching # # def calc(code) # make(code) { # seems as {plus(:a, :b)} do calc(a) + calc(b) end # seems as {mul(:a, :b)} do calc(a) * calc(b) end # seems something do code end # } # end # # p calc(code) # #== Enumerable matching example # is = build { exact([1,2,3,4,5]) } # make is do # seems as {exact([:a,:b, _!(:c)])} do # puts a.to_s + ", " + b.to_s + " and " + c.to_s # end # seems something do # puts "not matched" # end # end # #== Hash to Hash matching example # dict = build { {:name => "Taro", :age => 5} } # make dict do # seems as {{:name => :name}} do # puts "He is " + name # end # seems something do # puts "no name" # end # end # #== Hash to non-Hash object matching example # class Person # def initialize(name, age) # @name = name # @age = age # end # attr :name # attr :age # end # # make Person.new("Jiro", 3) do # seems as {{:name => :name}} do # puts "He is " + name # end # seems something do # puts "no name" # end # end # module EXAMPLES end end