Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I cant speak for J but this is the first APL program I ever wrote (it's in Dyalog) it take a wolfram code on it's right and an array of 1s and 0s on the left and returns the next generation for that one dimensional automaton (it treats the world as wrapping but it would be easy to clamp the sides to 1 or 0):

  ODAT ← {(⊂[1] ¯1 0 1 ⌽ (3,⍴⍵)⍴⍵) ∊ ((8 ⍴ 2) ⊤ ⍺) / ⊂[1] 2 2 2 ⊤ ⌽ 0,⍳7 }
as a traditional function with comments and stuff its:

  NG←R ODAT S;N;P;PS

   ⍝ First construct the eight *P*ossible states:
   ⍝ Creates an array 7 6 5 4 3 2 1 0 (⌽0,⍳7) and converts to binary (2 2 2⊤)
   ⍝ 1 1 1 1 0 0 0 0
   ⍝ 1 1 0 0 1 1 0 0
   ⍝ 1 0 1 0 1 0 1 0
   ⍝ Then encloses along vertical axis (⊂[1])
   P←⊂[1]2 2 2⊤⌽0,⍳7

   ⍝ Convert the *R*ule from wolfram number to binary ((8⍴2)⊤R)
   ⍝ 110 becomes 0 1 1 0 1 1 1 0
   ⍝ Then choose *P*ropogating *S*tates (/P)
   PS←((8⍴2)⊤R)/P

   ⍝ Take *S*tate and triplicates ((3,⍴S)⍴S) and rotates (¯1 0 1⌽) to create a matrix
   ⍝ each colume is a cell and it's *N*eighbors
   ⍝ A state 1 0 1 1 0 0 becomes:
   ⍝ 0 1 1 0 0 1
   ⍝ 1 0 1 1 0 0
   ⍝ 0 1 1 0 1 0
   ⍝ Then encloses vertically again
   N←⊂[1]¯1 0 1⌽(3,⍴S)⍴S

   ⍝ Now we have two arrays: 
   ⍝ *N* which is the current generation of state/neighbor triplets
   ⍝ *PS* which is the triplets that produce a live cell in the *N*ext *G*eneration.
   ⍝ we create a new array by testing for membership (∊) of current generation
   ⍝ and *P*ropogating *S*tates
   NG ← N∊PS
Some of that might be idiomatic python but the thing is I didn't write it as the multi-line function. I wrote it by modifying the one line function. Which I did because that's how APL's data-flowy syntax/semantics encourages you write things and doing that in one line sure as hell isn't idiomatic python.

The determined programmer can write any language in any language. It's what we write when we aren't being determined fortran/smalltalk/whatever programmers that speaks to the difference in programming languages.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: