JavaScript / Cubix, 36 bytes
//.!v+u;$I^@O<.Iu
a=>eval(a.join`-`)
Try it!
The JavaScript function can be tested using the snippet below, the Cubix program can be tested here.
How does this work?
JavaScript
The first line is a line comment to JavaScript, since it starts with two slashes, so JavaScript only sees the bottom line (a=>eval(a.join`-`)
), which takes an array as input, joins it with minus signs inbetween, and then runs that as code, resulting in the subtraction of all elements in the array.
let f=
//.!v+u;$I^@O<.Iu
a=>eval(a.join`-`)
console.log(f([1,2,3,4,5]))
console.log(f([3,1,4,1,5]))
Cubix
Cubix sees the following cube (notice that Cubix ignores all whitespace):
/ / .
! v +
u ; $
I ^ @ O < . I u a = > e
v a l ( a . j o i n ` -
` ) . . . . . . . . . .
. . .
. . .
. . .
The Beginning
The IP starts on the third line, pointing east. It hits the 'I'
command, which takes a number from the input, and pushes it to the stack. Then, it is redirected by '^'
into the sum-loop.
Sum-loop
I removed all characters not part of the sum loop, and replaced them by no-ops ('.'
). The IP initally arrives on the second line, pointing east.
. . .
! v +
u ; $
. . . . . . I u . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . .
. . .
. . .
First, the '!'
command checks the top element. If that is 0
(i.e. we have reached the end of the input), the next instruction ('v'
) is executed, reflecting the IP out of the loop. If we have not yet reached the end of the input, we add the top two items together ('+'
, the second item is the sum up to that point, the top item the new input). Then, the IP wraps to another face of the cube, into the 'u'
character, which causes the IP to make a u-turn, and execute a 'I'
command (read another input integer), while pointing north. The IP wraps back to the top face, skips ('$'
) the delete instruction (';'
) and makes another u-turn, back to the point at which we started.
The End
If the IP is reflected out of the loop, the following characters are executed:
. . .
. v .
. ; .
. . @ O < . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . .
. . .
. . .
These instructions delete the top element (which is zero), and then output the top element (the sum) as an integer. Then the '@'
command is reached, so the program ends.
Is trailing whitespace in the output allowed? – Business Cat – 2017-04-18T13:10:34.597
Are two different versions of the same language allowed? See the Python 2 / 3 Answer by HyperNeutrino
– Mr. Xcoder – 2017-04-18T13:12:06.860@Mr.Xcoder it's allowed. – programmer5000 – 2017-04-18T13:24:35.880
@BasicSunset yes. You may also require a trailing newline in the input. – programmer5000 – 2017-04-18T13:41:55.770
Ugh, so many polyglot questions recently :P – Beta Decay – 2017-04-18T23:23:04.083
@BetaDecay is there...a problem?? – programmer5000 – 2017-04-18T23:38:39.117
Nah, just it'd be nice if there was a little more variation – Beta Decay – 2017-04-18T23:45:20.480
2@BetaDecay you think that's a problem, check out code-golf! – programmer5000 – 2017-04-18T23:46:32.977
1@programmer5000 You mean
[code-golf] -[polyglot]
? – Erik the Outgolfer – 2017-04-19T08:15:37.217