Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save haxscramper/04cab2f6abc410d2e5b5df950415fb81 to your computer and use it in GitHub Desktop.
Save haxscramper/04cab2f6abc410d2e5b5df950415fb81 to your computer and use it in GitHub Desktop.
Nim features you didn't know and don't need to

Nim features you didn’t know and don’t need to

Can put basically anything as identifier

Technically this is just function call using method call syntax - × is treated as regular identifier (like someFunction) and it is perfectly legal to call obj .someFunction arg. So a .× b is not really different in that regard.

proc ×(a, b: set[char]): seq[(char, char)] =
  for aIt in a:
    for bIt in b:
      result.add (aIt, bIt)

echo {'c', 'd'}  {'a', 'b'}
@[('c', 'a'), ('c', 'b'), ('d', 'a'), ('d', 'b')]

s_T_yLe_InSeSe_tiVe

let hello = 12
echo hE_LL_o
12

Unicode identifiers

import macros

dumpTree:
    2  ⟦ {t₁ .∈ s(t₁) | (t₁, t₂) .∈ M} ⟧
    ---------------------------------------s(t₂) ⟧ +s(t₁) ⟧


echo "compile ok!"
compile ok!

Arbitrary expressions as function parameter default value

proc tmp(a = 12, b = a + 2, c = ((
  block:
    var buf: string
    for i in 0 .. 2:
      buf &= " * " & $i

    buf
))
) = echo b, c

tmp(b = 122)
122 * 0 * 1 * 2

Source code filters

#?replace(sub="function", by="func")
function nice(x, y: int): int = x + y


echo 12 .nice 50
62

Custom ternary operators with term rewriting

import macros
proc arrImpl(a, b, c: int): void = echo a, " ", b, " ", c

macro `~`(lhs: untyped{nkBracketExpr}, c: int): untyped =
  let a = lhs[0]
  let b = lhs[1]
  quote do:
    arrImpl(`a`, `b`, `c`)

2[3] ~ 4
2 3 4

Variadic infix operators

Use { `|` * a } pattern for term rewriting template to collect all arguments into single list and then pass to actual implementation.

proc `|`(s: varargs[int]): int = 
  for arg in s:
    result += arg
  
proc `|`(a: int, b: string): string = 
  $a & "--" & b
  
template optPipe{ `|` * a }(a: int): untyped = |a

echo 2 | 3 | (12 + 23) | "hello"
40--hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment