Commits

Felix Geller committed 4e6e707

Small example for an object that can match multiple patterns.

Comments (0)

Files changed (1)

PatternMatchingTests.ns3

 TEST_CONTEXT = (
 	
 ))
+class ListExampleTests = TestContext (
+"
+Contains small tests for list. Can demonstrate that an object might want to match multiple patterns."|
+|)
+(
+class Cons of: a and: d = (|	car = a.
+	cdr = d.
+|)
+('as yet unclassified'
+match: p <Pattern> ^ <Binding> = (
+	| mr = p car: car cdr: cdr. |
+	"so i want a helper for this..."
+	^ mr isBound ifTrue: [ mr ] ifFalse: [ p first: car rest: cdr ]
+))
+class Nil = (|
+|)
+('as yet unclassified'
+match: p <Pattern> ^ <Binding> = (
+	" ugly :( "
+	^ p nil: self.
+))'as yet unclassified'
+testConsCell = (
+|	c = Cons of: 1 and: Nil new.
+	a |
+	a:: c case:
+			<car: ?x cdr:> => [  x ]
+		   otherwise: [ Nil new ].
+	assert: a = 1.
+)
+testConsCellAsFirstRest = (
+|	c = Cons of: 1 and: Nil new.
+	a |
+	a:: c case:
+			<first: ?x rest:> => [  x ]
+		   otherwise: [ Nil new ].
+	assert: a = 1.
+)) : ('as yet unclassified'
+TEST_CONTEXT = (
+	
+))
 class SimplificationTests = TestContext (|
 |)
 (