Sha256: 8a532ee4b5459792a8eb8588fc92ed53f91207087cbfd2748fca72dd6d64f004

Contents?: true

Size: 874 Bytes

Versions: 10

Compression:

Stored size: 874 Bytes

Contents

# pattern_matching.fy
# Examples of pattern matching facilities in fancy

class PatternMatching {
  def match_it: obj {
    match obj {
      case String -> "It's a String!" println
      case Fixnum -> "It's a Number!" println
      case _ -> "Aything else!" println
    }
  }

  def match_with_extract: str {
    match str {
      # m holds the MatchData object, m1 & m2 the first and second matches
      case /^(.*) : (.*)$/ -> |m, m1, m2|
        "First match: #{m1}" println
        "Second match: #{m2}" println
    }
  }
}

pm = PatternMatching new
pm match_it: "foo"
pm match_it: 42
pm match_it: 'foo

pm match_with_extract: "Hello : World!"

# more pattern matching:

def do_it: num {
  (num, num * num)
}

match do_it: 10 {
  case Tuple -> |_, x, y| # first arg is a Tuple MatchData object (not used here).
    x inspect println # 10
    y inspect println # 100
}

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fancy-0.10.0 examples/pattern_matching.fy
fancy-0.9.0 examples/pattern_matching.fy
fancy-0.8.0 examples/pattern_matching.fy
fancy-0.7.0 examples/pattern_matching.fy
fancy-0.6.0 examples/pattern_matching.fy
fancy-0.5.0 examples/pattern_matching.fy
fancy-0.4.0 examples/pattern_matching.fy
fancy-0.3.3 examples/pattern_matching.fy
fancy-0.3.2 examples/pattern_matching.fy
fancy-0.3.1 examples/pattern_matching.fy