]> gitweb.factorcode.org Git - factor.git/commitdiff
generalizations: Add nrotates and -nrotates.
authorDoug Coleman <doug.coleman@gmail.com>
Tue, 14 Jun 2022 03:57:00 +0000 (22:57 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Fri, 17 Jun 2022 02:28:15 +0000 (21:28 -0500)
3 d nrotates -> d nrot d nrot d nrot

core/generalizations/generalizations-tests.factor
core/generalizations/generalizations.factor

index 87157eac086534a57541d99f42996bc8cbc54181..c59940026e197ee13fe81185847e673c061389fb 100644 (file)
@@ -29,6 +29,12 @@ IN: generalizations.tests
 [ [ 1 ] 5 ndip ] must-infer
 { 1 2 3 4 } [ 2 3 4 [ 1 ] 3 ndip ] unit-test
 
+[ [ 1 2 3 ] 2 3 nrotates ] must-infer
+[ [ 1 2 3 ] 2 3 -nrotates ] must-infer
+{ 1 2 3 4 } [ 1 2 3 4  4 4 nrotates ] unit-test
+{ 1 2 3 4 } [ 1 2 3 4  4 4 -nrotates ] unit-test
+{ 4 1 2 3 } [ 1 2 3 4  1 4 -nrotates ] unit-test
+
 [ 1 2 3 4 5 [ drop drop drop drop drop 2 ] 5 nkeep ] must-infer
 [ 1 2 3 4 5 2 '[ drop drop drop drop drop _ ] 5 nkeep ] must-infer
 { 2 1 2 3 4 5 } [ 1 2 3 4 5 [ drop drop drop drop drop 2 ] 5 nkeep ] unit-test
index b45e61ce6812efb9742b9ab61f9e893fc6b25a15..5ead9b12dd03499badf0fcd46f3a841659788427 100644 (file)
@@ -5,8 +5,8 @@ USING: arrays combinators kernel kernel.private math ranges
 memoize.private sequences ;
 IN: generalizations
 
-! These words can be inline combinators the word does no math on
-! the input parameters, e.g. n.
+! These words can be inline combinators when the word does no math
+! on the input parameters, e.g. n.
 ! If math is done, the word needs to be a macro so the math can
 ! be done at compile-time.
 <<
@@ -48,15 +48,21 @@ MACRO: nrot ( n -- quot )
 MACRO: -nrot ( n -- quot )
     1 - [ ] [ '[ swap _ dip ] ] repeat ;
 
+: ndip ( n -- )
+    [ [ dip ] curry ] swap call-n call ; inline
+
+: nrotates ( n depth -- quot )
+    '[ _ [ _ nrot ] times ] call ; inline
+
+: -nrotates ( n depth -- quot )
+    '[ _ [ _ -nrot ] times ] call ; inline
+
 : ndrop ( n -- )
     [ drop ] swap call-n ; inline
 
 : nnip ( n -- )
     '[ _ ndrop ] dip ; inline
 
-: ndip ( n -- )
-    [ [ dip ] curry ] swap call-n call ; inline
-
 : nkeep ( n -- )
     dup '[ [ _ ndup ] dip _ ndip ] call ; inline