etng-r2/clojure-sequences-20100416.txt
author Tony Garnock-Jones <tonygarnockjones@gmail.com>
Wed, 16 Jan 2019 17:15:58 +0000
changeset 438 1fe179d53161
parent 285 034958cf32d9
permissions -rw-r--r--
Add missing primitive implementation for the plain interpreter.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
285
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     1
Subject: Clojure's sequences and nil punning
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     2
Time-stamp: <2010-04-16 17:25:01 tonyg>
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     3
From: tonyg
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     4
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     5
http://clojure.org/lazy
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     6
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     7
    namespace s = "http://eighty-twenty.org/etng/r1/ns/stream#";
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     8
    define s:foldr stream knil kons ->
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     9
      stream .s:case {
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    10
	.s:empty -> knil;
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    11
	.s:next(head, tail) -> kons(head, s:foldr tail knil kons);
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    12
      };
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    13
    define s:map stream fn -> s:foldr stream s:empty {elt, acc -> s:cons(fn(elt), acc)};
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    14
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    15
Expand through `s:foldr`, and we get
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    16
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    17
    define s:map stream fn ->
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    18
      stream .s:case {
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    19
      	.s:empty -> s:empty;
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    20
	.s:next(head, tail) -> s:cons(fn(head), s:map tail fn);
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    21
      };
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    22
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    23
Delay:
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    24
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    25
    define nothing = { .case v -> v .nothing };
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    26
    define just x  = { .case v -> v .just x };
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    27
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    28
    define delay v ->
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    29
      -- Carefully written to avoid holding onto v for too long.
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    30
      -- Needs let rec cell = etc. to avoid the initial cell.set!
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    31
      let cell = ref ();
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    32
      do cell.set { let forced = v ();
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    33
      	       	    do cell.set { forced };
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    34
		    forced };
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    35
      { msg -> cell.get () msg };
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    36
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    37
Lazy map:
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    38
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    39
    define s:lazymap stream fn ->
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    40
      delay {stream .s:case {
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    41
      	.s:empty -> s:empty;
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    42
	.s:next(head, tail) = s:cons(delay {fn(head)}, delay {s:map tail fn});
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    43
      }};