So in [https://forum.nim-lang.org/t/5855](https://forum.nim-lang.org/t/5855) 
@juancarlospaco shared a website [https://code-golf.io](https://code-golf.io)

It's a website for competitive code golfing and it supports Nim!

So if someone considers it fun and has free time, we can try making new 
solutions, and, only if you want to, share the solutions (CodeGolf doesn't 
allow you to see other people's solutions) or the tricks for making Nim code 
smaller here.

Some tricks:

  * I've learned it from 
[https://forum.nim-lang.org/t/5855#36306](https://forum.nim-lang.org/t/5855#36306),
 but you can use a%%b instead of a mod b, %% is actually different from mod (it 
should be used for unsigned ints only, but it works if both arguments are 
positive)
  * <1 is shorter than ==0 if you don't care about negative
  * You don't need spaces around .. or ..< in for a in 0..<5
  * You don't need to specify first argument in case of using .. - for x in ..4 
\- 
[https://nim-lang.org/docs/system.html#..%2CT](https://nim-lang.org/docs/system.html#..%2CT)
  * **You can replace indentation with `;` and parentheses** (although be aware 
that in some cases it doesn't reduce save char count) , for example:



Before: 
    
    
    proc a(x:int):int=
     result=5
     if x>5:
      if x<3:
       if x==0:
        echo x
        echo x-1
     elif x==0:
      echo 0
      echo 5
     else:return 5
    
    
    Run

After: 
    
    
    proc b(x:int):int=(result=5;(if x>5:(if x<3:(if x==0:(echo x;echo 
x-1)))elif x==0:(echo 0;echo 5) else:return 5))
    
    
    Run

15 characters less

And of course, **don 't follow these tips in your real projects**

Reply via email to