• R/O
  • SSH

Joypy: Commit

Main interpreter and library.


Commit MetaInfo

Revision8825021cd713ab5f4b50617dd05b6d29ec73356b (tree)
Time2019-07-22 00:28:56
AuthorSimon Forman <sforman@hush...>
CommiterSimon Forman

Log Message

Implement map combinator.

Change Summary

Incremental Difference

diff -r 101d507003b8 -r 8825021cd713 thun/defs.txt
--- a/thun/defs.txt Sun Jul 21 07:32:20 2019 -0700
+++ b/thun/defs.txt Sun Jul 21 08:28:56 2019 -0700
@@ -1,9 +1,11 @@
11 -- == 1 -
2+? == dup bool
23 ++ == 1 +
3-? == dup bool
44 anamorphism == [pop []] swap [dip swons] genrec
55 app1 == grba infrst
66 app2 == [grba swap grba swap] dip [infrst] cons ii
7+app3 == 3 appN
8+appN == [grabN] cons dip map disenstacken
79 at == drop first
810 average == [sum 1.0 *] [size] cleave /
911 b == [i] dip i
@@ -23,6 +25,7 @@
2325 fork == [i] app2
2426 fourth == rest third
2527 gcd == true [tuck mod dup 0 >] loop pop
28+grabN == [] swap [cons] times
2629 grba == [stack popd] dip
2730 hypot == [sqr] ii + sqrt
2831 ifte == [nullary] dipd swap branch
diff -r 101d507003b8 -r 8825021cd713 thun/thun.pl
--- a/thun/thun.pl Sun Jul 21 07:32:20 2019 -0700
+++ b/thun/thun.pl Sun Jul 21 08:28:56 2019 -0700
@@ -242,6 +242,34 @@
242242 Quoted = [If, Then, R0, R1, genrec],
243243 append(R0, [Quoted|R1], Else).
244244
245+/*
246+This is a crude but servicable implementation of map combinator.
247+Obviously it would be nice to take advantage of the implied parallelism.
248+Instead the quoted program, stack, and each term in the arg list are
249+transformed to a simple Joy expression that runs the program on a prepared
250+stack. These expressions are collected in a list and the whole thing is
251+evaluated with infra on an empty list, so the result is the mapped list.
252+
253+The chief advantage of doing it this way (as opposed to using Prolog's map)
254+is that the whole state remains in the continuation expression.
255+*/
256+
257+combo(map, [_, []|S], [[]|S], E, E ) :- !.
258+combo(map, [P, List|S], [Mapped, []|S], E, [infra|E]) :-
259+ prepare_mapping(P, S, List, Mapped).
260+
261+% Set up a program for each term in ListIn
262+%
263+% [term S] [P] infrst
264+%
265+% prepare_mapping(P, S, ListIn, ListOut).
266+
267+prepare_mapping(P, S, In, Out) :- prepare_mapping(P, S, In, [], Out).
268+
269+prepare_mapping( _, _, [], Out, Out) :- !.
270+prepare_mapping( P, S, [T|In], Acc, Out) :-
271+ prepare_mapping(P, S, In, [[T|S], P, infrst|Acc], Out).
272+
245273
246274 /*
247275 Compiler
Show on old repository browser