[Question]
  [
Print a continuous sinusoidal wave scrolling vertically on a terminal. The program should not terminate and should continuously scroll down the wave (except until it is somehow interrupted). You may assume overflow is not a problem (i.e. you may use infinite loops with incrementing counters, or infinite recursion).
The wave should satisfy the following properties:
* Amplitude = 20 chars (peak amplitude)
* Period = 60 to 65 lines (inclusive)
* The output should only consist of spaces, newline and `|`
* After each line of output, pause for 50ms
Sample output:
```
                    |
                      |
                        |
                          |
                            |
                              |
                                |
                                 |
                                   |
                                    |
                                     |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                       |
                     |
                   |
                 |
               |
             |
            |
          |
        |
       |
     |
    |
   |
  |
 |
 |
 |
 |
 |
 |
 |
  |
   |
    |
     |
      |
        |
         |
           |
             |
               |
                 |
                   |
                     |
                       |
                         |
                           |
                             |
                              |
                                |
                                  |
                                   |
                                    |
                                      |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                    |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                       |
                     |
                   |
                 |
               |
             |
           |
          |
        |
       |
     |
    |
   |
  |
 |
 |
 |
 |
 |
 |
 |
  |
   |
    |
     |
       |
        |
          |
            |
             |
               |
                 |
                   |
```
The above output should go on forever unless otherwise interrupted, e.g. SIGINT or SIGKILL, or closing terminal window, or you power off your machine, or the Sun swallows the Earth, etc.
Shortest code wins.
Note. I am aware of a similar problem on [Display Scrolling Waves](https://codegolf.stackexchange.com/questions/11940/display-scrolling-waves) but this isn't exactly the same. In my problem, the wave is not to be scrolled "in place" - just output it on a terminal. Also, this is an ascii-art problem, so don't use Mathematica to plot it.
  
[Answer]
## C, ~~74~~ ~~73~~ ~~70~~ ~~69~~ 67 characters
67 character solution with many good ideas from @ugoren & others:
```
i;main(j){main(poll(printf("%*c|\n",j=21+sin(i++*.1)*20,0),0,50));}
```
69 character solution with while loop instead of recursion:
```
i;main(j){while(printf("%*c|\n",j=21+sin(i++*.1)*20,0))poll(0,0,50);}
```
Approaching perl territory. :)
[Answer]
## APL (35)
(Yes, it does fit in 35 bytes, [here's a 1-byte APL encoding](http://www-03.ibm.com/systems/resources/systems_i_software_globalization_pdf_cp00907z.pdf))
```
{∇⍵+⌈⎕DL.05⊣⎕←'|'↑⍨-21+⌈20×1○⍵×.1}1
```
Explanation:
* `{`...`}`1: call the function with 1 at the beginning
* `1○⍵×.1`: close enough for government work to `sin(⍵×π÷30)`. (`1○` is `sin`).
* `-21+⌈20`: normalize to the range `1..40` and negate
* `'|'↑⍨`: take the last `N` characters from the string `'|'` (which results in a string of spaces with a `|` at the end
* `⎕←`: display
* `⌈⎕DL.05`: wait 50 ms and return `1`. (`⎕DL` returns the amount of time it actually waited, which is going to be close to `0.05`, rounding that value up gives `1`).
* `∇⍵+`: add that number (`1`) to `⍵` and run the function again.
[Answer]
# Mathematica ~~121 104 80 67~~ 64
```
n=1;While[0<1,Spacer[70 Sin[n Pi/32]+70]~Print~"|";[[email protected]](/cdn-cgi/l/email-protection); n++]
```
![sine](https://i.stack.imgur.com/JoFcE.png)
[Answer]
## Perl, 48 (68)
GNU sleep version: 48
```
print$"x(25+20*sin).'|
';$_+=.1;`sleep .05`;do$0
```
Cross platform: 68
```
use Time::HiRes"sleep";print$"x(25+20*sin).'|
';$_+=.1;sleep.05;do$0
```
Removed the use of Time::HiRes module by using shell sleep function.
Shortened increment as per Ruby example.
Shortened using $" and $0 seeing hints from Primo's work
Thanks for hints Primo.
[Answer]
## Perl - 64 (or 60) bytes
*The following uses a Windows-specific shell command:*
```
`sleep/m50`,print$"x(20.5-$_*(32-abs)/12.8),'|
'for-32..31;do$0
```
*The following uses a GNU/Linux-specific shell command:*
```
`sleep .05`,print$"x(20.5-$_*(32-abs)/12.8),'|
'for-32..31;do$0
```
Both at 64 bytes.
* Period is 64.
* Maximum amplitude is exactly 20.
* The curve is perfectly symmetric.
* Every period is identical.
```
                    |
                      |
                         |
                           |
                             |
                               |
                                |
                                  |
                                   |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                      |
                    |
                  |
               |
             |
           |
         |
        |
      |
     |
    |
   |
  |
 |
 |
|
|
|
|
|
 |
 |
  |
   |
    |
     |
      |
        |
         |
           |
             |
               |
                  |
                    |
                      |
                         |
                           |
                             |
                               |
                                |
                                  |
                                   |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                      |
                    |
                  |
               |
             |
           |
         |
        |
      |
     |
    |
   |
  |
 |
 |
|
|
|
|
|
 |
 |
  |
   |
    |
     |
      |
        |
         |
           |
             |
               |
                  |
                    |
```
Note that this isn't exactly a sinusoidal wave, but rather a quadratic interpolation. Plotted against an actual sin:
![](https://i.stack.imgur.com/j4coG.png)
At the granularity required, these are visually indistinguishable.
If the aesthetics aren't so important, I offer a **60 byte** alternative, with period length 62, maximum amplitude of ~20.02, and slight asymmetries:
```
`sleep/m50`,print$"x(20-$_*(31-abs)/12),'|
'for-31..30;do$0
```
[Answer]
# Ruby 56
```
i=0
loop{puts" "*(20*Math.sin(i+=0.1)+20)+?|;sleep 0.05}
```
[Answer]
# Befunge 98 - ~~103~~ 100
```
:1g:02p' \k:02gk,'|,a,$ff*:*8*kz1+:'<\`*
468:<=?ABDEFGGGHGGGFEDBA?=<:86420.,+)'&$#"!!! !!!"#$&')+,.02
```
Cheers for a program that does this, in a language without trigonometric capabilities; the first program in fact. The second line is simply data; the character corresponding with the ascii value of the sin, added to a space character.
**EDIT:** I saved 3 chars by not subtracting the space away; the sinusoid is translated 32 units to the right (which is valid).
Befunge also does not have a sleep command, or something similar. It would be nice to find a fingerprint, but I couldn't find one, so `ff*:*8*` pushes `8*225**2` (`405000`) and `kz` runs a noop that many times (well, that many times + 1). On windows command line with [pyfunge](http://mearie.org/projects/pyfunge/), this turns out to be about 50 milliseconds, so I say I'm good. Note: if anyone knows a good fingerprint for this, please let me know.
The last part of the code simply checks if the counter (for the data line) is past the data, if it is, the the counter is reset to 0.
I used [this](http://ideone.com/om176B) to generate the data.
---
# Taylor Series
Although this version is 105 chars, I just had to include it:
```
:::f`!4*jf2*-:::*:*9*\:*aa*:*:01p*-01g9*/a2*+\$\f`!4*j01-*b2*+:01p' \k:01gk,$'|,a,ff*:*8*kz1+:f3*`!3*j$e-
```
I was trying to shorten my program, and decided to look at the [taylor series for cosine](http://www.wolframalpha.com/input/?i=taylor%20series%20cos%28x%29) (sine is harder to calculate). I changed `x` to `pi * x / 30` to match the period requested here, then multiplied by `20` to match the amplitude. I made some simplifications (adjusted factors for canceling, without changing the value of the function by much). Then I implemented it. Sadly, it is not a shorter implementation.
```
:f`!4*jf2*-
```
checks whether the values of the taylor series are getting inaccurate (about `x = 15`). If they are, then I compute the taylor series for `x - 30` instead of `x`.
```
:::*:*9*\:*aa*:*:01p*-01g9*/a2*+
```
is my implementation of the taylor series at `x = 0`, when `x` is the value on the stack.
```
\$\f`!4*j01-* 
```
negates the value of the taylor series if the taylor series needed adjustment.
```
b2*+
```
make the cosine wave positive; otherwise, the printing would not work.
```
:01p' \k:01gk,$'|,a,
```
prints the wave
```
ff*:*8*kz1+
```
makeshift wait for 50 milliseconds, then increment `x`
```
:f3*`!3*j$e-
```
If `x` is greater than 45, change it to -14 (again, taylor series error adjustment).
[Answer]
# Python, ~~108~~,~~93~~,~~90~~,~~89~~,88
```
import math,time
a=0
while 1:print" "*int(20+20*math.sin(a))+"|";time.sleep(.05);a+=.1
```
Now with infinite scrolling :)
Edit: ok, 90. Enough?
Edit:Edit: no, 89.
Edit:Edit:Edit: 88 thanks to [boothby](https://codegolf.stackexchange.com/users/2180/boothby).
[Answer]
# PHP, 59 characters
```
<?for(;;usleep(5e4))echo str_pad('',22+20*sin($a+=.1)).~ƒõ;
```
[Answer]
# C64 BASIC, 64 PETSCII chars
![enter image description here](https://i.stack.imgur.com/se7Xb.png)
On a PAL C64, `For i=0 to 2:next i` cycles for approx. 0,05 seconds, so the delay time is respected.
[Answer]
# C - 86+3 characters
**Thanks shiona and Josh for the edit**
```
i;main(j){for(;j++<21+sin(i*.1)*20;)putchar(32);puts("|");usleep(50000);i++;main(1);}
```
~~i;main(j){for(j=0;j++<20+sin(i/10.)\*20;)putchar(32);puts("|");usleep(50000);i++;main();}~~
float i;main(j){for(j=0;j++<20+sin(i)\*20;)putchar(32);puts("|");usleep(50000);i+=.1;main();}
Compiled with the `-lm` flag, I assume I need to add 3 chars
[Answer]
## Javascript ~~88~~ ~~76~~ 78 characters
```
setInterval('console.log(Array(Math.sin(i++/10)*20+21|0).join(" ")+"|")',i=50)
```
Based on Kendall Frey's code.
[Answer]
**Ti-Basic, 33 bytes** 
```
While 1:Output(8,int(7sin(X)+8),"!":Disp "":π/30+X→X:End
```
**The following caveats exist**:
1. Due to screen limitation of 16x8, this sine wave only has an amplitude of 7 (period of 60 is still maintained)
2. Due to lack of an easy way to access the `|` char, `!` is used instead
3. Due to lack of an accurate system timer, the delay is not implemented. However, run speed appears approximately correct.
[Answer]
## JavaScript - 88
```
setInterval(function(){console.log(Array(Math.sin(i++/10)*20+21|0).join(" ")+"|")},i=50)
```
I'm sure someone can come up with something that's actually clever.
[Answer]
#  J - ~~103~~,~~58~~,~~57~~,54
Thanks to awesome guys from IRC
```
(0.1&+[6!:3@]&0.05[2:1!:2~' |'#~1,~[:<.20*1+1&o.)^:_]0
```
In words from right to left it reads: starting from 0 infinite times do: sin, add 1 ,multiply by 20, floor, append 1 (so it becomes array of 2 elements), copy two bytes ' |' correspondingly, print it, wait 0.05s and add 0.1
Instead of infinite loop we can use recursion, it would save 2 characters, but will also produce a stack error after some number of iterations
```
($:+&0.1[6!:3@]&0.05[2:1!:2~' |'#~1,~[:<.20*1+1&o.)0  
```
Where `$:` is a recursive call.
[Answer]
# Haskell - 75
```
main=putStr$concat["|\n"++take(floor$20+20*sin x)(repeat ' ')|x<-[0,0.1..]]
```
Unfortunately, I couldn't get the program to pause 50 ms without doubling my char count, so it just floods the console, but it does produce the sine wave.
---
Here's the full code with pausing (138 chars with newlines):
```
import GHC.Conc
import Control.Monad
main=mapM_(\a->putStr a>>threadDelay 50000)(["|\n"++take(floor$20+20*sin x)(repeat ' ')|x<-[0,0.1..]])
```
[Answer]
## Perl 6: 46 chars
```
sleep .05*say ' 'x(25+20*.sin),'|'for 0,.1...*
```
Create an infinite lazy Range using `0,0.1 ... *`, loop over that. `say` returns `Bool::True` which numifies as 1 in multiplication, this way I can keep it in a single statement.
[Answer]
fugly **Javascript - 77**
```
i=setInterval("console.log(Array(Math.sin(i+=.1)*20+20|0).join(' ')+'|')",50)
```
and if we do it in Firefox - **73**
```
i=setInterval("console.log(' '.repeat(Math.sin(i+=.1)*20+20|0)+'|');",50)
```
and if we're nasty - **67**
```
i=setInterval("throw(' '.repeat(Math.sin(i+=.1)*20+20|0)+'|');",50)
```
[Answer]
# Scala, ~~92~~,~~89~~,87
```
def f(i:Int){println(" "*(20+20*math.sin(i*.1)).toInt+"|");Thread sleep 50;f(i+1)};f(1)
```
[Answer]
## Python 3, 103
Stupid frikk'n imports...
```
import time,math
t=0
while 1:t+=(.05+t<time.clock())and(print(' '*int(20+20*math.cos(t*1.9))+'|')or.05)
```
Rather than "sleep", this implementation grinds at the cpu because python makes it easier to get a floating-point cpu clock than wall clock. This approach won't beat [friol's](https://codegolf.stackexchange.com/a/18640/2180), but it's fun so I'm leaving it up.
[Answer]
# C#
**[152]** Characters
```
namespace System{class P{static void Main(){for(var i=0d;;){Console.Write("{0,"+(int)(40+20*Math.Sin(i+=.1))+"}\n",'|');Threading.Thread.Sleep(50);}}}}
```
I could not get the Existing C# answer to Run and I couldn't downvote because I don't have enough Reputation
it was missing a couple of `{` and missing a `)` after the For Loop Declaration.
I figure that the variance in the look of the wave when it is run is because of the way we are trying to display this wave.
---
if we aren't counting the Namespace and the Method Declaration then it would be 
**[104]** characters for the working version
```
for(var i=0d;;){Console.Write("{0,"+(int)(40+20*Math.Sin(i+=.1))+"}\n",'|');Threading.Thread.Sleep(50);}
```
[Answer]
# VB [236][178]
not sure how you would count the tabs, I just took the count from Notepadd++ before I pasted here. **newlines are mandatory, probably why no one likes using it for code golfing.**
```
Module Module1
Sub Main()
Dim i
While True
Console.WriteLine("{0:" & (40 + 20 * Math.Sin(i = i + 0.1)) & "}", "|")
Threading.Thread.Sleep(50)
End While
End Sub
End Module
```
[Answer]
# C#
The Magic Line [91] Characters
```
for(var i=0d;;Console.Write("{0,"+(int)(40+20*Math.Sin(i+=.1))+"}\n",'|'))Thread.Sleep(50);
```
Working Program Below. [148] Characters
```
namespace System{class P{static void Main(){for(var i=0d;;Console.Write("{0,"+(int)(40+20*Math.Sin(i+=.1))+"}\n",'|'))Threading.Thread.Sleep(50);}}}
```
[Answer]
# Bash+bc (to do the math), 80
```
$ for((;;i++)){ printf "%$(bc -l<<<"a=20*s($i/10);scale=0;a/1+20")s|
";sleep .05;}
                |
                 |
                   |
                     |
                       |
                         |
                           |
                            |
                              |
                               |
                                |
                                 |
                                  |
                                   |
                                   |
                                   |
                                   |
                                   |
                                   |
```
[Answer]
# TI-BASIC, 30 bytes
Small size improvement over the other answer, at the cost of some accuracy. Note that TI-Basic technically has the | character, but you have to transfer it via computer or use an assembly program to access it.
```
While 1
Output(8,int(8+7sin(Ans)),":
Disp "
.03π+Ans
End
```
[Answer]
## Julia - 68
Edit: thanks to M L and ace.
```
i=0;while 0<1;println(" "^int(20sin(.1i)+20)*"|");i+=1;sleep(.05)end
```
Well, it can not compete vs APL, but here's my attempt.
Output:
```
                    |
                      |
                        |
                          |
                            |
                              |
                               |
                                 |
                                  |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                   |
                                  |
                                |
                              |
                             |
                           |
                         |
                       |
                     |
                   |
                 |
               |
             |
           |
         |
        |
      |
     |
    |
   |
  |
 |
|
|
|
|
|
 |
 |
  |
   |
     |
      |
       |
         |
           |
             |
              |
                |
                  |
                    |
                      |
                        |
                          |
                            |
                              |
                                |
                                 |
                                   |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                   |
                                 |
                                |
                              |
                            |
                          |
                        |
                      |
                    |
                  |
                 |
               |
             |
           |
         |
       |
      |
     |
   |
  |
  |
 |
|
|
|
|
|
 |
  |
  |
    |
     |
      |
        |
         |
           |
             |
               |
                 |
                   |
                     |
                       |
                         |
                           |
                            |
                              |
                                |
                                 |
                                   |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                  |
                                 |
                               |
                              |
                            |
                          |
                        |
                      |
                    |
                  |
                |
              |
            |
           |
         |
       |
      |
    |
   |
  |
 |
 |
|
|
|
|
|
 |
  |
   |
    |
     |
      |
        |
          |
           |
             |
               |
                 |
                   |
                     |
                       |
                         |
                           |
                             |
                              |
                                |
                                  |
                                   |
                                    |
                                     |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                  |
                                 |
                               |
                             |
                            |
                          |
                        |
                      |
                    |
                  |
                |
              |
            |
          |
         |
       |
      |
    |
   |
  |
 |
 |
|
|
|
|
 |
 |
  |
   |
    |
     |
       |
        |
          |
            |
             |
               |
                 |
                   |
                     |
                       |
                         |
                           |
                             |
                               |
                                |
                                  |
                                   |
                                    |
                                      |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                      |
                                      |
                                     |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                       |
                     |
                   |
```
[Answer]
# MATLAB, 81 bytes
```
t=0;while(fprintf('%s\n',i))i=[];t=t+1;i(fix(21+20*sind(t*6)))='|';pause(.05);end
```
I abused the fact that `i` is always initialized in MATLAB, which meant that I could put the `fprintf` in the `while` statement without initializing `i` first. This does mean the program first outputs an empty line, but I think this is not forbidden in the spec.
Furthermore, it abuses the fact that Matlab will ignore most ASCII control characters, printing a space instead of NULL (also for the first empty line).
[Answer]
## F# - ~~90~~ ~~79~~ ~~77~~ 76
Here's a solution using recursion
```
let rec f x=printfn"%*c"(int(20.*sin x)+21)'|';Thread.Sleep 50;f(x+0.1)
f 0.
```
It could probably be improved further.
[Answer]
## AutoHotkey 176
```
SetKeyDelay,-1
run Notepad.exe
WinWaitActive, ahk_class Notepad
p:=0
loop
{
sleep 50
p+=Mod(Floor(A_index/40),2)?-1:1,t:=""
loop % p
t .= " "
sendinput % t "|`n"
}
esc::Exitapp
```
Run the script . It opens Notepad and prints the characters. Press Esc anytime to exit.
[Answer]
**Clojure, 121**
Short version:
```
(loop[a 0](println(clojure.string/join(repeat(int(+ 20 (* 20 (Math/sin a)))) " ")) \|)(Thread/sleep 50)(recur(+ a 0.1)))
```
Pretty version:
```
(loop [a 0]
  (println (clojure.string/join (repeat (int (+ 20 (* 20 (Math/sin a)))) " ")) \|)    
  (Thread/sleep 50)
  (recur(+ a 0.1)))
```
Period is 64.
Type this into `lein repl` or save in file `sin.clj` and run with `lein exec sin.clj` (requires lein-exec plugin).
]