This commit is contained in:
zeroflag 2021-09-01 12:33:24 +02:00
parent 6ec630b6ad
commit be2d76431c
5 changed files with 22 additions and 16 deletions

View file

@ -284,7 +284,7 @@ A list is a dynamic, ordered data structed. `[` and `]` are Forth words, so a wh
``` ```
```forth ```forth
1 2 3 4 5 list* \ creates a new list and loads all items from the stack into it 1 2 3 4 5 >list* \ creates a new list and loads all items from the stack into it
``` ```
```forth ```forth
@ -342,7 +342,7 @@ Maps contain key value pairs.
``` ```
```forth ```forth
'key1' 'value1' map* \ creates a new map and loads all items from the stack into it. There must be an even number of items on the stack. 'key1' 'value1' >map* \ creates a new map and loads all items from the stack into it. There must be an even number of items on the stack.
``` ```
```forth ```forth

View file

@ -58,16 +58,15 @@
: peel ( l -- .. ) -> lst lst { } each ; : peel ( l -- .. ) -> lst lst { } each ;
: peel# ( m -- .. ) -> m m { dup 1st swap 2nd } each ; : peel# ( m -- .. ) -> m m { dup 1st swap 2nd } each ;
: list* ( .. -- l ) : >list* ( .. -- l )
<list> -> lst <list> -> lst
depth 0 do lst swap add loop depth 0 do lst swap add loop
lst reverse ; lst reverse ;
: map* ( .. -- m ) : >map* ( .. -- m )
depth odd? if 'expected even number of items for a map*' abort then depth odd? if 'expected even number of items for a >map*' abort then
<map> -> m <map> -> m
depth 2 / 0 do m -rot put loop depth 2 / 0 do m -rot put loop
m .
m ; m ;
: #[ ( -- ) depth >r rswap ; : #[ ( -- ) depth >r rswap ;
@ -110,4 +109,6 @@
: maxl ( ls -- n ) : maxl ( ls -- n )
nil => result nil => result
{ result @ nil != if result @ max then result ! } each { result @ nil != if result @ max then result ! } each
result @ ; result @ ;
: map* ( .. q -- .. ) >r >list* r> map peel ;

View file

@ -19,11 +19,13 @@
: match: immediate ` lastword set-predicate ; : match: immediate ` lastword set-predicate ;
: round* { round } map* ;
: npv ( cashflow rate -- n ) : npv ( cashflow rate -- n )
-> rate 0 => year -> rate 0 => year
{ rate year @ dis year inc } map sum ; { rate year @ dis year inc } map sum ;
: npv* ( .. rate -- n ) >r list* r> npv ; : npv* ( .. rate -- n ) >r >list* r> npv ;
: npv/npv' ( cashflow rate -- npv/npv' ) : npv/npv' ( cashflow rate -- npv/npv' )
1+ -> rate 0 => n 1+ -> rate 0 => n
@ -47,13 +49,13 @@ var: irr-guess 0 irr-guess !
loop loop
nil ; nil ;
: irr* ( cashflow -- n ) list* irr ; : irr* ( cashflow -- n ) >list* irr ;
var: juggler.steps 5 juggler.steps ! var: juggler.steps 5 juggler.steps !
[ ] val: juggler.exclude [ ] val: juggler.exclude
: juggler.solve ( steps exclude-list output-list input-list -- list/nil ) :com.vectron.fcl.Juggler/solve/TTTi jvm-call-static ; : juggler.solve ( steps exclude-list output-list input-list -- list/nil ) :com.vectron.fcl.Juggler/solve/TTTi jvm-call-static ;
: wzd* ( stack1 stack2 -- list/nil ) list* exchange list* aux> juggler.steps @ juggler.exclude 2swap juggler.solve ; : wzd* ( stack1 stack2 -- list/nil ) >list* exchange >list* aux> juggler.steps @ juggler.exclude 2swap juggler.solve ;
: udp-send-byte ( host port byte -- n ) :com.vectron.forthcalc.support.Udp/sendByte/Nis jvm-call-static ; : udp-send-byte ( host port byte -- n ) :com.vectron.forthcalc.support.Udp/sendByte/Nis jvm-call-static ;
: udp-send-str ( host port byte -- n ) :com.vectron.forthcalc.support.Udp/sendStr/sis jvm-call-static ; : udp-send-str ( host port byte -- n ) :com.vectron.forthcalc.support.Udp/sendStr/sis jvm-call-static ;

View file

@ -90,6 +90,6 @@ var: oy
x inc x inc
} each ; } each ;
: plots ( .. -- ) depth 0 != if list* plotl then ; : plots ( .. -- ) depth 0 != if >list* plotl then ;
reset-zoom reset-zoom

View file

@ -15,6 +15,7 @@ import org.junit.Test;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -857,6 +858,8 @@ public class FclTest {
assertEquals(55, evalPop(": tst 0 1 10 .. { + } each ; tst").intValue()); assertEquals(55, evalPop(": tst 0 1 10 .. { + } each ; tst").intValue());
assertEquals("[ 1 3 5 7 9 ]", evalPop(": tst 1 10 .. { odd? } filter ; tst").toString()); assertEquals("[ 1 3 5 7 9 ]", evalPop(": tst 1 10 .. { odd? } filter ; tst").toString());
assertEquals("[ 100 102 104 ]", evalPop(": tst 100 105 .. { even? } filter ; tst").toString()); assertEquals("[ 100 102 104 ]", evalPop(": tst 100 105 .. { even? } filter ; tst").toString());
assertEquals(asList(1l, 4l, 9l), evalGetStack(": tst 1 2 3 { dup * } map* ; tst"));
assertEquals(asList(1l, 2l, 4l), evalGetStack("1.1 2.2 3.8 round*"));
} }
@Test @Test
@ -880,7 +883,7 @@ public class FclTest {
assertEquals("[ 'a' 'c' ]", evalPop("[ 'a' 'b' 'c' ] dup 'b' remove").toString()); assertEquals("[ 'a' 'c' ]", evalPop("[ 'a' 'b' 'c' ] dup 'b' remove").toString());
assertEquals(asList(1l, 2l, 3l, 4l), evalGetStack("[ 1 2 3 4 ] peel")); assertEquals(asList(1l, 2l, 3l, 4l), evalGetStack("[ 1 2 3 4 ] peel"));
assertEquals(asList(1l, 2l, 3l, 4l), evalGetStack(": tst [ 1 2 3 4 ] peel ; tst")); assertEquals(asList(1l, 2l, 3l, 4l), evalGetStack(": tst [ 1 2 3 4 ] peel ; tst"));
assertEquals("[ 1 2 3 4 ]", evalPop("[ 1 2 3 4 ] peel list*").toString()); assertEquals("[ 1 2 3 4 ]", evalPop("[ 1 2 3 4 ] peel >list*").toString());
} }
@Test @Test
@ -906,9 +909,9 @@ public class FclTest {
assertEquals(asList("b", 2l, "a", 1l), assertEquals(asList("b", 2l, "a", 1l),
evalGetStack("#[ 'a' 1 'b' 2 ]# peel#")); evalGetStack("#[ 'a' 1 'b' 2 ]# peel#"));
assertEquals("#[ 'b' 2 'a' 1 ]#", assertEquals("#[ 'b' 2 'a' 1 ]#",
evalPop("'a' 1 'b' 2 map*").toString()); evalPop("'a' 1 'b' 2 >map*").toString());
assertEquals("#[ 'a' 1 'b' 2 ]#", assertEquals("#[ 'a' 1 'b' 2 ]#",
evalPop("#[ 'a' 1 'b' 2 ]# peel# map*").toString()); evalPop("#[ 'a' 1 'b' 2 ]# peel# >map*").toString());
assertEquals("[ 'a' 1 ]", assertEquals("[ 'a' 1 ]",
evalPop("#[ 'a' 1 'b' 2 ]# peel nip").toString()); evalPop("#[ 'a' 1 'b' 2 ]# peel nip").toString());
assertEquals("[ 'b' 2 ]", assertEquals("[ 'b' 2 ]",
@ -979,10 +982,10 @@ public class FclTest {
resetForth(); resetForth();
} }
try { try {
eval("1 map*"); eval("1 >map*");
fail("expected abort"); fail("expected abort");
} catch (Aborted e) { } catch (Aborted e) {
assertEquals("expected even number of items for a map*", e.getMessage()); assertEquals("expected even number of items for a >map*", e.getMessage());
resetForth(); resetForth();
} }
} }