Skip to content

Commit

Permalink
swapped monadic ↑ and ⊃
Browse files Browse the repository at this point in the history
  • Loading branch information
- committed Aug 22, 2017
1 parent f15320d commit 1d9b1a6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
27 changes: 16 additions & 11 deletions apl.js
Expand Up @@ -8,17 +8,21 @@ const prelude=[
// (2 2⍴⍳4)~2 !!! RANK ERROR
"~←~⍠{(~⍺∊⍵)/⍺}",
"_atop←{⍶⍹⍵;⍶⍺⍹⍵}", // (-⍟)2 3←→-⍟2 3 ⍙ 2(-*)3←→-2*3
// ⊃3←→3 ⍙ ⊃(1 2)(3 4)←→2 2⍴1 2 3 4 ⍙ ⊃(1 2)(3 4 5)←→2 3⍴1 2 0 3 4 5 ⍙ ⊃1 2←→1 2 ⍙ ⊃(1 2)3←→2 2⍴1 2 3 0
// ⊃1(2 3)←→2 2⍴1 0 2 3 ⍙ ⊃2 2⍴1(1 1 2⍴3 4)(5 6)(2 0⍴0)←→2 2 1 2 2⍴1 0 0 0 3 4 0 0 5 6 0 0 0 0 0 0 ⍙ ⊃⍬←→⍬
// ⊃2 3 0⍴0←→2 3 0⍴0 ⍙ ⍬⊃3←→3 ⍙ 2⊃'pick'←→'c' ⍙ (⊂1 0)⊃2 2⍴'abcd'←→'c' ⍙ 1⊃'foo' 'bar'←→'bar' ⍙ 1 2⊃'foo' 'bar'←→'r'
// ↑3←→3 ⍙ ↑(1 2)(3 4)←→2 2⍴1 2 3 4 ⍙ ↑(1 2)(3 4 5)←→2 3⍴1 2 0 3 4 5 ⍙ ↑1 2←→1 2 ⍙ ↑(1 2)3←→2 2⍴1 2 3 0
// ↑1(2 3)←→2 2⍴1 0 2 3 ⍙ ↑2 2⍴1(1 1 2⍴3 4)(5 6)(2 0⍴0)←→2 2 1 2 2⍴1 0 0 0 3 4 0 0 5 6 0 0 0 0 0 0 ⍙ ↑⍬←→⍬
// ↑2 3 0⍴0←→2 3 0⍴0
// ⍬⊃3←→3 ⍙ 2⊃'pick'←→'c' ⍙ (⊂1 0)⊃2 2⍴'abcd'←→'c' ⍙ 1⊃'foo' 'bar'←→'bar' ⍙ 1 2⊃'foo' 'bar'←→'r'
// (2 2⍴0)⊃1 2 !!! RANK ERROR ⍙ (⊂2 1⍴0)⊃2 2⍴0 !!! RANK ERROR ⍙ (⊂2 2⍴0)⊃1 2 !!! RANK ERROR
// (⊂2 2)⊃1 2 !!! RANK ERROR ⍙ (⊂0 2)⊃2 2⍴'ABCD' !!! INDEX ERROR
"⊃←{0=⍴⍴⍵:↑⍵ ⋄ 0=×/⍴⍵:⍵ ⋄ shape←⍴⍵ ⋄ ⍵←,⍵ ⋄ r←⌈/≢¨shapes←⍴¨⍵ ⋄ max←↑⌈/shapes←(⍴↓(r⍴1)∘,)¨shapes",
" (shape,max)⍴↑⍪/shapes{max↑⍺⍴⍵}¨⍵",
" ;",
" 1<⍴⍴⍺:↗'RANK ERROR' ⋄ x←⍵",
" {1<⍴⍴⍵:↗'RANK ERROR' ⋄ ⍵←,⍵ ⋄ (⍴⍵)≠⍴⍴x:↗'RANK ERROR' ⋄ ∨/⍵≥⍴x:↗'INDEX ERROR' ⋄ x←⊃⍵⌷x}¨⍺",
" x}",
"↑←{",
" 0=⍴⍴⍵:⊃⍵ ⋄ 0=×/⍴⍵:⍵ ⋄ shape←⍴⍵ ⋄ ⍵←,⍵ ⋄ r←⌈/≢¨shapes←⍴¨⍵ ⋄ max←⊃⌈/shapes←(⍴↓(r⍴1)∘,)¨shapes",
" (shape,max)⍴⊃⍪/shapes{max↑⍺⍴⍵}¨⍵",
"}⍠↑",
"⊃←⊃⍠{;",
" 1<⍴⍴⍺:↗'RANK ERROR' ⋄ x←⍵",
" {1<⍴⍴⍵:↗'RANK ERROR' ⋄ ⍵←,⍵ ⋄ (⍴⍵)≠⍴⍴x:↗'RANK ERROR' ⋄ ∨/⍵≥⍴x:↗'INDEX ERROR' ⋄ x←⊃⍵⌷x}¨⍺",
" x",
"}",
// a←' this is a test '⋄(a≠' ')⊂a←→'this' 'is' (,'a') 'test'
"⊂←⊂⍠{1<⍴⍴⍺:↗'RANK ERROR' ⋄ 1≠⍴⍴⍵:↗'NONCE ERROR' ⋄ ⍺←,⍺=0",
" keep←~1 1⍷⍺ ⋄ sel←keep/⍺ ⋄ dat←keep/⍵",
Expand Down Expand Up @@ -1358,7 +1362,8 @@ voc._substitute=args=>{
return A(r,y.s)
}

voc['↑']=(y,x)=>x?take(y,x):first(y)
voc['↑']=(y,x)=>x?take(y,x):nyiErr()
voc['⊃']=(y,x)=>x?nyiErr:first(y)

// 2↑⎕a←→'AB' ⍙ ¯3↑⎕a←→'XYZ' ⍙ 5↑'abc'←→'abc ' ⍙ ¯5↑'abc'←→' abc' ⍙ 3↑⍳2←→0 1 0 ⍙ ¯1↑⍳4←→,3
// ⍴1↑(2 2⍴⍳4)(⍳10)←→,1 ⍙ 2↑1←→1 0 ⍙ 2 ¯2↑1 1⍴1←→2 2⍴0 1 0 0 ⍙ 3 3↑1 1⍴'a'←→3 3⍴'a '
Expand Down Expand Up @@ -1391,7 +1396,7 @@ const take=(x,y)=>{
return A(r,s)
}

// (1 2 3)(4 5 6)←→1 2 3 ⍙ (1 2)(3 4 5)←→1 2 ⍙ 'ab'←→'a' ⍙ 123←→123 ⍙ ⍬←→0
// (1 2 3)(4 5 6)←→1 2 3 ⍙ (1 2)(3 4 5)←→1 2 ⍙ 'ab'←→'a' ⍙ 123←→123 ⍙ ⍬←→0
const first=x=>{let y=x.a.length?x.a[0]:getProt(x);return y.isA?y:A([y],[])}

voc['⍉']=(y,x)=>{
Expand Down
2 changes: 1 addition & 1 deletion examples/5-rule30.apl
Expand Up @@ -4,4 +4,4 @@
r30
n50
tr82
' #'[{,t[2¨3,/0,0,]}nz,1,zn0]
' #'[{,t[2¨3,/0,0,]}nz,1,zn0]
14 changes: 7 additions & 7 deletions examples/6-queens.apl
Expand Up @@ -5,10 +5,10 @@ queens←{ ⍝ The N-queens problem.
search{ Search for all solutions.
():0 stitched: abandon this branch.
0=:rmdups all done: solution!
(hd tl)()(1) head 'n tail of remaining ranks.
(hd tl)()(1) head 'n tail of remaining ranks.
next,¨hd possible next steps.
remshd free¨tl unchecked squares.
,/next ¨rems ... in following ranks.
,/next ¨rems ... in following ranks.
}

cvex(1+)ׯ1 0 1 Checking vectors.
Expand All @@ -18,15 +18,15 @@ queens←{ ⍝ The N-queens problem.
rmdups{ Ignore duplicate solution.
rots{{}\4/} 4 rotations.
refs{{}\2/} 2 reflections.
best{()} best (=lowest) solution.
all8,refs¨rots all 8 orientations.
best{()} best (=lowest) solution.
all8,refs¨rots all 8 orientations.
(best all8)(,) ignore if not best.
}

fmt{ Format solution.
chars'·⍟'[().=] char array of placed queens.
expd1,0 1 expansion mask.
¨↓↓expd\chars vector of char matrices.
chars'·⍟'[().=] char array of placed queens.
expd1,0 1 expansion mask.
¨↓↓expd\chars vector of char matrices.
}

squares(÷2),1 initial squares
Expand Down

0 comments on commit 1d9b1a6

Please sign in to comment.