Wiki: Nim

Universal function call syntax

Instead of

function(arg)

use

function arg

or

arg.function

Instead of

function(arg1,arg2)

use

arg1.function arg2

Avoiding indented rows

Instead of

if condition:
 instruction

use

if condition:instruction

The same trick works for loops and even procs definitions. You can further avoid the leading spaces by separating instructions by ; instead of newlines.

You can also replace all indentation by parentheses.

for x in..10:
 for y in..10:
  if x+y>5:echo x+y

Can become:

for x in..10:(for y in..10:(if x+y>5:echo x+y))

or even shorter:

for x in..10:(for y in..10:
if x+y>5:echo x+y)

Creating seqs of strings

Use

"item1,item2,item3,item4,item5,item6,item7,item8".split','

to create a list of strings. This requires importing the strutils module and saves 2N-16 characters (where N is the number of items and assuming you already import something). If none of the items contain a whitespaces, use

split"item1 item2 item3 item4 item5 item6 item7 item8"

You can also use the split function from re instead for a shorter import, though it requires a regex splitter.

"item1,item2,item3,item4,item5,item6,item7,item8".split re","

Type inference in proc headers

Use default values for parameters to avoid having to declare their type. For example

proc f(n=0,s="")=...

saves 2+4 characters.

Furthermore, you can declare the output type to be auto (or even deprecated any) to save additional bytes.

Multiple variable declarations

Multiple variables can be declared using a single var keyword like

var x,y,z=0

or

var
 x=0
 y=1
 z=2

ARGV

Instead of iterating over commandLineParams or using paramCount loop over sufficiently large hardcoded int range and use paramStr. Like so

for i in 1..99:
 let n=parseInt paramStr i

Boolean access

Instead of

=if x>0:A else:B

use

=[B,A][1.min x]

Instead of

=if condition:A else:B

use

=[B,A][int condition]

Heavy replacing

For replacement heave solutions, use

import re
let r=replace

or multiReplace.

Mappings

Instead of using tables or case constructs, use two arrays of matching length. Find the index of a key using find and use that to access the value in the other array.

Functional programming

Use map, filter, all (or/and "It" variants) etc. from sequtils module.

Include

Use

include prelude

to import modules os, strutils, times, parseutils, hashes, tables, sets, sequtils, parseopt, strformat. Furthermore, depending on what modules you need, you can find some module that imports what you need and include that instead. Some examples of such modules:

Module Imports
cgi strutils, os, strtabs, cookies, uri
tables since, hashes, math, algorithm
times strutils, math, options, since, winlean, time_t
math since, bitops, fenv
net since, nativesockets, os, strutils, times, sets, options, monotimes, ssl_config, winlean, openssl, ssl_certs, winlean
json hashes, tables, strutils, lexbase, streams, macros, parsejson, options, since
oids hashes, times, endians, random, decode_helpers
re pcre, strutils, rtarrays

Looping a fixed amount

The usual way to execute some code a fixed number of times is via a for loop:

for i in..n:do something

However, if the index variable isn't needed, there is a neat trick to save a byte:

for()in..n:do something