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
I will post lots and lots of examples some from the book and some I just make up to learn the language.
Let create an example that uses Nim Procs (procedures). They are basically the same thing as functions in other languages.
here is the basic idea
proc keyword name_of_the_proc(param 1: type): return type (proc body)
proc fahrenheit(temp: float): float =
result = 5/9 * (temp - 32)
return result
Lets check to make sure it's the correct equation
nim> proc toCel(temp: float): float =
.... result = 5/9 * (temp - 32)
.... return result
....
nim> toCel(100)
37.77777777777778 == type float # yes its correct but we don't want that many decimal places. Lets use floor().
import std/math
proc toCel(temp: float): float =
result = 5/9 * (temp -32)
return result.floor()
Output
nim> toCel(100)
37.0 == type float
- Keyword is called "proc"
- The name of our procedure is called "fahrenheit"
- The first parameter is called temp and its type is a float
- The return type is also a float
- We imported math from the std library to use the floor() function.
Creating a proc on one line
import math
proc toCel(temp: float): float = 5/9 * (temp - 32)
toCel(100).floor()
A Nim proc doesn't require a return keyword so this example is perfectly acceptable to use.
Nixfreak public key
nixfreak dot protonmail.ch
Fingerprint: B19B C26C 80AE 32D2 46C6 FBF1 AA2F 0EBC D327 6BF1