]> gitweb.factorcode.org Git - factor.git/blob - extra/stack-as-data/stack-as-data.factor
c933c51b63ba18f2003d02ad04706186262acca9
[factor.git] / extra / stack-as-data / stack-as-data.factor
1 ! Copyright (C) 2022 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators generalizations kernel math sequences ;
4 IN: stack-as-data
5
6 MACRO: stack-nth ( n -- quot )
7     [ '[ 1 _ ndupd ] ]
8     [ 1 + '[ _ nrot ] ] bi
9     '[ @ @ ] ;
10
11 : stack-set-nth ( obj n -- quot )
12     [ '[ drop _ ] ] dip ndip ; inline
13
14 : stack-exchange ( m n -- quot )
15     [ [ stack-nth ] [ '[ _ stack-nth ] dip ] bi* ] 2keep
16     swapd
17     [ stack-set-nth ] 2dip stack-set-nth ;
18
19 : stack-filter ( n quot: ( obj -- ? ) -- quot' )
20     selector [ '[ _ ] replicate spread ] dip ; inline
21
22 : stack-map ( n quot: ( obj -- obj' ) -- quot' )
23     '[ _ ] replicate spread ; inline