Wiki: Nim
GitHub: edit | view

Universal function call syntax

Instead of



function arg



Instead of



arg1.function arg2

Avoiding indented rows

Instead of

if condition:


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



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




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


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

Instead of

=if condition:A else:B


=[B,A][int condition]

Heavy replacing

For replacement heave solutions, use

import re
let r=replace

or multiReplace.


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 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:

cgistrutils, os, strtabs, cookies, uri
tablessince, hashes, math, algorithm
timesstrutils, math, options, since, winlean, time_t
mathsince, bitops, fenv
netsince, nativesockets, os, strutils, times, sets, options, monotimes, ssl_config, winlean, openssl, ssl_certs, winlean
jsonhashes, tables, strutils, lexbase, streams, macros, parsejson, options, since
oidshashes, times, endians, random, decode_helpers
repcre, 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