egison-2.3.8: sample/collection-test.egi
(test (match-all {1 2 3 4 5} (List Integer) [<join ,{1 2} $xs> xs]))
(test (match-all {[1 2] [1 2]} (List [Integer Integer])
[<cons [$a $b] _> [a b]]))
;; match Multiset
(test (match-all {1 2 3 4 5} (Multiset Integer)
[<cons $m <cons $n _>> [m n]]))
;; match a collection of collections
(test (match-all {{1 2 3} {11} {21 22}} (List (Multiset Integer))
[<cons <cons $x _>
<cons <cons $y _>
<cons <cons $z _>
<nil>>>>
[x y z]]))
;; match a collection of collections with value patterns
(test (match-all {{1 2 3 4 5} {4 5 1} {6 1 7 4}} (List (Multiset Integer))
[<cons <cons $n _>
<cons <cons ,n _>
<cons <cons ,n _>
<nil>>>>
n]))
;; pattern as first class object
(test (let {[$pat <cons ,1 <nil>>]}
(match {1} (Multiset Integer)
{[pat <ok>]
[_ <not-ok>]})))
;; Or-pattern test1
(test (match {} (Multiset Integer)
{[(| <nil> <cons ,1 <nil>>) <ok>]
[_ <not-ok>]}))
;; Or-pattern test2
(test (match {1} (Multiset Integer)
{[(| <nil> <cons ,1 <nil>>) <ok>]
[_ <not-ok>]}))
(test (match-all {1 2 3 4 5 6 7 1 3 5 7} (Multiset Integer)
[<cons $x ^<cons ,x _>> x]))
(test (match-all {{1 2 3} {2 3 4} {1}} (Multiset (Multiset Integer))
[<cons <cons $x ^<cons ,x _>>
<cons ^<cons ,x _>
<cons ^<cons ,x _>
<nil>>>>
x]))
(test (match-all {{1 2 3} {2 3 4}} (Multiset (Multiset Integer))
[<cons <cons $x ^<cons ,x _>>
<cons ^<cons ,x _>
<nil>>>
x]))
(test (match-all {1 2 3} (List Integer)
[<join $hs $ts> [hs ts]]))
(test (match-all {1 2 3} (List Integer)
[<nioj $hs $ts> [hs ts]]))
(test (match-all {1 2 3} (Multiset Integer)
[<join $hs $ts> [hs ts]]))
(test (match-all {1 2 3} (Set Integer)
[<join $hs $ts> [hs ts]]))
(test (match-all {1 2 3} (List Integer)
[<join $hs <cons $x $ts>> [hs x ts]]))
;; noij
(test (match-all {<x> <y> <z>} (List Something) [<nioj $xs $ys> [xs ys]]))
(test ((remove-collection Suit) {<club> <heart> <diamond>} {<club> <diamond>}))
(test (subcollections {<x> <y> <z>}))
;; Cut-pattern
(test (match {2 7 7 2 7} (Multiset Integer)
{[<cons $m
<cons ,m
<cons ,m
<cons $n
!<cons ,n
!<nil>>>>>>
<ok>]
[_ <ko>]}))
;; Value-pattern
(test (match {5 2 1 3 4} (Multiset Integer)
{[<cons $n
<cons ,(- n 1)
<cons ,(- n 2)
<cons ,(- n 3)
<cons ,(- n 4)
<nil>>>>>>
<ok>]
[_ <ko>]}))
(test (match-all {1 2 3 4} (List Integer)
[<join _ <join $ns _>> ns]))
;; pattern macro
(define $pair
(macro [$s $pat]
<cons $`s <cons ,`s pat>>))
(test (match-all {1 2 3 1 2} (Multiset Integer)
[(pair %k _) k]))
(define $loop-pat
(lambda [$k $i]
(match i Integer
{[,k <join _ <cons $n_k _>>]
[_ <join _ <cons $n_i (loop-pat k (+ i 1))>>]})))
(define $loop-pat
(lambda [$k]
(| <nil> <cons ,k (loop-pat (- k 1))>)))
(define $isStraight?
(lambda [$Ns]
(match Ns (Multiset Integer)
{[<cons $n
(loop-pat (- n 1))>
<ok>]
[_ <ko>]})))
(define $isStraight?
(lambda [$Ns]
(match Ns (Multiset Integer)
{[<cons (as $n (max Ns))
(loop-pat (- n 1))>
<ok>]
[_ <ko>]})))
(test (isStraight? {1 2}))
;; comparison of large list
(test (match-all (between 1 2000) (List Integer) [<join ,(between 1 1000) <cons $x _>> x]))