From d72865f02218b21af489929823a216eb8435b737 Mon Sep 17 00:00:00 2001 From: crc Date: Wed, 14 Nov 2018 23:51:42 +0000 Subject: [PATCH] add Matrix example FossilOrigin-Name: dbd9a4588a272ee38d70897ca36ad3698ef6e63df28af91e17e4b76f4211b9f5 --- example/Matrix.forth | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 example/Matrix.forth diff --git a/example/Matrix.forth b/example/Matrix.forth new file mode 100644 index 0000000..5df5208 --- /dev/null +++ b/example/Matrix.forth @@ -0,0 +1,35 @@ +#!/usr/bin/env retro -i -s + +By WilhelmVonWeiner: Matrices that store their bounds and a +couple rushedly written tests. + +~~~ +{{ + :prepare (nms--knms) push dup-pair * rot rot pop ; + :create (nms--) d:create dup-pair * #2 + allot ; + :initialise (nm--) d:last store-next store-next ; + :fill (na--a) [ store-next ] times ; +---reveal--- + :matrix (nms--a) create initialise ; + :matrix (x1xn...nms--a) prepare matrix swap fill drop ; +}} +~~~ + +Test matrix, should print "matrix works!" and not "broken!". + +``` +:get-next n:dec dup fetch ; +:broken 'broken s:put nl ; +#3 #1 'tester matrix +get-next #3 -eq? [ broken ] if +get-next #1 -eq? [ broken ] if +'matrix_works!_at_ s:put n:put nl +``` + +Test matrix, should be "contained!" thrice. + +``` +#30 #20 #10 #3 #1 'tester s:keep matrix +[ tester #2 + #3 [ fetch-next swap ] times drop ] set:from-results +[ { #10 #20 #30 } set:contains? [ 'contained! s:put sp nl ] if ] set:for-each +```