<commit_before>#!/usr/bin/env python
"""
Loose port of the examples at `A Tour of Scala: Case Classes <http://www.scala-lang.org/node/107>`_
"""
from __future__ import print_function

from pyfpm import MatchFunction, handler
from pyfpm import Case

class Term(Case): pass
class Var(Term):
    def __init__(self, name):
        self.name = name
class Fun(Term):
    def __init__(self, arg, body):
        self.arg = arg
        self.body = body
class App(Term):
    def __init__(self, f, v):
        self.f = f
        self.v = v

print('-'*80)
example = Fun('x', Fun('y', App(Var('x'), Var('y'))))
print(example)

print('-'*80)
x = Var('x')
print(x.name)

print('-'*80)
class printTerm(MatchFunction):
    @handler('Var(n:str)')
    def var(n):
        print(n, end='')

    @handler('Fun(x:str, b:Term)')
    def fun(x, b):
        print('^' + x + '.', end='')
        printTerm(b)

    @handler('App(f:Term, v:Term)')
    def app(f, v):
        print('(', end='')
        printTerm(f)
        print(' ', end='')
        printTerm(v)
        print(')', end='')

class isIdentityFun(MatchFunction):
    @handler('Fun(x, Var(y))')
    def identity(x, y):
        return x == y

    @handler('_')
    def other():
        return False

id = Fun('x', Var('x'))
t = Fun('x', Fun('y', App(Var('x'), Var('y'))))
printTerm(t)
print()
print(isIdentityFun(id))
print(isIdentityFun(t))<commit_msg>updated example with conditional pattern syntax<commit_after>#!/usr/bin/env python
"""
Loose port of the examples at `A Tour of Scala: Case Classes <http://www.scala-lang.org/node/107>`_
"""
from __future__ import print_function

from pyfpm import MatchFunction, handler
from pyfpm import Case

class Term(Case): pass
class Var(Term):
    def __init__(self, name):
        self.name = name
class Fun(Term):
    def __init__(self, arg, body):
        self.arg = arg
        self.body = body
class App(Term):
    def __init__(self, f, v):
        self.f = f
        self.v = v

print('-'*80)
example = Fun('x', Fun('y', App(Var('x'), Var('y'))))
print(example)

print('-'*80)
x = Var('x')
print(x.name)

print('-'*80)
class printTerm(MatchFunction):
    @handler('Var(n:str)')
    def var(n):
        print(n, end='')

    @handler('Fun(x:str, b:Term)')
    def fun(x, b):
        print('^' + x + '.', end='')
        printTerm(b)

    @handler('App(f:Term, v:Term)')
    def app(f, v):
        print('(', end='')
        printTerm(f)
        print(' ', end='')
        printTerm(v)
        print(')', end='')

class isIdentityFun(MatchFunction):
    @handler('Fun(x, Var(y)) if x==y')
    def identity(x, y):
        return True

    @handler('_')
    def other():
        return False

id = Fun('x', Var('x'))
t = Fun('x', Fun('y', App(Var('x'), Var('y'))))
printTerm(t)
print()
print(isIdentityFun(id))
print(isIdentityFun(t))
{'fraction_non_alphanumeric': 0.19829424307036247, 'fraction_numerical': 0.006396588486140725, 'mean_word_length': 3.0679190751445087, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1336311', 'n_tokens_mistral': 1037, 'n_tokens_neox': 1011, 'n_words': 257}