Learning Nim
I have been using nim-lang or nim off and on for about 6 mo. I really like the language and its fun to program in , but I have no patience to actually learn nim the correct way. I mean instead of just throwing shit together and thinking (I am cool). So I bought the books Nim In Action and Mastering Nim and I am going to go through both the books to understand better about programming.
For those whom have never heard of the Nim language take a look and see for yourself. Its very easy to get started and learn.
Nim In Action:
- Book details and how to buy can be found here: Nim in action
Starting Mastering Nim:
- Book details can be found here: Master Nim - from Nim Blog
- Can be bought on Amazon here: Mastering nim
Example's will be from both books
"Nim is a general purpose language , it is a system's programming language, meaning Nim can be used from web app development to writing kernels. Nim can used to write low level drivers or compile on embedded SoC (System on chip). "
Nim is a compile language and it gets "translated" to C source code. Nim can compile to Javascript, ObjC, C++, and and even WASM.
A good example of the capabilities of Nim for systems programming is the toy example NimKernel
Nim features include:
-
Type system
-
Metaprogramming
-
Style insensitivity: Nim uses snake_case or camelCase for style even builtin functions are style insensitive.
Example of using to_upper or toUpper
import strutils # nim library for string utilities
echo "Hello, ".to_upper()"
echo "World, .toUpper()"
👑 INim 0.6.1
Nim Compiler Version 1.6.8 [Linux: amd64] at /home/nixfreak/.nimble/bin/nim
nim> import strutils
nim> echo "Hello, ".to_upper()
HELLO,
nim> echo "World, ".toUpper()
WORLD,
- python esque syntax (No curly or square brackets , only spaces)
import strutils
var
fname: string
lname: string
age: int
echo "What is your first name: "
fname = readLine(stdin)
echo "What is your last name: "
lname = readLine(stdin)
echo "How old are you :"
age = parseInt(readLine(stdin))
echo "Hello ", $fname & " " & $lname
echo "You are ", age, " years old"
Output:
What is your first name:
Ben
What is your last name:
Watterman
How old are you :
43
Hello Ben Watterman
You are 43 years old
Another example:
import strutils
var
fname: string
lname: string
age: int
echo "What is your first name: "
fname = readLine(stdin)
echo "What is your last name: "
lname = readLine(stdin)
echo "How old are you :"
age = parseInt(readLine(stdin))
echo "Hello ", capitalizeAscii($fname) & " " & capitalizeAscii($lname) # capitalizeAscii is a funct found in strutils library
echo "You are ", age, " years old"`
Output:
What is your first name:
ben
What is your last name:
henry
How old are you :
45
Hello Ben Henry
You are 45 years old
Another example using terminal colors
- We import "terminals" module or library to create some color on screen for our output.
import std/strutils
import std/terminal
var
fname: string
lname: string
age: int
echo "What is your first name: "
fname = readLine(stdin)
echo "What is your last name: "
lname = readLine(stdin)
echo "How old are you :"
age = parseInt(readLine(stdin))
var first_name = capitalizeAscii($fname)
var last_name = capitalizeAscii($lname)
var first = $first_name
var last = $last_name
#echo "Hello ", $first & " ", $last
stdout.styledWriteLine {styleBright}, fgBlue, $first, " " ,fgGreen, $last
echo "You are ", age, " years old
Output
What is your first name:
ben
What is your last name:
watterman
How old are you :
43
Ben Watterman
You are 43 years old
Unfortunately "markdown" doesn't have color support when using code blocks, so a nice screenshot should suffice.
Nixfreak public key
nixfreak dot protonmail.ch
Fingerprint: B19B C26C 80AE 32D2 46C6 FBF1 AA2F 0EBC D327 6BF1