combinators.smart: adding smart-with.

db4
John Benediktsson 2012-08-13 19:32:12 -07:00
parent 07e74c0ad8
commit 2620c64c21
3 changed files with 14 additions and 2 deletions

View File

@ -226,6 +226,11 @@ HELP: smart-when*
}
{ $description "A version of " { $link when } " that takes two quotations, where the first quotation is a predicate that preserves any inputs it consumes and the second is the " { $snippet "true" } " branch. If the " { $snippet "true" } " branch is taken, the values are left on the stack and the quotation is called. If the " { $snippet "false" } " branch is taken, the number of inputs inferred from predicate quotation is dropped." } ;
HELP: smart-with
{ $values
{ "param" object } { "obj" object } { "quot" { $quotation "( param ..a -- ..b" } } { "curry" curry } }
{ $description "A version of " { $link with } " that puts the parameter before any inputs the quotation uses." } ;
ARTICLE: "combinators.smart" "Smart combinators"
"A " { $emphasis "smart combinator" } " is a macro which reflects on the stack effect of an input quotation. The " { $vocab-link "combinators.smart" } " vocabulary implements a few simple smart combinators which look at the static stack effects of input quotations and generate code which produces or consumes the relevant number of stack values." $nl
"Take all input values from a sequence:"

View File

@ -1,7 +1,7 @@
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays combinators.smart kernel math
stack-checker tools.test locals ;
USING: accessors arrays assocs combinators.smart kernel math
sequences stack-checker tools.test locals ;
IN: combinators.smart.tests
: test-bi ( -- 9 11 )
@ -86,3 +86,7 @@ IN: combinators.smart.tests
[ ( x x -- x ) ] [ [ curry inputs ] infer ] unit-test
[ ( x -- x ) ] [ [ [ curry ] curry inputs ] infer ] unit-test
{ 1 1 1 } [ 1 3 [ ] smart-with times ] unit-test
{ "BCD" } [ 1 "ABC" [ + ] smart-with map ] unit-test
{ H{ { 1 2 } } } [ 1 H{ { 1 2 } { 3 4 } } [ drop = ] smart-with assoc-filter ] unit-test

View File

@ -119,3 +119,6 @@ MACRO: map-reduce-outputs ( quot mapper reducer -- quot )
: smart-apply ( quot n -- )
[ dup inputs ] dip mnapply ; inline
: smart-with ( param obj quot -- obj curry )
swapd dup inputs '[ [ _ -nrot ] dip call ] 2curry ; inline