Queued, not asked for — the freeze is the point and this is feature-shaped.
Filing it because five programs in a row wanted the same five lines.
There is no way to put two arrays together. += appends one element, and
handing it an array is refused:
$ cat j.lux
var a = [1, 2]
let b = [3, 4]
a += b
$ lux run j.lux
error: cannot add [int] to an array of int
--> j.lux:3:1
3 | a += b
^^^^^^
help: `lux learn arrays` — an array holds one type, so += has to match it
Verified against 0.19.2. The workaround is the same every time:
func joined(first: [int], second: [int]) -> [int] {
var both: [int]
for item in first {
both += item
}
for item in second {
both += item
}
return both
}
Where it came up
Writing a Texas Hold'em tutor, which needs a player's two cards and the board's
three, four, or five as one array before it can be scored, and needs it again
for the opponent, and again for every hand it deals in simulation. Also in a
maze, a hangman, and a rock-paper-scissors bot — anywhere a program builds one
list out of two it already has.
Every one of those wrote joined and moved on. It is not a hard function. What
it is, is the same function five times, which is the shape of a gap.
The obvious fix is the wrong one
a += b should stay refused. lux has arrays of arrays, and there the meaning
splits in two:
var grid: [[int]] = [[1, 2], [3, 4]]
let row = [5, 6]
grid += row // append a row? or glue two grids?
Appending is what it means today and what a grid-building loop needs. If +=
also concatenated, that line would be ambiguous in a way no error message could
resolve, and the failure would be silent — the program runs, the grid is the
wrong shape, and the learner is debugging geometry. A language that refuses to
coerce a string into a number should not guess here either.
So the ask, if it is ever worth anything, is a named built-in rather than an
operator:
let all = joined(hole, board)
It reads as what it does, it cannot be confused with appending, and it goes
where length and split already live — split makes an array out of one
string, and this makes one array out of two.
The case against, which is real
Every built-in is a word a beginner has to meet, and the loop it replaces is
five lines a beginner should be able to write. Writing joined by hand is a
genuinely good exercise: it is a for loop, an accumulator, and a return, which
is most of what the functions card teaches. A language this size earns its size
by leaving that alone.
The counter is that the exercise stops being an exercise the second time you
write it, and by the fifth it is noise in the program that wanted the answer. My
own reading is that this sits just under the bar — worth recording, not worth
the clock.
Queued, not asked for — the freeze is the point and this is feature-shaped.
Filing it because five programs in a row wanted the same five lines.
There is no way to put two arrays together.
+=appends one element, andhanding it an array is refused:
Verified against 0.19.2. The workaround is the same every time:
Where it came up
Writing a Texas Hold'em tutor, which needs a player's two cards and the board's
three, four, or five as one array before it can be scored, and needs it again
for the opponent, and again for every hand it deals in simulation. Also in a
maze, a hangman, and a rock-paper-scissors bot — anywhere a program builds one
list out of two it already has.
Every one of those wrote
joinedand moved on. It is not a hard function. Whatit is, is the same function five times, which is the shape of a gap.
The obvious fix is the wrong one
a += bshould stay refused. lux has arrays of arrays, and there the meaningsplits in two:
Appending is what it means today and what a grid-building loop needs. If
+=also concatenated, that line would be ambiguous in a way no error message could
resolve, and the failure would be silent — the program runs, the grid is the
wrong shape, and the learner is debugging geometry. A language that refuses to
coerce a string into a number should not guess here either.
So the ask, if it is ever worth anything, is a named built-in rather than an
operator:
It reads as what it does, it cannot be confused with appending, and it goes
where
lengthandsplitalready live —splitmakes an array out of onestring, and this makes one array out of two.
The case against, which is real
Every built-in is a word a beginner has to meet, and the loop it replaces is
five lines a beginner should be able to write. Writing
joinedby hand is agenuinely good exercise: it is a
forloop, an accumulator, and a return, whichis most of what the functions card teaches. A language this size earns its size
by leaving that alone.
The counter is that the exercise stops being an exercise the second time you
write it, and by the fifth it is noise in the program that wanted the answer. My
own reading is that this sits just under the bar — worth recording, not worth
the clock.