[toronto-lisp] Factor notes, and website discussion
Vishvajit Singh
vishvajitsingh at gmail.com
Mon Feb 9 03:57:22 UTC 2009
Sorry, some of the attachments were scrubbed from the previous mail.
Re-sent in txt form.
Vish Singh
On Sun, Feb 8, 2009 at 10:54 PM, Vishvajit Singh
<vishvajitsingh at gmail.com> wrote:
> Hi everyone,
>
> I've attached the notes from my Factor presentation last week, along
> with some of the code I demonstrated. I'm new at giving this kind of
> presentation, so it was a learning experience for me. I think that the
> live presentation I gave (actual coding, mistakes and all) worked a
> lot better than a PowerPoint-style one would have. I hope to give more
> presentations in the future.
>
> This stuff really ought to go on a website. Let's set up a Toronto
> Lisp website for tutorials, code snippets, meeting minutes, project
> collaboration, and so on. I believe someone said they already had a
> domain name registered, which is great. How about we set up a Wiki at
> first, and see where it goes?
>
> Vish Singh
>
-------------- next part --------------
USING: random locals ;
TUPLE: node left right data ;
: <node> node boa ;
: if2* ( ? x true false -- )
[ [ swap ] prepose ] dip
[ swap ] 2dip
if* ;
: when2* ( ? x true -- )
[ drop ] if2* ;
: if-positive ( n true false -- )
[ dup 0 > ] 2dip if ;
: split-node ( node -- left right data )
[ left>> ] [ right>> ] [ data>> ] tri ;
: make-random-tree ( depth -- tree )
[ 1 - dup [ make-random-tree ] bi@ 1000 random <node> ]
[ drop f ]
if-positive ;
: walk-tree-ordered ( tree quot -- )
[ [ [ left>> ] dip walk-tree-ordered ]
[ [ data>> ] dip call ]
[ [ right>> ] dip walk-tree-ordered ]
2tri ]
when2* ;
: collector ( -- reader writer )
[let | seq! [ { } ] |
[ seq ]
[ seq swap suffix seq! ] ] ;
: flatten-tree-ordered ( tree -- seq )
collector swapd walk-tree-ordered call ;
: add-to-binary-tree ( tree x -- newtree )
[ [ split-node ] dip 2dup < rot
[ [ add-to-binary-tree ] [ swapd add-to-binary-tree swap ] if ] dip ]
[ [ f f ] dip ]
if2* <node> ;
: seq-to-binary-tree ( seq -- tree )
f swap [ add-to-binary-tree ] each ;
: binary-sort ( seq -- sorted-seq )
seq-to-binary-tree flatten-tree-ordered ;
-------------- next part --------------
Binary tree example:
USING: random ;
TUPLE: node left right data ;
: <node> node boa ;
: make-random-tree ( depth -- tree )
dup 0 >
[ 1 - dup [ make-random-tree ] bi@ 1000 random <node> ]
[ drop f ]
if ;
How does it work?
: make-random-tree ( depth -- tree )
dup 0 >
[ true-branch.. ]
[ false-branch.. ]
if ;
What does the true-branch do?
[ 1 - dup [ make-random-tree ] bi@ 1000 random <node> ]
1 - Subtract 1 from depth.
dup [ make-random-tree ] bi@ Push onto the stack two random trees with this depth.
1000 random Push onto the stack a random number from 0 to 999.
<node> Construct a node, setting its slots from the 3 values on the stack.
What does the false-branch do?
drop Throw away the depth parameter.
f Push onto the stack the empty tree.
-------------- next part --------------
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: io io.servers.connection accessors threads calendar calendar.format ;
IN: time-server
: handle-time-client ( -- )
now timestamp>rfc822 print ;
: <time-server> ( -- threaded-server )
<threaded-server>
"time-server" >>name
1234 >>insecure
[ handle-time-client ] >>handler ;
: start-time-server ( -- threaded-server )
<time-server> [ start-server ] in-thread ;
MAIN: start-time-server
More information about the toronto-lisp
mailing list