]> gitweb.factorcode.org Git - factor.git/blob - extra/stack-as-data/stack-as-data.factor
Fixes #2966
[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 MACRO: stack-set-nth ( obj n -- quot )
12     '[ [ drop _ ] _ ndip ] ;
13
14 MACRO: stack-exchange ( m n -- quot )
15     [ [ stack-nth ] [ '[ _ stack-nth ] dip ] bi* ] 2keep
16     swapd
17     '[ _ _ stack-set-nth _ _ stack-set-nth ] ;
18
19 MACRO: stack-map ( n quot: ( obj -- obj' ) -- quot' )
20     '[ _ ] replicate '[ _ spread ] ;