-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchlist.tiny
51 lines (43 loc) · 1.46 KB
/
matchlist.tiny
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
######################################################
#
# Pattern matching using patterns as objects and the
# .matchlist() method
#
#######################################################
# A database of people and relationships
rels = [
['parent-of', 'Bob', 'Sue'],
['gender', 'Bob', 'Male'],
['parent-of', 'Bob', 'John'],
['gender', 'John', 'Male'],
['parent-of', 'Sue', 'Bill'],
['gender', 'Sue', 'FeMale'],
['gender', 'Bill', 'Male'],
['parent-of', 'Sue', 'Mary'],
['gender', 'Alice', 'FeMale'],
['gender', 'Mary', 'FeMale'],
['parent-of', 'John', 'george'],
['gender', 'George', 'Male'],
['parent-of', 'John', 'Alice'],
['parent', 'Male', 'Father'],
['parent', 'FeMale', 'Mother']
]
# Function to find the gender of a person
fn genderOf person ->
('gender' :: {it == person} :: gender :: _)
.matchlist(rels, { gender })
.first()
# Function to find a person's parents
fn parentsOf person ->
('parent-of' :: {it == person} :: parent :: _)
.matchlist(rels, { parent })
# Function to find a person's grandparents
fn grandParentsOf person ->
parentsOf( person )
.flatmap{ parentsOf(it) }
# Function to find a person't grandfather
fn grandFathersOf person ->
grandParentsOf( person )
.where{ genderOf(it) == 'Male' }
println("Bob's grandparents are " + grandParentsOf('Bob'))
println("Bob's grandfathers are " + grandFathersOf('Bob'))