Abhishek Kapatkar
Engineer @ Netflix
For Python Programmers
Twitter/Github: @cabhishek
San Francisco
Nim is an incredibly fast systems and applications programming language that is expressive, extensible and fun to use.
echo "hello, 世界"
Nim Code
Nim compiler
C code
C compiler
Executable
Features | Python | Nim |
---|---|---|
Memory Management | Automatic | Automatic or Manual |
Types | Dynamic | Static |
Generics | Duck Typing | Yes |
Object Oriented | Yes | Minimal |
Closures | Yes | Yes |
Function overload | No | Yes |
Distribution | Requires CPython | Binary |
Multi Threaded | No | Yes |
Performance | Depends? | similar to C/C++ |
proc search(a: seq[int], key: int): bool =
# type inferred
var
low = 0
high = len(a) - 1
while low <= high:
let mid: int = (low + high) div 2 # integer division
if key < a[mid]:
high = mid - 1
elif key > a[mid]:
low = mid + 1
else:
return true
return false
echo search(@[2,3,4,5,6,7,8], 5) # true
Instead of writing
foo(bar(a))
a.bar().foo()
It is possible to write
echo evens(divide(multiply(values, 10), 3))
vs
echo values.multiply(10).divide(3).evens
import strutils, strformat
type
Person = object
name: string
age: int
proc speak (p: Person) =
echo fmt"My name is {p.name} and I am {p.age} years old."
let person = Person(name: "jim", age: 30)
person.speak() # My name is jim and I am 30 years old.
type Point = tuple[x, y: int]
proc `+`(a, b: Point): Point =
(a.x + b.x, a.y + b.y)
let
p1 = (x: -1, y: 4)
p2 = (x: 5, y: -2)
p3 = p1 + p2
echo p3 # (x: 4, y: 2)
Metaprogramming is a feature of Nim that gives you the ability to treat your application’s source code as data. This means you can write code that reads, generates, analyses and modifies other code.
Allows simple substitution mechanism
Simple way to reduce code duplication
To invoke a template, call it like a procedure
Templates are declarative
echo mul(2, 3)
echo 2 * 3
At compile time its re-written to
template mul(x, y: int): int = x * y
import macros
macro mul(x, y: int): typed =
expectKind(x, nnkIntLit)
expectKind(y, nnkIntLit)
let stmtList = newNimNode(nnkStmtList)
stmtList.add(
newCall(
newIdentNode("echo"),
newStrLitNode("Result is: "),
infix(x, "*", y)
)
)
return stmtList
mul(2, 2) # Result is: 4
Supports C/C++ and Javascript backends
https://nim-lang.org/docs/backends.html
Cross compilation
https://nim-lang.org/docs/nimc.html#cross-compilation
Nim Installer
https://nim-lang.org/docs/niminst.html
Self hosted
No GIL
FFI (Foreign function interface)
async/await
Package manager
Docs generator
....
Web services / API's
Command line applications
Compilers
Scientific computing
Games
Automation scripts
UI applications
Python C extensions
And more...
Arraymancer: A fast, ergonomic and portable tensor library with a deep learning focus
ao: Physically based ray tracer in Nim
Jester: A sinatra-like web framework
Karax: Single page JS applications
Reel valley: Game in JS. Details here
Spry: Programing language
Nim pymod: Call Nim from Python
Razcal: Build cross platform desktop app with Lua, MoonScript, and Layout Language
...
Smaller ecosystem
Occasional compiler bugs
https://nim-lang.org
Installation via ChooseNim
Nim in Action [Book]
Tutorials
Language manual
Code examples:
Interactive playground
Unofficial Nim blog
IRC: #nim on Freenode
Gitter: nim-lang/nim
Twitter: @nim_lang
Language goals
Efficiency
Expressiveness
Elegance
Key takeaway points
Incredibly fast, extensible & fun
Python look & feel
Zero dependency binary distribution
Comprehensive Std library
@cabhishek
By Abhishek Kapatkar