Paste: chunked.cgi

Author: Serre
Mode: factor
Date: Fri, 6 Mar 2026 11:24:33
Plain Text |
#!/usr/bin/env factor-vm
USING: assocs calendar environment formatting io
io.encodings.string io.encodings.utf8 kernel literals math
math.parser prettyprint sequences threads vocabs ;
IN: chunked-cgi

: send-chunk ( string -- )
   [ utf8 encode length >hex print ] keep print flush ;

: tick-loop ( n -- )
   [ "This page will self-destruct in %.1f seconds.<br/>"
     sprintf send-chunk ]
   [ 0.5 dup seconds sleep - ] bi
   [ 0 <= ] 1check [ drop ] [ tick-loop ] if ;

! == Script proper:
   "Content-type: text/html; charset=utf8" print
   "Transfer-Encoding: chunked\r\n" print
   8 tick-loop "*Ka-Boom*" send-chunk "0\r\n" print

Annotation: Pointless Smartassery.

Author: Serre
Mode: factor
Date: Fri, 6 Mar 2026 12:31:38
Plain Text |
! If you wanted to be pointlessly clever, the tick loop can
! instead be implemented as a series of cascading, parallel
! threads mapped over the desired time intervals.
: tick-loop ( n -- )
   0 swap 0.5 <range> [ reverse ] keep zip
      [ first2 seconds sleep
        "This page will self-destruct in %.1f seconds.<br/>"
        sprintf send-chunk ]
   parallel-each ;

Annotation: Practical Smartassery.

Author: Serre
Mode: factor
Date: Fri, 6 Mar 2026 12:47:38
Plain Text |
! ...which is a very pointless way to avoid explicit looping
! since you can do the same by rolling the seconds to sleep
! into the quot to each.
: tick-loop ( n -- )
   0 swap 0.5 [ <range> reverse ] keep
   [ seconds sleep  "This page will self-destruct in 
   %.1f seconds.<br/>" sprintf send-chunk ] curry each ;

New Annotation

Summary:
Author:
Mode:
Body: