!
! Based on pattern matching code from Paul Graham's book 'On Lisp'.
USING: assocs classes classes.tuple combinators kernel lexer
-macros make namespaces parser quotations sequences summary words
-;
+make namespaces parser quotations sequences summary words ;
IN: match
SYMBOL: _
2dup [ class-of ] same? [
[ tuple-slots ] bi@ [ (match) ] 2all?
] [ 2drop f ] if ] }
- { [ t ] [ 2drop f ] }
+ [ 2drop f ]
} cond ;
: match ( value1 value2 -- bindings )
MACRO: match-cond ( assoc -- quot )
<reversed>
- dup ?first callable? [ unclip ] [ [ throw-no-match-cond ] ] if
+ dup ?first callable? [ unclip ] [ [ no-match-cond ] ] if
[
first2
[ [ dupd match ] curry ] dip
(match-first) drop ;
: (match-all) ( seq pattern-seq -- )
- [ nip ] [ (match-first) swap ] 2bi
+ [ (match-first) ] keep
[ , [ swap (match-all) ] [ drop ] if* ] [ 2drop ] if* ;
: match-all ( seq pattern-seq -- bindings-seq )