What if I tell you there is a language similar to Python in terms of syntax but closer to C++ or sometimes even better in terms of speed and these are just some of its charms and this is just tip of the iceberg
You would probably say that I am mad but wait we are in for a treat just bear with me.
So, what exactly is Nim?
Nim first version 0.8.2 was launched in the year 2008 it was developed by Andreas Rumpf. According to the official docs, "Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula."
Some awesome features it offers.
- Syntax is similar to Python that helps a lot of programmers while switching it surely helped me, we will see code examples to help you understand it visually.
- You can compile your programs for most systems like windows, iOS, linux, android, Nintendo Switch, embedded systems this basically means that cross compilation is possible for many platforms. Go through this to find out more Nim Compiler User Guide
- Inspired by C++ and Rust, Nim's memory management is deterministic and customizable with destructors and move semantics. It is ideal for hard-realtime embedded systems.
- Nim can be used for all backend and frontend requirements because it can be compiled into C, C++, or JavaScript.
Example for Cross Compilation from windows
#To cross compile, use for example:
#The code will compile and give e executable for linux
nim c --cpu:i386 --os:linux --compileOnly --genScript myproject.nim
- We can simply add C or C++ code in the same Nim file this help us in using Win-Api easily.
- Its speed is comparable to C++ making it ideal for many task that are simply not possible with python because of its speed.
I think we have enough reasons to know that it’s something one should look into.
Installing NIM
Go to this link to install Nim.
once you have installed Nim you should be able to run nim --version
without any problems.
You first line of Nim
# This is a comment
echo "Hello World"
To create an executable nim compile main.nim
We can run main.nim
file from cmd or use nim compile --run main.nim
this will create and run the executable.
Take input from cmd prompt.
var name: string = readLine(stdin)
echo "Hi, ", name, "!"
#Input Harry
#Output
Hi Harry
Some comparisons to see how awesome Nim is.
Source: https://github.com/lh3/biofast
Source: https://github.com/lh3/biofast
We see that sometimes Nim is even faster than C++.
Before ending I would Like to Show one of my Own Creations that Got me to Nim. Caution use this at your own risk.
#This will encrypt each and every file in that folder given in the path variable.
import os
import strformat
import base64
import nimcrypto
func toByteSeq*(str: string): seq[byte] {.inline.} =
@(str.toOpenArrayByte(0, str.high))
let
password: string = "myKey" # Our secret key
path: string = "path_of_folder_we_want_to_encrypt"
for file in walkDirRec path:
let fileSplit = splitFile(file)
if fileSplit.ext != ".encrypted":
echo fmt"[*] Encrypting: {file}"
var
inFileContents: string = readFile(file)
plaintext: seq[byte] = toByteSeq(inFileContents)
ectx: CTR[aes256]
key: array[aes256.sizeKey, byte]
iv: array[aes256.sizeBlock, byte]
encrypted: seq[byte] = newSeq[byte](len(plaintext))
iv = [byte 183, 142, 238, 156, 42, 43, 248, 100, 125, 249, 192, 254, 217, 222, 133, 149]
var expandedKey = sha256.digest(password)
copyMem(addr key[0], addr expandedKey.data[0], len(expandedKey.data))
echo len(inFileContents)
ectx.init(key, iv)
ectx.encrypt(plaintext, encrypted)
ectx.clear()
let encodedCrypted = encode(encrypted)
let finalFile = file & ".encrypted"
moveFile(file, finalFile)
writeFile(finalFile, encodedCrypted)
In the end, I would like to say that Nim is an awesome language that has a lot of potentials and will surely make great use. I would like to say everyone who liked this blog should help the community grow and spread the word. It doesn't matter how good this language is if it is not used It will surely die. Make Nim Famous
Top comments (1)
Google or Microsoft should step in to support