1+ use crate :: runtime:: { stack:: Stack , value:: { Array , Value } } ;
2+
3+ use super :: { core:: define_commands, RuntimeException } ;
4+ use indoc:: indoc;
5+
6+ fn _test ( stack : & mut Stack ) -> Result < ( ) , RuntimeException > {
7+ if let Value :: Number ( num) = stack. pop ( ) ? {
8+ stack. push ( Value :: Array ( Array :: from (
9+ ( 0 ..( num. round ( ) as i32 ) ) . map ( |n| Value :: Number ( n. into ( ) ) ) . collect ( )
10+ ) ) ) ;
11+ } Err ( RuntimeException :: WrongElementType )
12+ }
13+
14+ define_commands ! ( append_iters
15+ EachCommand ( '∴' , [ "map" , "each" ] ) {
16+ with {
17+ description: String :: from( indoc! { "
18+ Applies function to each value
19+ " } )
20+ }
21+ stack {
22+ let f = stack. pop( ) ?;
23+ if let Value :: Array ( arr) = stack. pop( ) ? {
24+ let old = arr. move_out( ) ;
25+ let mut new = vec![ ] ;
26+ for e in old {
27+ stack. push( e) ;
28+ f. invoke( stack) ?;
29+ new. push( stack. pop( ) ?) ;
30+ }
31+ stack. push( Value :: Array ( Array :: from( new) ) ) ;
32+ } else { return Err ( RuntimeException :: WrongElementType ) }
33+ Ok ( ( ) )
34+ }
35+ } ,
36+
37+ EmptyArrayCommand ( '∅' , [ "aempty" , "emptyarr" ] ) {
38+ with {
39+ description: String :: from( indoc! { "
40+ Places an empty array on top of stack
41+ " } )
42+ }
43+ stack {
44+ stack. push( Value :: Array ( Array :: new( ) ) ) ;
45+ Ok ( ( ) )
46+ }
47+ } ,
48+
49+ Range ( '⇡' , [ "range" ] ) {
50+ with {
51+
52+ }
53+ stack {
54+ if let Value :: Number ( num) = stack. pop( ) ? {
55+ stack. push( Value :: Array ( Array :: from(
56+ ( 0 ..( num. round( ) as i32 ) ) . map( |n| Value :: Number ( n. into( ) ) ) . collect( )
57+ ) ) ) ;
58+ return Ok ( ( ) )
59+ } Err ( RuntimeException :: WrongElementType )
60+ }
61+ }
62+ ) ;
0 commit comments