author  Tony GarnockJones <tonygarnockjones@gmail.com> 
Tue, 25 May 2010 08:09:57 +1200  
changeset 285  034958cf32d9 
permissions  rwrr 
285
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

1 
Subject: Clojure's sequences and nil punning 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

2 
Timestamp: <20100416 17:25:01 tonyg> 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

3 
From: tonyg 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

4 

034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

5 
http://clojure.org/lazy 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

6 

034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

7 
namespace s = "http://eightytwenty.org/etng/r1/ns/stream#"; 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

8 
define s:foldr stream knil kons > 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

9 
stream .s:case { 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

10 
.s:empty > knil; 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

11 
.s:next(head, tail) > kons(head, s:foldr tail knil kons); 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

12 
}; 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <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 GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

14 

034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

15 
Expand through `s:foldr`, and we get 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

16 

034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

17 
define s:map stream fn > 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

18 
stream .s:case { 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

19 
.s:empty > s:empty; 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <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 GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

21 
}; 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

22 

034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

23 
Delay: 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

24 

034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

25 
define nothing = { .case v > v .nothing }; 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

26 
define just x = { .case v > v .just x }; 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

27 

034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

28 
define delay v > 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

29 
 Carefully written to avoid holding onto v for too long. 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

30 
 Needs let rec cell = etc. to avoid the initial cell.set! 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

31 
let cell = ref (); 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

32 
do cell.set { let forced = v (); 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

33 
do cell.set { forced }; 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

34 
forced }; 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

35 
{ msg > cell.get () msg }; 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

36 

034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

37 
Lazy map: 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

38 

034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

39 
define s:lazymap stream fn > 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

40 
delay {stream .s:case { 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

41 
.s:empty > s:empty; 
034958cf32d9
Notes and incomplete work.
Tony GarnockJones <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 GarnockJones <tonygarnockjones@gmail.com>
parents:
diff
changeset

43 
}}; 