This question is a follow-up to this previous question of mine. Assuming I understood correctly what is outlined in this meta post.
I wrote (and now, re-wrote) a function that takes as input a vector with two integers between 1 and 8 representing a position in a chessboard. This function should output a vector where each cell is a similar vector of integers, with the positions that a knight in the input position could reach.
E.g. for the input 1 1, my function should output (2 3)(3 2).
KnightMovesRevised ← {
⍝ Monadic function, expects a vector with 2 integers, e.g. (1 1)
⍝ Given a chessboard position, find the legal knight moves.
⍝ Returns vector of 2-integer vectors, e.g. (2 3)(3 2)
⍝ aux train to check if position is inside chessboard
isInsideBoard ← ∧/(1∘≤∧≤∘8)
signs ← ¯1 1 ∘., ¯1 1
offsets ← (1 2)(2 1)
moves ← , signs ∘.× offsets
⍝ list all the locations the knight could go to
locations ← moves + ⊂⍵
⍝ and keep the valid ones
valid ← isInsideBoard¨ locations
valid/locations
}
Changes
From the previous version to this one, I
- Reformatted code a bit with a suggested naming convention, by naming an auxiliary train and adding a couple more comments;
- Removed the train used to write
offsets
, which I had used just to give a go at tacit programming. This is such a small vector that I think it makes more sense to hardcode it; - Rewrote
signs
by writing twice the¯1 1
and removing,
,⍨
. This made it slightly easier to digest and not much more annoying to type;
These changes were motivated by the two great reviews I got (here and here) and I was hoping I could get reviews on those changes, because I tried to adhere to their suggestions but I didn't necessarily agree with all of them.
Questions
(paired with the above)
- Are the extra comments ok or are they too much?
- Are
signs
andoffsets
defined in an acceptable way? I like the trade-off between hardcoding too much and using too many functions just to create a couple of constants. - What is the standard spacing notation around
¨
? Should I writef¨ arg
,f ¨ arg
,f ¨arg
orf¨arg
?