From 142007f94ddfe228d9b2c208a82846447a495b17 Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Sun, 4 Jan 2009 15:59:55 -0500 Subject: [PATCH] inverses for append and prepend --- extra/inverse/inverse-tests.factor | 5 +++++ extra/inverse/inverse.factor | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/extra/inverse/inverse-tests.factor b/extra/inverse/inverse-tests.factor index d106b1068a..5e662ed78f 100644 --- a/extra/inverse/inverse-tests.factor +++ b/extra/inverse/inverse-tests.factor @@ -70,3 +70,8 @@ C: nil [ t ] [ pi [ pi ] matches? ] unit-test [ 0.0 ] [ 0.0 pi + [ pi + ] undo ] unit-test [ ] [ 3 [ _ ] undo ] unit-test + +[ { 1 } ] [ { 1 2 3 } [ { 2 3 } append ] undo ] unit-test +[ { 3 } ] [ { 1 2 3 } [ { 1 2 } prepend ] undo ] unit-test +[ { 1 2 3 } [ { 1 2 } append ] undo ] must-fail +[ { 1 2 3 } [ { 2 3 } prepend ] undo ] must-fail diff --git a/extra/inverse/inverse.factor b/extra/inverse/inverse.factor index f1ca394e80..21fdecdafd 100755 --- a/extra/inverse/inverse.factor +++ b/extra/inverse/inverse.factor @@ -4,13 +4,13 @@ USING: accessors kernel words summary slots quotations sequences assocs math arrays stack-checker effects generalizations continuations debugger classes.tuple namespaces make vectors bit-arrays byte-arrays strings sbufs math.functions macros -sequences.private combinators mirrors +sequences.private combinators mirrors splitting combinators.short-circuit fry words.symbol ; RENAME: _ fry => __ IN: inverse ERROR: fail ; -M: fail summary drop "Unification failed" ; +M: fail summary drop "Matching failed" ; : assure ( ? -- ) [ fail ] unless ; @@ -204,6 +204,9 @@ DEFER: _ \ unclip [ prefix ] define-inverse \ suffix [ dup but-last swap peek ] define-inverse +\ append 1 [ [ ?tail assure ] curry ] define-pop-inverse +\ prepend 1 [ [ ?head assure ] curry ] define-pop-inverse + ! Constructor inverse : deconstruct-pred ( class -- quot ) "predicate" word-prop [ dupd call assure ] curry ;