- How are ragged arrays possible?
- How can I have more than two arguments to a function?
- How do simply change the nth element of an array??
- What's the difference between bqn.js and CBQN?
How are ragged arrays possible?
Consider:
"hi"‿"world"
⟨ "hi" "world" ⟩
>"hi"‿"world"
Error: >: Incompatible element shapes (encountered shapes ⟨2⟩ and ⟨5⟩)
at >"hi"‿"world"
^
≠"hi"‿"world"
2
≠¨"hi"‿"world"
⟨ 2 5 ⟩
≢"hi"‿"world"
⟨ 2 ⟩
>"hello"‿"world"
┌─
╵"hello
world"
┘
≢"hello"‿"world"
⟨ 2 ⟩
≢>"hello"‿"world"
⟨ 2 5 ⟩
What is this ⟨ 2 ⟩ array that contains two differently-shaped arrays, and how is it different from a ⟨ 2 5 ⟩ array? Are these just boxes?
It's a unit array. For this purpose it's similar to a box as in J, but is less of an exceptional type - it's still an array. J:
'hi';'world'
┌──┬─────┐
│hi│world│
└──┴─────┘
(<'hi'),<'world'
┌──┬─────┐
│hi│world│
└──┴─────┘
# each 'hi';'world'
┌─┬─┐
│2│5│
└─┴─┘
*/# each 'hi';'world'
|domain error, executing dyad *
|x is boxed and y is boxed
| */#each'hi';'world'
#>1 { 'hi';'world'
5
vs. BQN:
(<"hi")∾<"world"
⟨ "hi" "world" ⟩
"hi"‿"world"
⟨ "hi" "world" ⟩
(<"hi")∾<"world"
⟨ "hi" "world" ⟩
≠¨ (<"hi")∾<"world"
⟨ 2 5 ⟩
×´≠¨ (<"hi")∾<"world"
10
≠>1⊏"hi"‿"world"
5
New to BQN, I experienced unit arrays as mysterious •Show differences:
5
5
<5
┌·
· 5
┘
5+<5
┌·
· 10
┘
5+5
10
⊑<5
5
NB. bqncrate ?q=atom
How can I have more than two arguments to a function?
First of all, I want to emphasize that _join_ is not a function, but a 2-modifier, therefore it is not a function with four arguments. Functions only have one or two arguments.
# adapted from strings.bqn's _join
_join_ ← {∾1↓⥊(<𝕗)≍˘𝕩 ; ∾𝕨‿𝕗‿𝕩‿𝕘}
(2-modifier block)
"hello" ", "_join_"!" "world"
"hello, world!"
Mainly, use destructing headers or your own destructuring to separate logical parameters from arrays passed in.
┌ Manhattan1 ← { 𝕊 ⟨ax,ay,bx,by⟩: (|bx-ax) + |by-ay }
│ Manhattan2 ← { 𝕊 ⟨⟨ax,ay⟩,⟨bx,by⟩⟩: (|bx-ax) + |by-ay }
│ Manhattan3 ← { 𝕊 [⟨ax,ay⟩,⟨bx,by⟩]: (|bx-ax) + |by-ay }
└ Manhattan4 ← { 𝕊 [a,b]: +´|b-a }
(function block)
Manhattan4 [0‿0,5‿¯5]
10
Manhattan3 [0‿0,5‿5]
10
Manhattan2 ⟨0‿0,5‿5⟩
10
Manhattan1 0‿0‿5‿5
10
(The referenced strings.bqn is from bqn-libs.)
How do simply change the nth element of an array??
Use ⌾ (Under).
100⌾(3⊸⊑) ↕5
⟨ 0 1 2 100 4 ⟩
100⌾(¯1⊸⊑) ↕5
⟨ 0 1 2 3 100 ⟩
100⌾⊑ ↕5
⟨ 100 1 2 3 4 ⟩
√⌾(3⊸⊑) ↕5
⟨ 0 1 2 1.7320508075688772 4 ⟩
In particular, 2d arrays work exactly like this with y‿x coordinates, indexed from the top-left of the array. I got the idea that this didn't work and wrote some truly incredible unnecessary workarounds.
5‿5⥊'.'
┌─
╵".....
.....
.....
.....
....."
┘
'o'⌾(1‿2⊸⊑)5‿5⥊'.'
┌─
╵".....
..o..
.....
.....
....."
┘
What's the difference between bqn.js and CBQN?
CBQN is faster, has a superior CLI interface, and has superior integration with systems languages across the C ABI, both outbound from BQN with •FFI, and inbound with libcbqn.
Of bqn.js, first of all, there are two versions: docs/bqn.js for integration into a website, and bqn.js in the root directory of the BQN repo which uses node and has more CLI scripting utility.
Both versions of bqn.js have superior integration with JavaScript and are very easily extended through additional system-provided values - for example, this site's repl.js provides its own •HashMap which is not present by default. docs/bqn.js is notably also missing •Import