@@ -35,9 +35,10 @@ <h2>Simple SVG Graphics: Stairs</h2>
3535
3636< script type ="module ">
3737 import { forever } from "../../kolibri/sequence/util/helpers.js" ;
38- import { Scheduler } from "../../kolibri/dataflow/dataflow.js" ;
3938 import { Sequence } from "../../kolibri/sequence/constructors/sequence/Sequence.js" ;
4039 import { dom } from "../../kolibri/util/dom.js" ;
40+ import { Seq } from "../../kolibri/sequence/constructors/seq/seq.js" ;
41+ import { id } from "../../kolibri/stdlib.js" ;
4142
4243 const svgWidth = 500 ;
4344 const [ svg ] = dom ( `<svg id="svg" viewBox="${ - svgWidth / 2 } ${ - svgWidth / 2 } ${ svgWidth } ${ svgWidth } " > </svg> ` ) ;
@@ -49,7 +50,7 @@ <h2>Simple SVG Graphics: Stairs</h2>
4950 [ - 10 , 10 ] ,
5051 [ - 10 , - 10 ] ,
5152 ] ;
52- const nextAfter = ( [ [ firstX , firstY ] , second , third , [ lastX , lastY ] ] ) =>
53+ const nextAfter = ( [ [ firstX , firstY ] , second , third , [ lastX , lastY ] ] ) => // pure
5354 [
5455 second ,
5556 third ,
@@ -58,22 +59,25 @@ <h2>Simple SVG Graphics: Stairs</h2>
5859 firstY + ( firstY - lastY ) * 0.05 ]
5960 ] ;
6061
61- const lineFor = ( [ _f , _s , [ x1 , y1 ] , [ x2 , y2 ] ] ) =>
62+ const lineFor = ( [ _f , _s , [ x1 , y1 ] , [ x2 , y2 ] ] ) => // pure
6263 `<line x1="${ x1 } " y1="${ y1 } " x2="${ x2 } " y2="${ y2 } "></line>` ;
6364
64- const allTrails = Sequence ( trail , forever , nextAfter ) ;
65+ const allTrails = Sequence ( trail , forever , nextAfter ) ; // pure, lazy
66+ const renderLine = line => svg . innerHTML += line ; // impure side effect
67+ const wait = ms => ok => setTimeout ( ok , ms ) ; // impure async
68+ const immediate = f => ok => { f ( ) ; ok ( ) } ; // ready for promise ctor
6569
66- const scheduler = Scheduler ( ) ;
70+ const lazyLines = allTrails
71+ . map ( lineFor )
72+ . map ( line => ( ) => renderLine ( line ) ) // map to a sequence of _functions_ that can be called as f()
73+ . map ( immediate ) // make each function Promise-ready
74+ . and ( f => Seq ( f , wait ( 50 ) ) ) // intersperse with monads FTW
75+ . scan ( ( acc , cur ) => acc . then ( ( ) => new Promise ( cur ) ) , Promise . resolve ( undefined ) ) ;
76+
77+ lazyLines
78+ . take ( 255 * 2 ) // late "pruning" thanks to laziness
79+ . reduce$ ( id , undefined ) ; // start the promises, trigger the painting
6780
68- allTrails
69- . take ( 255 )
70- . map ( lineFor )
71- . forEach$ ( line =>
72- scheduler . add ( ok => setTimeout ( _ => {
73- svg . innerHTML += line ;
74- ok ( ) ;
75- } , 50 ) )
76- ) ;
7781
7882</ script >
7983</ body >
0 commit comments