]> gitweb.factorcode.org Git - factor.git/commitdiff
combinators.extras: adding ?call.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 13 Nov 2020 04:35:07 +0000 (20:35 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 13 Nov 2020 04:35:07 +0000 (20:35 -0800)
this is kind of like an optional chained operation.

extra/combinators/extras/extras-tests.factor
extra/combinators/extras/extras.factor

index 3ca171eb053d017deecd3c25f7e7d26ac5cb717f..a038e43f797ea64eefb393e085c0016b28479c1d 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2013 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: combinators.extras io.files kernel math sequences
-tools.test ;
+splitting tools.test ;
 
 { "a b" }
 [ "a" "b" [ " " glue ] once ] unit-test
@@ -60,3 +60,8 @@ tools.test ;
         [ f ]
     } cond*
 ] unit-test
+
+{ f } [ f { } ?call ] unit-test
+{ 5 } [
+    "hello factor!" { [ " " split ] [ first ] [ length ] } ?call
+] unit-test
index 2d8047b0b99a3bba6fd5415a0fa5cb895ceb35fa..77e1c3ec8808a748cb094e0eaa5845900ab38f0c 100644 (file)
@@ -103,3 +103,6 @@ DEFER: cond*
     [ dup callable? [ drop t ] [ first call ] if ] map-find
     [ dup callable? [ nip call ] [ second call ] if ]
     [ no-cond ] if* ;
+
+MACRO: ?call ( quot -- quot )
+    <reversed> [ ] [ swap '[ [ @ @ ] [ f ] if* ] ] reduce ;