[Question]
  [
Your task is to write a program or function. Its output/return value must follow these rules:
* The program outputs `1`
* The program repeated `n` times outputs `n`
* The program backwards outputs `-1`
* The program backwards and repeated `n` times outputs `-n`
* A mix of forward and backward programs will output `number_forward - number_backward`
Take the program `aB` for example.
* `aB` should output `1`
* `aBaB` should output `2`
* `Ba` should output `-1`
* `BaBa` should output `-2`
* `aBBa` should output `0`
* `aBBaBa` should output `-1`
* `aaB`, `C`, or `​` would not need to be handled
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest in bytes per language wins.
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 2 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
NC
```
**[Try it online!](https://tio.run/##y0rNyan8/9/P@f9/AA "Jelly – Try It Online")** Or see all [test-cases](https://tio.run/##y0rNyan8///h7i1lj5rWGPz//9/PmcvPGUg4@wERkPBzhhLOfgA "Jelly – Try It Online").
### How?
In Jelly any sequence of monadic atoms forms a monadic chain which simply applies those monadic atoms in turn from left to right, starting with the value of it's left argument. When a full program is run with no arguments zeros are implicitly supplied as necessary, i.e. `main_link(0)` is called. As such `NC` is `C(N(0))` and `CN` is `N(C(0))`.
```
NCNC ... CN ... NC - Main Link: no arguments
N                  - negate((implicit)0)     -1 *  0 =  0
 C                 - complement(that)         1 -  0 =  1
  N                - negate(that)            -1 *  1 = -1
   C               - complement(that)         1 - -1 =  2
     ...           - 
     ...           -                             ... =  a
        C          - complement(a)            1 -  a =  1 - a
         N         - negate(that)       -1 * (1 - a) =  a - 1
            ...    - 
            ...    -                             ... =  b
               N   - negate(b)               -1 *  b = -b
                C  - complement(that)         1 - -b =  b + 1
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 3 bytes
I wonder if 2 bytes is possible in some language (I suspect that'd require it to consider the parity of the positions of the commands).
```
‘ḷ’
```
Explanation: `‘` increments, `’` decrements, and `ḷ` takes the left one, and for some magic reasons, when repeated, the program works as requested.
[Try it online!](https://tio.run/##y0rNyan8//9Rw8yHO7Y/apgBRGDGTLwMqOL//wE "Jelly – Try It Online")
[Answer]
# [Bash](https://www.gnu.org/software/bash/), 39
Thanks to:
* @pxeger for `s/EXIT|TIXE/0/`
* @Sisyphus for `s/0 # 0/0/`
```
trap echo\ $[++a] 0 ]a--[$ \ohce part
```
[Try it online!](https://tio.run/##jY/BCoMwEETv@xVzEISKsR8gvbXXfoB6WKMlUEokDW3/PrUidVNEetvZeTPLtnw3QbNHWSI9nk8pDrhY92TXqdEh73hAr42tkVRZxg32aDjPqwS1NbrHwM5TGJNErn@gFOmxqmV9nRXR54xwI3AWy26iRTyal2J54Kc/DszGn/xmNn7K3GwHzl7YTVoV4gdVSPZrrRKr5OZSmOEN "Bash – Try It Online")
[Answer]
# Polyglot [Japt](https://github.com/ETHproductions/japt), R, Applescript, 5 bytes
All credit for this one goes to @DanielSchepler. Daniel, if you open a duplicate answer to this, I will happily delete this one.
```
0+1-0
```
I don't know Japt at all, but this is just simple arithmetic evaluation. The result of the last evaluation is output at the end of the program by default. Japt was the first language that I found that satisfied this, but I'm sure there are others.
Forward (1): [Try it online!](https://tio.run/##y0osKPn/30DbUNfg/38A "Japt – Try It Online")
Forward-Backward-Backward-Forward-Forward-Backward-Backward (1):[Try it online!](https://tio.run/##y0osKPn/30DbUNfAQNdQG0qAuahi//8DAA "Japt – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/) `-0777p`, 13 bytes
```
++$_;#;_$--
```
[Try it online!](https://tio.run/##K0gtyjH9/59LW1sl3lrZOl5FV5fr//9/@QUlmfl5xf91fU31DAz/6xqYm5sXAAA "Perl 5 – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 4 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
¾¼.¾
```
[Try it online - 1x forward: `1`](https://tio.run/##yy9OTMpM/f//0L5De/QO7fv/HwA).  
[Try it online - 1x backwards: `-1`](https://tio.run/##yy9OTMpM/f//0D69Q3sO7fv/HwA).  
[Try it online - 3x forward: `3`](https://tio.run/##yy9OTMpM/f//0L5De/SABAr1/z8A).  
[Try it online - 3x backwards: `-3`](https://tio.run/##yy9OTMpM/f//0D69Q3sO7UOl/v8HAA).  
[Try it online - 1x forward + 1x backwards: `0`](https://tio.run/##yy9OTMpM/f//0L5De/SAxD69Q3sO7fv/HwA).  
[Try it online - 2x forward + 5x backwards + 1x forward: `-2`](https://tio.run/##yy9OTMpM/f//0L5De/SABIzSO7SHEAVW@f8/AA).
**Explanation:**
The *1x forward* program does the following:
```
¾     # Push the counter variable to the stack (0 by default)
 ¼    # Increment the counter variable
  .   # No-op, because `.¾` doesn't exist as 2-byte command, so it just ignores this
   ¾  # Push the counter variable to the stack (which is 1 now)
      # (implicitly output the top of the stack as result)
```
The *1x backward* program does the following:
```
¾     # Push the counter variable to the stack (0 by default)
 .¼   # Decrement the counter variable
   ¾  # Push the counter variable to the stack (which is -1 now)
      # (implicitly output the top of the stack as result)
```
Any combination of these two programs after one another increments and decrements the counter variable, but only the last value pushed to the stack is output implicitly at the very end.
[Answer]
# [R](https://www.r-project.org/), 26 bytes
```
F+
1->F#F=FF
FF=F#F>-1-
+F
```
[Try it online!](https://tio.run/##K/r/302by1DXzk3ZzdbNjcsNSCq72eka6nJpu/3/DwA "R – Try It Online")
[!enilno ti yrT](https://tio.run/##K/r/302bS9dQ185N2c3WzY3LDUgqu9npGnJpu/3/DwA "R – Try It Online")
[Twice backwards and four times forwards](https://tio.run/##K/r/302bS9dQ185N2c3WzY3LDUgqu9npGnJpu@GVwSKhS3WZ//8B "R – Try It Online")
A very different (and shorter) approach to [Giuseppe's R answer](https://codegolf.stackexchange.com/a/216464/86301) (in which he predicted that this answer was coming!).
`F` is initially a shorthand for `FALSE` and so gets coerced to `0`. When the program is run forwards, we use rightwards assignment to get `F=F+1` and `FF=F`. When concatenated with itself, the program becomes
```
F+
1->F#F=FF
FF=F#F>-1-
+FF+
1->F#F=FF
FF=F#F>-1-
+F
```
[Try it online!](https://tio.run/##K/r/302by1DXzk3ZzdbNjcsNSCq72eka6nJpu@GW@f8fAA "R – Try It Online")
and so the second time, we are doing `F=+FF+1` which also increments `F`.
When run backwards, the only difference is that we use `+-1` instead of `+1`.
When the final line is run, we print `+F`.
[Answer]
# [C++ (gcc)](https://gcc.gnu.org/), ~~324~~ ~~325~~ 323 bytes
Note the leading and trailing newlines.
Thanks to [Jonathan Allen](https://codegolf.stackexchange.com/users/53748/jonathan-allan) for spotting the need for a leading newline.
```
#ifndef M//
#define M(a,b)a##b//
#define W(z,x)M(z,x)//
#import<cstdio>//
int n;struct X{X(int d){n+=d;}};main(){printf("%d",n);}//
#endif//
X W(a,__LINE__)=1;//;1-=)__ENIL__,s(W X
//fidne#
//};)n,"d%"(ftnirp{)(niam;}};d=+n{)d tni(X{X tcurts;n tni
//>oidtsc<tropmi#
//)x,z(M)x,z(W enifed#
//b##a)b,a(M enifed#
//M fednfi#
```
[Try it online!](https://tio.run/##Tc4/y8IwEAbwvZ8iNAgJRoJzrJuDYFzbLaT583JDr6WNIJZ@9r6Jk8vx8IPn7tw0nf6c2/eKQkQfItFSVjQHwEA0s6LnltL@B1v2EW@uv7MwDNM4p4tbkofxmgUwEVRLml8ukW7tWAHPVzw2Xm2bGiwg4@s0Z4@sPvhaIFdb2RXQQ8yhy1esMOZxf96M4c1ZSanOp4Ybc3veH8aIhbWkq6SM4DHQHDbFUdT@ULOYEOZp5QzBDuWgb464ck@ys/wPSe41p0Vhgdy8juDT4i5pHqcByi7@Fh@mv7MlASEGX7in1PJeWKZ/UJMcMObevv8D "C++ (gcc) – Try It Online")
[aBaB](https://tio.run/##7dA9q8MgFAbgvb9CIgWlFuls061DoemabGI0Xs6QEzEWSkN@e6526n7XuxxeHjgfHBvC8cfabdtR8OgGTxopdzQHwIE0zIieG0r7L2zZW7x486mFYQxTTGc7JwfTJQtgIqjmFJ82kW7pWAHHFzzUTq2rGg0g40uI2T2r9q4SyNVaZg3owOfQ5S1GaH2/Pa5a8/qkpFSnY821vj5ud63FzFrS7aT04HCgOayKo6jcvmI@IcSwcIZgxrLQ1QdcuCPZWb6HJPuMaVZYIHdeJnBptucUpzBCmcVf4s2aT23JgOAHV7in1PBeGNZ8YUNyQJ/7/l/45xdu2y8 "C++ (gcc) – Try It Online") (2)
[Ba](https://tio.run/##Tc4/y8IwEAbwvZ8iNAgJRopzrJuDYFzbLaT583JDr6GNIJZ@9r6Jk8vx8IO752yMpz9r972iEND5QFTTVDQHQE8UM2LghtLhBzv2EW@uvrMwjHGa08UuycF0zQKYCMolzS@bSL/2rIDjKx5bJ7dNjgaQ8TXO2QOrD64WyOVWbnl0EHLoc8sitH7cnzeteXs6y6aR55ZrfXveH1oLwzrSV00TwKGnOWySo6jdoWYhIcxx5QzBjKXQtUdcuSPZWf6HJPua0yKxQN68TuDSYi9pnuII5RZ/iw9T39kRjxC8KzxQaviQq9UPKpIDhry37/8 "C++ (gcc) – Try It Online") (-1)
[BaBa](https://tio.run/##7ZC9isMwEIT7PIWwCEhEQaRWnC5FIE5rd8LWz7GF10JWIMT42X1SqvTXXjMMH8zMsiaE448x27aj4NE6TxopdzQbQEca1ouB95QOX7Blb/HizUcLhjFMMZ3NnCxMl0wAE0E1p/g0iXRLxwqwfMFDbdW6qrEHZHwJMXPPqr2tBHK1li6HFnw2XV6Zhdb32@OqNa@PJyWlOtVc6@vjdtda9Kwl3U5KDxYdzWZVHEVl9xXzCSGGhTOEfiyDtj7gwi3JnOV7SDLPmGaFBeTkZQKbZnNOcQojlC7@Em/WfLQlDsE7W/BAac@HPN18wYZkgz7n/l/45xdu2y8 "C++ (gcc) – Try It Online") (-2)
[aBBa](https://tio.run/##zZDBqoMwEEX3fkUwFBIaCV2ndtdFoXaruxAT85iFo2gKpeK326Rv4yd0M1wO3Jnh2HEs/qzdtoyCR9d5UkmZ0RgAO1IxI1puKG13sGZv8eLVdyYM/ThM4Wzn4GC4RAIYCKo5TE8bSLM0LAHHFzyWTq2r6g0g48s4Re5ZfnC5QK7WtKtDBz6GJl4xQuv77XHVmpcnJaU6FSXX@vq43bUWM6tJk0npwWFHY1gVR5G7Q858QJjGhTME06eDrjziwh2JnMV/SLDPKcwKE4jNywAuzPYcpmHsIe3iL/Fm1XfWpEPwnUu4pdTwVhhW7WBFYkAfe7@ncN4pLP4d7hSa31O4bR8 "C++ (gcc) – Try It Online") (0)
[BaaB](https://tio.run/##zZDBqoMwEEX3fkUwFBIaCV2ndtdFoXaruxAT85iFo2gKpeK326Rv4yd0M1wOzNzh2HEs/qzdtoyCR9d5UkmZ0RgAO1IxI1puKG13sGZv8eLVdyYM/ThM4Wzn4GC4RAIYCKo5TE8bSLM0LAHHFzyWTq2r6g0g48s4Re5ZfnC5QK7WdKtDBz6GJrbMQuv77XHVmpfFSUmpTiXX@vq43bUWhtWkyaT04LCjMayKo8jdIWc@IEzjwhmC6VOhK4@4cEciZ/EfEuxzCrPCBOLmZQAXZnsO0zD2kG7xl3iz6jtr0iH4ziXcUmp4G6urHaxIDOjj3u8pNDuF/waLncL59xRu2wc "C++ (gcc) – Try It Online") (0)
[aBBaBa](https://tio.run/##7ZDBioMwEIbvPkUwFBIaCT2n9tZDofaqtxATs8zBUTSFUvHZ3aR78RGWZS/Dzwf/zPDZcSy@rN22jIJH13lSSZnRGAA7UjEjWm4obXewZm/x4tVnJgz9OEzhbOfgYLhEAhgIqjlMTxtIszQsAccXPJZOravqDSDjyzhF7ll@cLlArta0q0MHPoYmXjFC6/vtcdWalyclpToVJdf6@rjdtRYzq0mTSenBYUdjWBVHkbtDznxAmMaFMwTTp4OuPOLCHYmcxX9IsM8pzAoTiM3LAC7M9hymYewh7eIv8WbVZ9akQ/CdS7il1PBWGFbtYEViQB97v0/hvFNY/DjcKTT/Cv@gwm37Bg "C++ (gcc) – Try It Online") (-1)
## Rundown
Comments are used to make the code legal in forward as well as reversed form.
```
#ifndef M//
...
#endif//
```
These are your basic guards to make sure we only define things once.
```
int n;struct X{X(int d){n+=d;}};main(){printf("%d",n);}//
```
Inside the guard we also declare the integer n, which will be automatically initialized to 0 by virtue of being a global, as well as define a struct X, whose constructor takes an integer value to add to the global n. The `main()` function simply prints the value of n.
`main()` is run after all global variables have been sorted out, so we use the declaring of globals as a way to alter that value of `n` before `main()` is entered.
This requires a way to declare an arbitrary amount of variables, leading to:
```
#define M(a,b)a##b//
#define W(z,x)M(z,x)//
```
The main magic of the stew: Helper macroes that allow the gluing of tokens together in a very useful way. Calling `W(a,b)` will yield the token `ab`, with the potent twist of evaluating `b` as a macro first, which makes these kinds of things possible:
```
#define M(a,b)a##b
#define W(z,x)M(z,x)
#define b 10
int W(a,b);          // This makes an int called "a10"
```
We combine this with the special preprocessor macro `__LINE__` which always expands to the current line number, allowing us a way to produce an arbitrary amount of variables having unique names.
```
X W(a,__LINE__)=1;//;1-=)__ENIL__,s(W X
```
Suppose we are at line 553; this will then declare a variable `a553` of type `X`, running its constructor with the argument 1. The reversed version is identical, save for using -1 instead.
Our preprocessor guards make sure the above line and its reverse are the only things repeated.
[Answer]
# [Nim](http://nim-lang.org/), 115 bytes
```
when not declared i:#1=-i
 var i=0;addQuitProc do:echo i#i ohce:od corPtiuQdda;0=i rav 
i+=1#:i deralced ton nehw
```
There is a leading and trailing newline.
The second line has a trailing space.
[Try it online!](https://tio.run/##Dc1BCoQwDAXQfU8R6HIY0G2ld9AjhCTQD04DoerxO17gvY7fnOlp1qn7IDU5OUwJJa/1i0Q3B6EuG6seF8YeLqReTJoTMsibWHEl8dgHrkOVt6WCgm9K@NQ1F7xs8CkvO/x9rD1pzj8 "Nim – Try It Online")
[Try it online!Try it online!Try it online!](https://tio.run/##5c3BCcMwDAXQu6f44GMoJFcH75CMYCSBBakFwknGd71HF3iv6XeM8FZpaNbBQldxYWiKW/5owFMcmte9MJ@39sONwJaEqkGjwipJMgaZH13vk7nsa1Z4eRB0yVtMOlkvF02223ykvuE/yjF@ "Nim – Try It Online")
[!enilno ti yrT](https://tio.run/##Dc1BCoQwDAXQfU8R6HIY0G2ld9AjhCTQD04DoerxO17gvY7fnOlp1qn7IDU5OUwJJa/1g0Q3B6EuG6seF8YeLqReTJoTMsibWHEl8dgHrkOVt6WCgm9K@NY1F7xs8CkvO/x9rD1pzj8 "Nim – Try It Online")
[!enilno ti yrT!enilno ti yrTTry it online!!enilno ti yrT](https://tio.run/##zc/BCYUwDIDhe6cI9CiCXivdQUcISaABbSBUHb@vO7yLC/wff9Wr9/AWqVCtAQud6MKgKa550gAPOmheNmQ@bm27GwFbEioGGhWskCRjIPO96X0w47ZkBccHgs55jUlH1vGkkW02HClv@BA5/01OH7rs/Qc "Nim – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), 41 bytes
```
$.+=1;END{exit!p$.}#}.$p!tixe{DNE;1=-.$
```
[Try it online!](https://tio.run/##KypNqvz/n0tFT9vW0NrVz6U6tSKzRLFARa9WuVZPpUCxJLMitdrFz9Xa0FZXT4Xr/38A "Ruby – Try It Online")
[Answer]
# [R](https://www.r-project.org/), ~~99~~ 91 bytes
```
l=readLines();`*`=grepl;1+sum("^l"*l-"^r"*l)#1-)r*'r^'-r*'l^'(mus;lperg=`*`;)(seniLdaer=r
```
[Try it online!](https://tio.run/##tc4xCoQwEIXh3mMkRTKRFKlDbuAZBgUHEUaRFzx/NouwneVWf/U@HprVAlnWaT@lespzmMsGuTSnsd6HN6wmaDSMHrIpEoIDu9ij7Pxx16yXYCt9mclXOfdpXQQFg8UL/YUdqwuIjtFDMVnSB9YfPKYXWtvwv9etfQA "R – Try It Online")
-8 bytes thanks to [Dominic van Essen](https://codegolf.stackexchange.com/users/95126/dominic-van-essen)!
[Tripled](https://tio.run/##1c4xCoQwEIXh3mMkxWQiKVKH3MAzDAoGEUaRFzx/NsvCdpZbbPVX7@OhWc0oyzrtZ6mO0@znvKFcmuJY78MZUeM1GEEP2xgYniAUelTIHXdNehVsuS8Tu1rOfVqXgozB4oF@wyRKHoEEPRyiZf3A@oXH@EBrG373@j/p1l4)
[Reversed](https://tio.run/##tcwxCoQwEEbhPscwRTKRFKlDbuAZBgUHEUaRP3j@bBZhO8utXvU@NKsFsqzTfkr1lOcwlw1yaU5jvQ8/sA5B48DoIZsiITiwiz3Kzh93zXoJttLPTL7KuU/rIigwFi/0F3asLiA6Rg/FZEkfWH/wmF5obeZvtGntAw)
[Forwards and Backwards](https://tio.run/##tc8xCoAwEETRPsdImh00RWrxBp5BEFxEWEUmeP6IhUXsU/7q8VmCjdRlnfZTs2BIXb4P2aiXiZ/N94b4Fd9CSBFg7zl7sUu5xbfsKznuPECynvu0LsqRLrAifgArgEBMAbAKsB/QpYqw4tpfuOYXrpQH)
Yet [another iteration](https://codegolf.stackexchange.com/a/177886/67312) on [third time the charm](https://codegolf.stackexchange.com/questions/157876/third-time-the-charm/157943#157943).
R has an odd but often useful (and not just for golfing) feature where data can be stored in the code, and read in as such. `readLines()` reads in lines until the end of the file. Then the `grepl` counts lines beginning with `l` (adding 1) and the ones beginning with `r` (adding `-1`), then incrementing/decrementing as appropriate: the first non-empty line is not counted as it's the only code that is run.
Starts/ends with a newline for reversing and concatenation purposes.
Possibly some `->` or `<-` chicanery will yield a shorter result -- indeed, Robin Ryder has [proven me right](https://codegolf.stackexchange.com/a/216483/67312), and it's shorter by a lot!
[Answer]
# [Brain-Flak (Rain-Flak)](https://github.com/DJMcMayhem/Brain-Flak), 16 bytes
```
({}())#)])([}{(
```
[Try it online!](https://tio.run/##SypKzMzTTctJzP7/X6O6VkNTU1kzVlMjurZag@v/fwA "Brain-Flak – Try It Online")
## Explanation
Making a program that when repeated counts up is easy
```
({}())
```
This adds 1 and with no input it defaults to zero, so repeating it counts the number of times.
However reversing is a little trickier. Only one balanced string is also a balanced string in reverse and that is the empty string, so we need to use some comments. We use the line comments of the Rain-Flak interpter and hide a payload between a `#` and a newline. Which simply is
```
({}[()])
```
This works the same way as the original program but with minus 1 instead of 1.
[Answer]
# Python IDLE, 5 bytes
```
0-~+0
```
[Try it online!](https://tio.run/##XU65DsIwDN3zFd5IVSpVYkOiW1lZ2KOSpmoEOeQmgi799RBCD8Qb7HfYsu3oeqMPIXRoFDDWeedRMAZSWYMOLErtoqu5k0aTNMVNK5acG2XlQ7DYVaNbQp59lHBFL44EIhyOX/LBACeQ2npHs@SJFxfWQX0514gGt8EbiuaeVHqA7qqq2u1hWNYEp3@X6ZBlIZTFlJck1ZnmUzHXma7Zr17zTb4B "Python 3 – Try It Online")
## Explanation
### Forwards
```
0      # zero
 -~    # minus-complement (the increment prefix "operator", which
       #                   also works as a binary add-plus-one)
   +0  # zero
```
### Concatenated
```
0           # zero
 -~         # plus one plus
   +00      # zero
      -~    # plus one plus
        +0  # zero
```
## Reversed
```
0      # zero
 +     # plus
  ~-   # complement-minus (the decrement prefix "operator")
    0  # zero
```
## Bonus variants
* `0-~-0`, which counts up regardless of flipping (in fact, it's palindromic);
* `0+~+0`, which counts down regardless of flipping.
[Answer]
# [Vyxal](https://github.com/Lyxal/Vyxal), 5 bytes
```
›#‹
```
[Try it Online!](http://lyxal.pythonanywhere.com?flags=&code=%0A%E2%80%BA%23%E2%80%B9%0A&inputs=&header=&footer=)
[Try it Online!Try it Online!](http://lyxal.pythonanywhere.com?flags=&code=%0A%E2%80%BA%23%E2%80%B9%0A%0A%E2%80%BA%23%E2%80%B9%0A&inputs=&header=&footer=)
[Try it Online!Try it Online!Try it Online!Try it Online!Try it Online!](http://lyxal.pythonanywhere.com?flags=&code=%0A%E2%80%BA%23%E2%80%B9%0A%0A%E2%80%BA%23%E2%80%B9%0A%0A%E2%80%BA%23%E2%80%B9%0A%0A%E2%80%BA%23%E2%80%B9%0A%0A%E2%80%BA%23%E2%80%B9%0A&inputs=&header=&footer=)
[!enilnO ti yrT](http://lyxal.pythonanywhere.com?flags=&code=%0A%E2%80%B9%23%E2%80%BA%0A%0A%E2%80%BA%23%E2%80%B9%0A%0A%E2%80%B9%23%E2%80%BA%0A&inputs=&header=&footer=)
[!enilnO ti yrTTry it Online!!enilnO ti yrT](http://lyxal.pythonanywhere.com?flags=&code=%0A%E2%80%B9%23%E2%80%BA%0A&inputs=&header=&footer=)
[Answer]
# [Haskell](https://www.haskell.org/), 35 bytes
```
main=print$0--1- 
 +1--0$tnirp=niam
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/PzcxM8@2oCgzr0TFQFfXUFeBS0HbUFfXQKUkL7OowDYvMzH3/38A "Haskell – Try It Online")
I tried for quite a bit to do this without comments. It doesn't seem impossible, but it does seem very hard.
[Answer]
# [Wolfram Language (Mathematica REPL)](https://www.wolfram.com/wolframscript/), 5 bytes
```
0+1-0
```
Test cases:
```
0+1-0            1
0+1-00+1-0       2
0-1+0           -1
0-1+00-1+0      -2
0+1-00-1+0       0
0+1-00-1+00-1+0 -1
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2gbXFKUmZcelFqQk5icGq3030DbUNfgv5KOUkyekq6dklKsNVc1V0i@a0VBUWpxcWZ@XnRirK2toQ6amI0dSNgITRhmeFlqUXEqUCNQia4hATU2dth0GWFaiEWZAWFV2I035Kr9/1@3AChRAgA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# ARM Thumb, 8 bytes excluding return statement
Raw machine code:
```
2101 1840 3901 2100
```
Assembly:
```
        .thumb
        @ R1 = 1
        movs    r1, #1
        @ r0 += 1
        adds    r0, r1
        @ Dummy code
        subs    r1, #1
        movs    r1, #0
```
Reverse each 16 bits
```
2100 3901 1840 2101
```
```
        .thumb
        @ r1 = 0
        movs    r1, #0
        @ r1 = -1
        subs    r1, #1
        @ r0 += -1
        adds    r0, r1
        @ Dummy code
        movs    r1, #1
```
Going forwards, it sets r1 to 1 and adds it to r0
Going backwards, it sets r1 to 0, subtracts 1 to get r1 to -1 (since Thumb can only load 0-255), then adds it to r0 effectively subtracting 1.
Call it with r0 = 0.
You need to end it with a return statement (`bx lr` or `4770` in hex) at the end, but those four instructions can be repeated forward and backwards as many times as you desire.
[Answer]
# ARM (NOT thumb), 16 bytes excluding return instruction
Second, entirely different answer, still 4 instructions though.
Machine code:
```
01 10 80 e3 01 00 80 e0 e0 80 00 01 e3 f0 10 00
```
This one, unlike my Thumb submission, is entirely different in that it is **byte** reversible (hence why it is split in 8 bits instead of 32 bits)
It happens to do basically the same thing, though, add 1 on forwards and -1 on backwards.
Forwards:
```
        movs      r1, #1
        add       r0, r0, r1
        smlatteq  r0, r0, r0, r8
        andseq    pc, r0, r3, ror #1
```
Backwards:
```
        movs      r1, #-1   @ a.k.a. mvns      r1, #0
        add       r0, r0, r1
        smlatteq  r0, r0, r0, r8
        tsteq     r0, r3, ror #1
```
You may be asking yourself, "`smlatteq`? `andseq`? `tsteq`? What the \*\*\*\* do those do?"
The answer is nothing unless the zero flag is set.
This code **heavily** abuses ARM's conditional execution and "s" instructions.
`movs` will set the condition flags on the value it sets (why this exists for the immediate form, I will never know). I set them to 1 on forwards, and -1 on backwards. These are both non-zero, so they **clear** the zero flag.
We also use the version of `add` which does not update the flags, so the zero flag remains clear.
Therefore, since the zero flag is not set, all instructions with the `eq` suffix will be ignored.
And that is a VERY good thing, because `ands pc, r0, r3, ror #1` would almost certainly crash the program (it would jump to a garbage address)
Therefore, in terms of what is effectively executed, it is this:
Forwards:
```
        movs      r1, #1
        add       r0, r0, r1
        nop
        nop
```
Backwards:
```
        movs      r1, #-1           @ a.k.a. mvns      r1, #0
        add       r0, r0, r1
        nop
        nop
```
Similar to my Thumb answer, you can repeat these as much as you wish, but you must put a `bx lr` (`e12fff1e`) at the end, and it is called with `r0 == 0`.
[Answer]
# [Python 3](https://docs.python.org/3/), ~~109~~ 107 bytes
Same approach as my zsh answer, but it actually beats the other Python answer!
```
c=open(__file__).read().count;print(c('w	')-c('y\t'))#))'	y'(c-)'t\w'(c(tnirp;tnuoc.)(daer.)__elif__(nepo=c
```
[Try it online!](https://tio.run/##DcYxCoRADADA2ndckaRwmyvFnwhBYuQWJAlLRPb1e1Yz0fPn9h1DVg81ZD7rpcxUmu4HUhG/LZdo1RIF4ZmA5te@JRB9iGDqgDIT5Pa8wbTaYkm7XQrhsWsrxKxXPZnRNHyVMf4 "Python 3 – Try It Online")
-2 bytes thanks to @Jakque
[Answer]
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), ~~77~~ 71 bytes
```
for i in(1,a:=0):#)a(tnirp ro i=-a 
 a+=i or print(a)#:)0=:a,1(ni i rof
```
[Try it online!](https://tio.run/##NU7LisMwELvnK4RzsUkKDXtZBnzxb5Q9DM10O5B1jGv6@PqskzYXaSSkQelVrnP8@k554QAPY8xymTMUGu3QM/mjo9axLVFzQp6h/sBowJ1X1GDKGotl15I7euJ@sLF2a/Cy1F@ngQ7DD9osf/NdUK6CSXjU@AuOI0pmnVYR5VFZmsB1A4cTrbWm2ZfAcDB9hW7jwBt0G1dz13vofVKDzzqJo9fOgGBcNUt@EeQpZyt3nqy61ZTnWVIhJL7dln8 "Python 3.8 (pre-release) – Try It Online") (include testcases)
## How it works
The starting code is this :
```
for i in(1,a:=0):#<...>
 a+=i or print(a)#<...>
```
At the first iteration of the `for` loop
* The `for` loop assigns `a` to `0` and `i` to `1`.
* since `i` is equal to `1`, `i or print(a)` won't evaluate the print and will be equal to 1
* `a+=` we increment a (become `a-=` in the reversed program)
At the second iteration of the `for` loop:
* The `for` loop assigns `i` to `0`.
* `i or print(a)` will print `a` since `i` is evaluated as `False` and return `None`
* it then crash the program (to avoid multiple printing) since we try to add `None` to an integer
When chained, the prgrams become :
```
for i in(1,a:=0):#<...>
 a+=i or print(a)#<...>for i in(1,a:=0):#<...>
 a+=i or print(a)#<...>
```
There is only one active `for` loop (the other one are commentated), and the incrementation (or decrementation when reversed) occurs once for each repetition of the program. The printing occurs only once beacause of the crash.
The reverse program is only changing `a+=` by `a-=`
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 16 bytes
```
υΣＩ⁰⎚¹±υ⊞υ¹⎚⁰ＩΣυ
```
[Try it online!](https://tio.run/##S85ILErOT8z5//9867nF7/esfNS44VHfrEM7D2083/qoa9751kM7gXygKFDu3OLzrf//AwA "Charcoal – Try It Online") Explanation:
```
υ
```
Print the list.
```
ΣＩ⁰
```
Cast 0 to string, then take the digital sum and print it (this happens to do nothing).
```
⎚
```
Clear the canvas.
```
¹
```
Print a `-`.
```
±υ
```
Print the negated list.
```
⊞υ¹
```
Push `1` to the list. (This is what actually affects the final output.)
```
⎚
```
Clear the canvas.
```
⁰
```
Print nothing.
```
ＩΣυ
```
Cast the sum of the list to string and print it. (This code at the end of the last copy is the only output that survives the program.)
[Try it reversed!](https://tio.run/##S85ILErOT8z5//9867nF7/esfNS44VHfrEM7z7c@6pp3vvXQxkM7gXygKFDu3OLzrf//AwA "Charcoal – Try It Online") Explanation: The middle instructions differ slightly when the code is reversed:
```
¹
```
Print a `-`.
```
υ
```
Print the list.
```
⊞υ±¹
```
Push `-1` to the list.
[Answer]
# [Python 3](https://docs.python.org/3/), 164 bytes
```
import atexit as a;a._clear();a.register(lambda:print(v));v=locals().get('v',0)+1#;1-)0,'v'(teg.)(slacol=v;))v(tnirp:adbmal(retsiger.a;)(raelc_.a;a sa tixeta tropmi
```
**[Try it online!](https://tio.run/##DcfRCoMwDEDRXxH2YMK2ouzN4rdIrMEVoi1pKO7rXZ/uufln33R@7jseOal1ZHzFltKRJ7cEYVLARuU9FmMFoWPdaMoaT4OK6OssKZAUQLezQV/714DP8eHHNw6vtmC8O4QiFJLM1SNWsDNqnmhbDxJQthJ3VkceQYklLI3UFeosXmwtmvIR7/sP "Python 3 – Try It Online")** Or use [this program builder](https://tio.run/##PU/RasMwDHzvV5juwVLXhpa9JeR1H5DXMYqaapmLExtZNdnXZw7rCoI73engFH/0O0xvy9KHK7fW2o0bYxA1pDy7AslQQ9W590wCWKjw4JKygKfxcqU6ipsUMmKTWx968gmwGljBZrs/4uvppTkd8LgvKygPFULy1Aff5gYxg05OYk3Xy0gehDW5gaWiBkGIfX8ulEwio25mLSAhjm6zFo0SBqGxlK5uwU2wfvBR13A44W4HuW1tZ/HTfAUx2bipTLwrIG7@Gm8f@Xr7rzyE54FwuntdfZ65f9rL8t51vw "Python 3 – Try It Online") (Input `F` for forward and `R` for reverse).
[Answer]
# [Zsh](https://www.zsh.org/), 91 bytes
```
<<<$[`grep -c wx $0`-`grep -c y\z $0`];:<<'Q'
'Q'<<:;]`0$ zy c- perg`-`0$ x\w c- perg`[$<<<
```
[Try it online!](https://tio.run/##qyrO@P/fxsZGJTohvSi1QEE3WaG8QkHFIEEXzq@MqQIJxFpb2dioB6pzAbGNjZV1bIKBikJVpUKyrkJBalE6UAOQXxFTDheIVgGa@/8/AA "Zsh – Try It Online")
## Explanation
The overall idea is to read the source file and count the number of occurrences of the code forwards, minus the number backwards.
```
     `grep -c wx $0`                           # count occurrences of `wx`
                     `grep -c y\z $0`          # count occurrences of `yz`*
   $[               -                ]         # subtract
<<<                                            # print
                                      ;        # then
                                       :       # do nothing,
                                        <<'Q'  # with the rest of the file**
```
`*`: The backslash in the middle prevents the call to grep itself from being counted as an instance. In the mirrored copy on the next line, the backslash is placed inside `wx` instead, so that when re-reversed, it matches the negative one instead.
`**`: This starts a [here-document](https://google.com?q=zsh+heredoc), which consumes as input the rest of the file until the first line containing only `FW`. The single-quotes prevent execution of any `$[...]` and ``...`` parts from any possible repeated sections afterwards. We can't use line comments because `grep -c` outputs the number of matching lines, not individual matches. In another language we won't have this limitation.
The whole program is then duplicated, with the `\` inside the `wx` instead of the `yz`, is then reversed and put on the next line, so that it works as expected when the first instance is a reversed one.
[Answer]
# [Klein](https://github.com/Wheatwizard/Klein) 000, 12 bytes
```
\@+
 -
1
+@\
```
[Try it online!](https://tio.run/##y85Jzcz7/z/GQZtLQZfLkEvbIeb///8GBgYA "Klein – Try It Online")
**Reversed**
```
\@+
1
- 
+@\
```
[Try it online!](https://tio.run/##y85Jzcz7/z/GQZvLkEtXgUvbIeb///8GBgYA "Klein – Try It Online")
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 31 bytes
```
tnirP//1-eniL$--
$Line++//Print
```
Try it:
* [forward](https://tio.run/##y00syUjNTSzJTE78/78kL7MoQF/fUDc1L9NHRVeXS8UnMy9VW1tfP6AoM6/k/38A)
* [2x forward](https://tio.run/##y00syUjNTSzJTE78/78kL7MoQF/fUDc1L9NHRVeXS8UnMy9VW1tfP6AoM6@EgPT//wA)
* [backward](https://tio.run/##y00syUjNTSzJTE78/78kL7MoQF9fWzs1L9NHhUtXV8UnMy9V11BfP6AoM6/k/38A)
* [2x backward](https://tio.run/##y00syUjNTSzJTE78/78kL7MoQF9fWzs1L9NHhUtXV8UnMy9V11BfP6AoM6@EgPT//wA)
* [forward, backward](https://tio.run/##LckxCgAgCAXQvXO0yUe6R0NXkBByyCG8v0H01rcllm4Jm5IZbmcwN6hbr0Cp3VyJmMcxj99ErwvwGu135gU)
* [forward, 2x backward](https://tio.run/##y00syUjNTSzJTE78/78kL7MoQF/fUDc1L9NHRVeXS8UnMy9VW1tfP6AoM68EKq2tDZbm0tUFS@saEif9/z8A)
A full program. `$Line` has an initial value of `1`. In a notebook, it would increment for each input expression, but this doesn't apply outside the REPL.
[Answer]
# JavaScript, ~~131~~ ~~121~~ 122 bytes
* -10 thanks to [Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld)
* +1 I forgot that the newline would also be reversed, so another one needed to be added
```
clearTimeout(this.a);a=setTimeout(console.log,0,b=-~this.b)//)b.siht-~=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc;
```
(notice the leading and trailing newlines. the stacksnippets removes some of the leading and trailing newlines for the entire snippet, but it still works with/without the first and last newlines)
Try It Right Here! (TryItOnline has an error where `clearTimeout` is not defined, but `clearTimeout` is a valid JavaScript function.)
aB
```
clearTimeout(this.a);a=setTimeout(console.log,0,b=-~this.b)//)b.siht-~=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc
```
aBaB
```
clearTimeout(this.a);a=setTimeout(console.log,0,b=-~this.b)//)b.siht-~=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc
clearTimeout(this.a);a=setTimeout(console.log,0,b=-~this.b)//)b.siht-~=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc
```
Ba
```
clearTimeout(this.a);a=setTimeout(console.log,0,b=~-this.b)//)b.siht~-=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc
```
BaBa
```
clearTimeout(this.a);a=setTimeout(console.log,0,b=~-this.b)//)b.siht~-=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc
clearTimeout(this.a);a=setTimeout(console.log,0,b=~-this.b)//)b.siht~-=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc
```
aBBa
```
clearTimeout(this.a);a=setTimeout(console.log,0,b=-~this.b)//)b.siht-~=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc
clearTimeout(this.a);a=setTimeout(console.log,0,b=~-this.b)//)b.siht~-=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc
```
aBBaBa
```
clearTimeout(this.a);a=setTimeout(console.log,0,b=-~this.b)//)b.siht-~=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc
clearTimeout(this.a);a=setTimeout(console.log,0,b=~-this.b)//)b.siht~-=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc
clearTimeout(this.a);a=setTimeout(console.log,0,b=~-this.b)//)b.siht~-=b,0,gol.elosnoc(tuoemiTtes=a;)a.siht(tuoemiTraelc
```
[Answer]
# Python 3, 112 bytes
```
import os#1=-KO_R.so
os.system('cls')#)5-KO_R.so(tnirp
print(os.R_OK-3)#)'slc'(metsys.so
os.R_OK+=1#so tropmi
```
very ugly, but works.
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 104 bytes
```
var i=-~i,y=-~y;setTimeout(_=>--i||console.log(y))//))y(gol.elosnoc||i-->=_(tuoemiTtes;y-~=y,i~-=i rav
```
[Try it online!](https://tio.run/##bY1BCoMwEEX3nsJlQk08gMRlT9BdKTqxg4xopiRRCASvbu2mUCh8Pvz3Fn@CDcLg6RWV4yceYEtT9kexgS/JqJ2qdHZqAsYbLchrFJ1plaKcB3aBZ9QzjyJJWddSJjHyrHHm4HjImZRqTSfiyrjQLWJoktpNqmhXhkoPW3H0TWHhvLxrrcE@tMcNfUAh9cTk@lNfVzdEYifASiF/9sXCH/TJ1x1v "JavaScript (Node.js) – Try It Online")
Using V8(`print`) or browser(`alert`) saves some bytes
[Answer]
# Ruby, 60 bytes
Note trailing newline:
```
a||=0;a+=1;at_exit{p a;exit!}#}!tixe;a p{tixe_ta;1=-a;0=||a
```
[Try it online!](https://tio.run/##KypNqvz/P7GmxtbAOlHb1tA6sSQ@tSKzpLpAIdEaxFCsVa5VLMmsSLVOVCioBjHiSxKtDW11E60NbGtqErn@/wcA "Ruby – Try It Online")
Reversed version (leading newline):
```
a||=0;a-=1;at_exit{p a;exit!}#}!tixe;a p{tixe_ta;1=+a;0=||a
```
[Try it reversed!](https://tio.run/##KypNqvz/nyuxpsbWwDpR19bQOrEkPrUis6S6QCHRGsRQrFWuVSzJrEi1TlQoqAYx4ksSrQ1ttROtDWxrahL//wcA "Ruby – Try It Online")
`at_exit` specifies a block to be executed upon `exit`, `SystemExit` exception, or uncatchable error. `exit!` specifically bypasses these, giving the ability to skip all but the first registered block.
[Answer]
# [><>](https://esolangs.org/wiki/Fish), 19 bytes
## Forwards
```
l\ v
;l-n
n+l;
v \l
```
[Try it online!](https://tio.run/##S8sszvj/PydGoYzLOkc3jytPO8eaq0whJuf/fwA "><> – Try It Online")
## Backwards
```
l\ v
;l+n
n-l;
v \l
```
[Try it online!](https://tio.run/##S8sszvj/PydGoYzLOkc7jytPN8eaq0whJuf/fwA "><> – Try It Online")
Works a lot like [my klein solution](https://codegolf.stackexchange.com/a/216515/56656), but it has to deal with the finickiness of ><>.
]