g | x | w | all
Bytes Lang Time Link
041AWK250910T174411Zxrs
009Pyt230218T135006ZKip the
034Zsh230214T122954Zroblogic
022Perl 6170723T203540ZSean
033Arturo230214T064453Zchunes
008MATL170723T023850ZLuis Men
010Japt170723T094942ZShaggy
063C++170723T135340ZHatsuPoi
056D170904T164542ZAdalynn
038Ruby170724T191247ZAlex
01605AB1E170724T180024ZMagic Oc
012J170723T032009ZJonah
005Jelly170723T033112ZDennis
050C# .NET Core170724T164415Zjkelm
0638th170723T200003ZChaos Ma
034Math.JS170723T230916ZATaco
013Dyalog APL170723T140756ZUriel
019Mathematica170723T141224ZJungHwan
018LOGO170723T023740Zuser2027
015CJam170723T102039ZErik the
011Pyth170723T060915Zdeltaeps
026Haskell170723T033249ZWheat Wi
030Pari/GP170723T034117Zalephalp
040Python 2170723T033213Zxnor
026Haskell170723T033310Zuser4594
042Python 2170723T023105Zmusicman
022Mathematica170723T022634Zuser2027
011Actually170723T024951Zuser4594
024R170723T022824ZGiuseppe
016Jelly170723T023017Zhyperneu
028JavaScript ES6170723T022928ZETHprodu

AWK, 41 bytes

{for(;i++<NF;)print$i+(1~i%4?1:i%2?-1:0)}

Attempt This Online!

Pyt, 9 bytes

ĐŁřπ*₂šƖ+

Try it online!

Đ            implicit input; Đuplicate
 Ł           get Łength
  ř          řangify
   π*₂       multiply by pi/2
      š      take šin
       Ɩ     cast to Ɩnteger
        +    add the two arrays element-wise

Zsh, 34 bytes

for i;echo $[++j%2*(j%4>2?-1:1)+i]

Try it online!

Perl 6, 28 22 bytes

{((1+0i,×i...)Z+$_)».re}

(*Z-(i*i,*i...*))».re

Try it online!

i*i, *i ... * produces an infinite list of the numbers -1, -i, 1, i repeateded in a cycle. Those numbers are zipped with subtraction (Z-) with the input list (*), and then the real components of the resulting complex numbers are extracted (».re).

Arturo, 33 bytes

$=>[i:0map&=>[&-((i%4)-1)%2'i+1]]

Try it

Port of ETHproductions' JavaScript answer.

MATL, 11 8 bytes

Jyn:^Yj+

Try it at MATL Online!

Explanation

J     % Push 1j (imaginary unit)
      % STACK; 1j
y     % Implicit input. Duplicate from below
      % STACK: [-4 3 0 1 7 9 8 -2 11 -88], 1j, [-4 3 0 1 7 9 8 -2 11 -88]
n     % Number of elements
      % STACK: [-4 3 0 1 7 9 8 -2 11 -88], 1j, 10
:     % Range
      % STACK: [-4 3 0 1 7 9 8 -2 11 -88], 1j, [1 2 3 4 5 6 7 8 9 10]
^     % Power, element-wise
      % STACK: [-4 3 0 1 7 9 8 -2 11 -88], [1j -1 -1j 1 1j -1 -1j 1 1j -1]
Yj    % Imaginary part
      % STACK: [-4 3 0 1 7 9 8 -2 11 -88], [1 0 -1 0 1 0 -1 0 1 0]
+     % Add, element-wise. Implicit display
      % STACK: [-3 3 -1 1 8 9 7 -2 12 -88]

Japt, 11 10 bytes

Takes advantage of Japt's index wrapping.

Ë+[1TJT]gE

Test it


Explanation

Implicit input of array U.

Ë

Map over the array.

+

To the current element add...

gE

The element at the current index (E)...

[1TJT]

In the array [1,0,-1,0].

C++, 93 85 83 63 bytes

auto w=[](auto&i){for(int j=0;j<i.size();j+=2)i[j]+=j%4?-1:1;};

-8 bytes, thanks to this answer, i discovered that lambda parameters can be auto and you can pass with the correct parameter, it will work

-2 bytes thanks to Nevay

-2 bytes thanks to Zacharý

I removed the vector include. You will need to pass as argument to w a container that respect the following conditions :

STL Containers that respect the following conditions are array, vector, string, map, unordered_map, and maybe others

If outputting by modifying arguments arguments is not allowed, then :

C++, 112 110 bytes

#include<vector>
std::vector<int>w(std::vector<int>i){for(int j=0;j<i.size();j+=2)i[j]+=(j%4)?-1:1;return i;}

D, 56 bytes

void w(T)(T[]i){for(T j;j<i.length;j+=2)i[j]+=j%4?-1:1;}

Try it online!

This is a port of HatsuPointerKun's C++ answer, so don't forget about them!

Ruby, 38 bytes

->a{a.zip([1,0,-1,0].cycle).map &:sum}

Try it online!

05AB1E, 16 bytes

vy3L2.SR0¸«Nè+})

Try it online!


3L2.SR0¸« is the shortest thing I can think of for sin(x % 4) in 05AB1E.

J, 12 bytes

+1 0 _1 0$~#

Try it online!

Because J's shape operator $ fills cyclically, when we shape it to the length # of the input, it does exactly what we want, and we can just add it to the input ]

Jelly, 5 bytes

Jı*Ċ+

Try it online!

How it works

Jı*Ċ+  Main link. Argument: A (array)

J      Indices; yield [1, ..., len(A)].
 ı*    Elevate the imaginary unit to the power 1, ..., len(A), yielding
       [0+1i, -1+0i, 0-1i, 1+0i, ...].
   Ċ   Take the imaginary part of each result.
    +  Add the results to the corresponding elements of A.

C# (.NET Core), 50 bytes

n=>{for(int i=0;i<n.Length;i+=2)n[i]+=i%4<1?1:-1;}

Try it online!

Uses a simple lambda. Modifies the original array and returns the output through reference.

8th, 96 63 bytes

Code

a:new swap ( swap 90 * deg>rad n:cos int + a:push ) a:each drop

This code leaves resulting array on TOS

Usage and examples

ok> [0,0,0,0,0] a:new swap ( swap 90 n:* deg>rad n:cos n:int n:+ a:push ) a:each drop .
[1,0,-1,0,1]

ok> [-4,3,0,1,7,9,8,-2,11,-88] a:new swap ( swap 90 * deg>rad n:cos int + a:push ) a:each drop .
[-3,3,-1,1,8,9,7,-2,12,-88]

Explanation

We use cos(x) in order get the right sequence [1,0,-1,0]. Each array element's index is multiplied by 90 degrees and then it is passed to cos() function to get the desired "wave factor" to be added to the corresponding item.

: f \ a -- a
  a:new    \ create output array
  swap     \ put input array on TOS
  \ array element's index is passed to cos in order to compute
  \ the "wave factor" to add to each item
  ( swap 90 n:* deg>rad n:cos n:int n:+ 
  a:push ) \ push new item into output array 
  a:each
  drop     \ get rid of input array and leave ouput array on TOS
;

Math.JS, 34 bytes

f(k)=k.map(j(x,y,z)=x+im(i^y[1]))

Explained

f(k)=k.map(j(x,y,z)=x+im(i^y[1]))
f(k)=                               # Define a function f, which takes argument k.
     k.map(                     )   # Map k to a function
           j(x,y,z)=                # Function j. Takes arguments x, y, and z. Where x is the item, y is the index in the form [i], and z is the original list.
                      im(      )    # The imaginary component of...
                         i^y[1]     # i to the power of the index.
                    x+              # x +, which gives our wave.

Try it Online!

Dyalog APL, 13 bytes

⊢+1 0 ¯1 0⍴⍨≢

Try it online!

How?

1 0 ¯1 0 - the array [1, 0, -1, 0]

⍴⍨≢ - reshape to the length of the input, cyclic

⊢+ - vectorized sum with the input

Mathematica, 19 bytes

i=1;#+Im[i*=I]&/@#&

Explanation

i=1;#+Im[i*=I]&/@#&
i=1;                 (* set variable i to 1 *)
               /@#   (* iterate through the input: *)
    #+Im[i   ]&      (* add the imaginary component of i... *)
          *=I        (* multiplying i by the imaginary unit each iteration *)

Note: i=1 appears outside of the function, which is okay per this meta consensus.

LOGO, 18 bytes

[map[?+sin 90*#]?]

There is no "Try it online!" link because all online LOGO interpreter does not support template-list.

That is a template-list (equivalent of lambda function in other languages).

Usage:

pr invoke [map[?+sin 90*#]?] [-4 3 0 1 7 9 8 -2 11 -88]

(invoke calls the function, pr prints the result)

prints [-3 3 -1 1 8 9 7 -2 12 -88].

Explanation (already pretty understandable):

 map[?+sin 90*#]?       map a function over all the items of the input
              #         the 1-based index of the element in the input
       sin 90*#         equal to the required wave
     ?                  looping variable
     ?+sin 90*#         add the wave to the input

CJam, 15 bytes

l~_,,[1TW0]f=.+

Try it online!

Pyth, 11 bytes

.e+bss^.j)k

Try it online!

Haskell, 26 bytes

@Mego beat me to this solution

zipWith(+)$cycle[1,0,-1,0]

Try it online!

This is what Haskell is great at. This declares a point-free function that zips the input with an infinite list.

Haskell, 56 bytes

Here's a solution that uses complex numbers. Not very competitive because of the import but never the less pretty cool.

import Data.Complex
zipWith((+).realPart.((0:+1)^))[0..]

Try it online!

Pari/GP, 30 bytes

a->[a[x]+imag(I^x)|x<-[1..#a]]

Try it online!

Python 2, 40 bytes

i=2
for x in input():print~-x+i%5%3;i*=2

Try it online!

Haskell, 26 bytes

zipWith(+)$cycle[1,0,-1,0]

Try it online! (runs all test cases)

Explanation:

zipWith(+)$cycle[1,0,-1,0]  -- anonymous tacit function
zipWith(+)                  -- pairwise addition between input list
          $cycle[1,0,-1,0]  -- and an infinitely-cycling "wave" list

Python 2, 50 42 bytes

Saved 8 bytes thanks to @Sisyphus!

lambda l:map(sum,zip(l,[1,0,-1,0]*len(l)))

Try it online!

53 bytes

lambda l:[int(x+(1j**i).real)for i,x in enumerate(l)]

Try it online!

Mathematica, 26 23 22 bytes

Im[I^Range@Tr[1^#]]+#&

Try it online! (Mathics)

Note: The TIO link is for the 23-byte version, the 22-byte version is not Mathics-compatible.

Actually, 11 bytes

;r⌠╦½*C≈⌡M¥

Try it online! (runs all test cases)

Explanation:

;r⌠╦½*C≈⌡M¥
;r           range(len(input))
  ⌠╦½*C≈⌡M   for each value in range:
   ˫*C      cos(pi/2*value)
       ≈     floor to integer
          ¥  pairwise addition of the input and the new list

R, 29 24 bytes

(l=scan())+Im(1i^seq(l))

Try it online!

Jelly, 16 bytes

-1Jm2$$¦+2Jm4$$¦

Try it online!

heh I'm sure this is too long

Edit

I know a 5 byte solution is possible but my wifi appears to be starting to cut me off so I'll golf this tomorrow. If someone posts the short Jelly solution before I can golf this, that's fine with me; I'll just keep this here for reference as to how bad I am at Jelly lol another way of doing it. I mean, I could just look at the link Phoenix posted in the comments, but since I'm still learning, I don't want to look at the solution until I've figured it out myself. This might cost me reputation but the learning is what I'm here for :)))

JavaScript (ES6), 28 bytes

a=>a.map((x,i)=>x-(i%4-1)%2)

The calculation goes like this:

i%4  -1  %2
0    -1  -1
1     0   0
2     1   1
3     2   0

The last bit taking advantage of the fact that in JS, a negative number when modulated will retain its negative sign (i.e. -5 % 3 -> -2, instead of 1 as it would be in Python).