Hi,

The really smart and non-obvious thing about the Clojure code is that it
represents the board as a set of coordinates to the alive cells. A naive
solution would probably have used a two-dimensional array of size XxY with
the values t for live cells and f for dead ones. Was that part what
confused you? So, if you have the code loaded in Factor (use Emacs and FUEL
so that you can easily experiment) test it like this:

IN: scratchpad { { 1 0 } { 1 1 } { 1 2 } } step
{ { 1 1 } { 0 1 } { 2 1 } }
IN: scratchpad { { 1 0 } { 1 1 } { 1 2 } } step step
{ { 1 1 } { 1 0 } { 1 2 } }

IN: scratchpad is the prompt, the code after is what you should type. Note
that the last expression evaluates to the input argument which proves that
the pattern oscillates. Here is an extension to the program with nicer
output so that one can see what is going on:

http://paste.factorcode.org/paste?id=2963

IN: scratchpad USE: life
IN: scratchpad 3 20 run-life
---
...*..*.
.....*.*
....*..*
..**.*..
....*.*.
*.*.*...
..*.*...
.*..*..*
---
......*.
....**.*
..*..*..
..**.**.
.*......
.*..*...
..*.**..
...*....
---
.....**.
....**..
..*.....
.******.
.*.***..
.*****..
..*.**..
...**...
IN: scratchpad


2013/6/3 John Porubek <jporu...@gmail.com>

> At the risk of exposing my ignorance, how would I test this? I assume
> there's an analog to the Clojure example, but it's exact nature escapes me!
> Any help appreciated.
>
> -John
>
>
> On Sun, May 12, 2013 at 1:00 PM, John Benediktsson <mrj...@gmail.com>wrote:
>
>> I ported a Clojure implementation of Conway's Game of Life to Factor.
>>> Here is the original Clojure version:
>>> http://clj-me.cgrand.net/2011/08/19/conways-game-of-life/
>>> Here is my Factor port: http://paste.factorcode.org/paste?id=2932
>>> How can I make the Factor variant more elegant and/or idiomatic?
>>>
>>
>> Some ideas:
>>
>> 1) Your "frequencies" word is the same as "histogram" in math.statistics
>> vocabulary.
>>
>> 2) Your calculation of neighboring cells could use a constant to
>> represent the 8  adjacent cells, instead of computing it each time:
>>
>> CONSTANT: coordinates {
>>     { 1 1 }
>>     { 1 0 }
>>     { 1 -1 }
>>     { 0 1 }
>>     { 0 -1 }
>>     { -1 1 }
>>     { -1 0 }
>>     { -1 -1 }
>> }
>>
>> : neighbours ( loc -- neighbours )
>>   coordinates [ v+ ] with map ;
>>
>> 3) you might try factoring out the steps in "steps", perhaps something
>> like this:
>>
>> : count-cells ( cells -- counts )
>>     [ neighbours ] map concat histogram ;
>>
>> :: live? ( loc count cells -- ? )
>>     count 3 = [ t ] [ count 2 = loc cells in? ] if ;
>>
>> :: steps ( cells -- cells' )
>>     cells count-cells [ cells live? ] assoc-filter keys ;
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Learn Graph Databases - Download FREE O'Reilly Book
>> "Graph Databases" is the definitive new guide to graph databases and
>> their applications. This 200-page book is written by three acclaimed
>> leaders in the field. The early access version is available now.
>> Download your free book today! http://p.sf.net/sfu/neotech_d2d_may
>> _______________________________________________
>> Factor-talk mailing list
>> Factor-talk@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>
>>
>
>
> ------------------------------------------------------------------------------
> Get 100% visibility into Java/.NET code with AppDynamics Lite
> It's a free troubleshooting tool designed for production
> Get down to code-level detail for bottlenecks, with <2% overhead.
> Download for free and get started troubleshooting in minutes.
> http://p.sf.net/sfu/appdyn_d2d_ap2
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>


-- 
mvh/best regards Björn Lindqvist
http://www.bjornlindqvist.se/
------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite
It's a free troubleshooting tool designed for production
Get down to code-level detail for bottlenecks, with <2% overhead.
Download for free and get started troubleshooting in minutes.
http://p.sf.net/sfu/appdyn_d2d_ap2
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to