3 ms·
Language has to be an important factor here. If I re-write this in APL --and you know APL-- reading the solution is pretty linear: R ← data BETWEEN limits
by robomartin 14y ago
Language has to be an important factor here. If I re-write this in APL --and you know APL-- reading the solution is pretty linear:
R ← data BETWEEN limits
R ← ((data>limits[1])∧(data<limits[2]))/data
R ← a COMMON b
R ← (∨/a ∘.= b)/a
x ← 2 8 7 9 ¯5 0 2
y ← 1 ¯3 10 0 8 9 1
x_btwn ← x BETWEEN 2 10
y_btwn ← y BETWEEN ¯2 9
xy_common ← x_btwn COMMON y_btwn
If you know APL the above pretty much reads like the palm of your hand.
Since APL is unknown to most, here's a quick explanation.
R ← data BETWEEN limits
Dyadic function declaration. Takes two arguments.
R ← ((data>limits[1])∧(data<limits[2]))/data
Let's break this up:
(data>limits[1])
Takes the "data" vector and compares it to the first element in "limits", which happens to be the "low" limit. You get a binary vector as the result with a "1" anywhere the comparison is true and "0" otherwise.
If "limits" is 2 10:
0 1 1 1 0 0 0 0
Now:
(data<limits[2])
Does the same thing with the upper limit:
1 1 1 1 1 1 1 1
Then:
0 1 1 1 0 0 0 0 ∧ 1 1 1 1 1 1 1 1
Performs a logical AND of the two binary vectors, resulting in a new vector:
0 1 1 1 0 0 0 0
Finally:
R ← 0 1 1 1 0 0 0 0/data
Selects elements from the "data" vector based on the values in the binary vector and returns the result vector:
8 7 9
Anyhow, that's why I think that language is important. If I wrote this in Forth the pattern would be very different and the thought process required to understand it more convoluted. Probably true as well for assembly.