User:MikewikiBlues/sandbox

Nim (programming language)

Nim is a system programming language created by Andreas Rumpf in 2008. It was originally called Nimrod, but was renamed to Nim in 2014 ref?.

Nim supports procedural, functional, and generic programming styles. There is partial support for objects. It was influenced by C, Pascal, Python.

The compiler is written in Nim xxref, and produces C code as its output. This allows Nim code to run on a range of operating systems.

Contents --

History
Nim (originally Nimrod) was created by Andreas Rumpf in 2008. The aim was to create a high-performance language with strong static typing.

Features

 * Indentation is significant in expressing control structures, in a similar manner to Python.


 * Strong static typing.


 * Garbage-collected.


 * Integer, character, and boolean types. In addition, bit-sets, strings, enumerations, objects, and resizable arrays are built -in.   //ref?tut of the nr pl


 * Modules with interfaces and separate compilation.

closures, generics, macros, threads, parallel programming. //ok - redistrib these?


 * parallel ....


 * exepti


 * It compiles into the C language, providing portability.


 * It supports procedural, functional, and generic programming styles. There is partial support for objects.

Examples
Here is a Nim version of 'Guess The Number' from Rosetta xxxxxxx from http://rosettacode.org/wiki/Guess_the_number/With_feedback#Nim

import math, rdstdin, strutils randomize let iRange = 1..100 echo "Guess my target number that is between ", iRange.a, " and ", iRange.b, " (inclusive)." let target = random(iRange) var answer, i = 0 while answer != target: inc i let txt = readLineFromStdin("Your guess " &amp; $i &amp; ": ") try: answer = parseInt(txt) except ValueError: echo " I don't understand your input of '", txt, "'" continue if answer &lt; iRange.a or answer &gt; iRange.b: echo " Out of range!" elif answer &lt; target: echo " Too low." elif answer &gt; target: echo " Too high." else: echo " Ye-Haw!!" echo "Thanks for playing."

Here is a Nim program which calculates pi using the Leibnitz formula, from the Nim language manual. It spawns a thread for each term of the series, then sums the terms. The 'parallel' statement specifies parallel execution within a section. The compiler checks for race conditions.

http://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80

Leibniz formula for PI?????  how do i put pi in? maybe in wiki editor, is it above 80?


 * 1) Compute PI in an inefficient way

import strutils, math, threadpool

proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)

proc pi(n: int): float = var ch = newSeq[float](n+1) parallel: for k in 0..ch.high: ch[k] = spawn term(float(k)) for k in 0..ch.high: result += ch[k]

echo formatFloat(pi(5000))

Tools
todo make a list of tools


 * debugger (endb)


 * supportref todo for a number of text editors (including [web-addr aporia], written in Nim). xxxxtodo refs