]> gitweb.factorcode.org Git - factor.git/commitdiff
combinators.extras: Add a closure-limit word that takes a maximum number of recursions
authorDoug Coleman <doug.coleman@gmail.com>
Tue, 24 Jan 2023 15:03:05 +0000 (09:03 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 24 Jan 2023 15:03:05 +0000 (09:03 -0600)
extra/combinators/extras/extras.factor

index 33de726db3e155c92140a1671905622010bb5340..2b827b3886dc4a972d774b42b6fe4b21181988a4 100644 (file)
@@ -195,3 +195,20 @@ MACRO: 4keep-under ( quot -- quot' )
 : 1temp1d ( quot: ( a b c -- d e f ) -- quot ) '[ swap @ swap ] ; inline
 : 1temp2d ( quot: ( a b c -- d e f ) -- quot ) '[ rot @ -rot ] ; inline
 : 2temp2d ( quot: ( a b c d -- e f g h ) -- quot ) '[ 2 4 0 nrotated @ 2 4 0 -nrotated ] ; inline
+
+ : (closure-limit) ( vertex set quot: ( vertex -- edges ) i n -- )
+    2dup < [
+        [ 1 + ] dip
+        2reach ?adjoin [
+            [ [ dip ] keep ] 2dip [ (closure-limit) ] 2curry 2curry each
+        ] [ 5drop ] if
+    ] [
+        5drop
+    ] if ; inline recursive
+
+: closure-limit-as ( vertex quot: ( vertex -- edges ) n exemplar -- set )
+    [ 0 ] 2dip
+    new-empty-set-like [ -roll (closure-limit) ] keep ; inline
+
+: closure-limit ( vertex quot: ( vertex -- edges ) n -- set )
+    HS{ } closure-limit-as ; inline