17
\$\begingroup\$

What general tips do you have for golfing in Nim? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to Nim (e.g. "remove comments" is not an answer).

Please post one tip per answer.

asked Nov 26, 2016 at 11:29
\$\endgroup\$

6 Answers 6

10
\$\begingroup\$

Flexible call syntax

Nim is pretty flexible when it comes to function call syntax. For example, here are some ways to call a function with one argument:

ord(c)
ord c
c.ord

And ways to call a function with two arguments:

max(a,b)
a.max(b)
a.max b

Choose the golfiest version that works right for your situation, especially regarding precedence. For example, compare:

abs(n)+2
n.abs+2
(abs n)+2

As opposed to:

abs(n+2)
(n+2).abs
abs n+2
answered Nov 26, 2016 at 12:26
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Note that max a,b even works (sometimes). \$\endgroup\$ Commented Nov 26, 2016 at 18:49
  • \$\begingroup\$ You can also drop the space when followed by a " (quotation mark): len"Hello world!" \$\endgroup\$ Commented Oct 5, 2022 at 22:46
10
\$\begingroup\$

Use the future module

The future module contains two main byte-saving features: lambdas and list comprehensions. Lambdas are extremely useful.

For example, this:

proc f(s:any):any=s&", world!"

can be shortened to this:

import future
s=>s&", world!"

which saves a byte. Note, however, that lambdas can't be used outside of a parameter list -- so to test your code, you'll have to do something like this:

import future
proc test(f: string -> string) = echo f "Hello"
test(s=>s&", world!")

As well, list comprehensions can be used with the future module. For example, this code prints a seq (@[...]) of all squares less than 100 divisible by 4:

import future
echo lc[x*x|(x<-1..9,x*x mod 4==0),int]
answered Nov 26, 2016 at 12:15
\$\endgroup\$
5
  • 1
    \$\begingroup\$ For a fairer comparison it should be noted that you can sometimes use any instead of string (I'm assuming you chose the longest type name), but this still saves regardless. \$\endgroup\$ Commented Nov 26, 2016 at 23:43
  • \$\begingroup\$ @Sp3000 I didn't know you could use any, thanks for the tip! You should post that as an answer. \$\endgroup\$ Commented Nov 27, 2016 at 11:55
  • \$\begingroup\$ For an even better comparison, you can do proc(s:any):any=s&", world!", dropping the <space>f for an anonymous proc \$\endgroup\$ Commented Dec 20, 2016 at 9:57
  • 1
    \$\begingroup\$ The name has been changed to sugar, and future has been deprecated. \$\endgroup\$ Commented Mar 6, 2022 at 1:59
  • \$\begingroup\$ I think this change was approx around the 1.x boundary. \$\endgroup\$ Commented Apr 10, 2022 at 8:47
9
\$\begingroup\$

Unsigned operators

When working with nonnegative integers, sometimes it's better to use unsigned operators. Specifically, if possible, use /% and %% instead of div and mod.

answered Nov 26, 2016 at 12:21
\$\endgroup\$
5
\$\begingroup\$

Use on and off as a boolean value

on / off are aliases for true / false.

answered Oct 4, 2022 at 23:38
\$\endgroup\$
3
\$\begingroup\$

Use ; to end statements

Oftentimes, a lot of Nim bytes come from the mandatory indentation of two spaces. This can sometimes be avoided by utilizing ; to break statements. For example (a bit contrived, but nevertheless effectively demonstrating the idea):

while 1>0:echo "Hello";echo"World"
answered Apr 18, 2022 at 8:46
\$\endgroup\$
1
\$\begingroup\$

1>0 for true and 0>1 for false

In Nim, there is no implicit conversion of int->bool or bool->int. It is thus advantageous for golfing to use 1>0 or 0>1 to get the values for true and false, as one might use 1/0 in C. This can help with, for example, the creation of infinite loops.

answered Apr 18, 2022 at 8:43
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.