Scala, 49 bytes
(a:Seq[Int])=>a.zip(a.reverse)map{case(a,b)=>a+b}
Lambda. Paste in the REPL, it gets assigned to a default variable, like res0
or whatever. Note the variable name from the output produced after pasting that line into the REPL and pressing Enter, let's say it's res4
. To call the lambda, just use res4(Seq(1, 2, 3))
or res4(Seq(89, 88, 1, 2, 3, 1000, 2000))
for the test cases.
Implementation notes:
zip
creates a Tuple2
from 2 Seq
s, which need to be unpacked in the pattern match (the case
expression after the map
) before the elements can be added. Indexed access is convoluted for golfing in Scala...
Here's another version, using convoluted indexing tricks for 57 bytes:
(a:Seq[Int])=>{val l=a.size-1;0.to(l)map(i=>a(i)+a(l-i))}
This just produces a new Seq
from adding the "complementary" elements of the Seq
s (symmetrically about the mid of the Seq
. Very imperative.
I don't like it either, but the type ascriptions are necessary to get the REPL to accept it, otherwise it just botches out Missing Parameter Type
.
The idea in the 2nd version can be used in Java 8 as well, just swapping in IntStream.rangeClosed(0,l)
instead of Scala's implicit to
on Int
s, unlike the former, which would be considerably more convoluted in Java.
Note that @jkelm's answer for C# can be trivially translated to Java.