-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
179 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# In Nimrod type classes can be seen as an abstract type. Type classes specify | ||
# interfaces, which can be instantiated by concrete types. | ||
type | ||
Comparable = generic x, y | ||
(x < y) is bool | ||
|
||
Container[T] = generic c | ||
c.len is ordinal | ||
items(c) is iterator | ||
for value in c: | ||
type(value) is T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Array | ||
# Length is known at compile time | ||
var a = [1,2,3,4,5,6,7,8,9] | ||
var b: array[128, int] | ||
b[9] = 10 | ||
b[0..8] = a | ||
var c: array['a'..'d', float] = [1.0, 1.1, 1.2, 1.3] | ||
c['b'] = 10000 | ||
|
||
# Seq | ||
# Variable length sequences | ||
var d = @[1,2,3,5,6,7,8,9] | ||
d.add(10) | ||
d.add([11,12,13,14]) | ||
d[0] = 0 | ||
|
||
var e: seq[float] = @[] | ||
e.add(15.5) | ||
|
||
var f = newSeq[string]() | ||
f.add("foo") | ||
f.add("bar") | ||
|
||
# Tuples | ||
# Fixed length, named | ||
var g = (13, 13, 14) | ||
g[0] = 12 | ||
|
||
var h: tuple[key: string, val: int] = ("foo", 100) | ||
|
||
# A sequence of key-val tuples: | ||
var i = {"foo": 12, "bar": 13} | ||
|
||
# Set | ||
# Bit vector of ordinals | ||
var j: set[char] | ||
j.incl('X') | ||
|
||
var k = {'a'..'z', '0'..'9'} | ||
|
||
j = j + k | ||
|
||
# Tables | ||
# Hash tables (there are also ordered hash tables and counting hash tables) | ||
import tables | ||
var l = initTable[string, int]() | ||
l["foo"] = 12 | ||
l["bar"] = 13 | ||
|
||
var m = {"foo": 12, "bar": 13}.toTable | ||
m["baz"] = 14 | ||
|
||
# Sets | ||
# Hash sets (also ordered hash sets) | ||
import sets | ||
var n = initSet[string]() | ||
n.incl("foo") | ||
|
||
var o = ["foo", "bar", "baz"].toSet | ||
o.incl("foobar") | ||
|
||
# Queues | ||
import queues | ||
var p = initQueue[int]() | ||
p.add(12) | ||
p.add(13) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import strutils, rdstdin | ||
|
||
proc menu(xs) = | ||
for i,x in xs: echo " ",i,") ",x | ||
|
||
proc ok(reply, count): bool = | ||
try: | ||
let n = parseInt(reply) | ||
return 0 <= n and n < count | ||
except: return false | ||
|
||
proc selector(xs, prompt): string = | ||
if xs.len == 0: return "" | ||
var reply = "-1" | ||
while not ok(reply, xs.len): | ||
menu(xs) | ||
reply = readLineFromStdin(prompt).strip() | ||
return xs[parseInt(reply)] | ||
|
||
const xs = ["fee fie", "huff and puff", "mirror mirror", "tick tock"] | ||
let item = selector(xs, "Which is from the three pigs: ") | ||
echo "You chose: ", item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import math | ||
|
||
proc middleThreeDigits(i): auto = | ||
var s = $abs(i) | ||
if s.len < 3 or s.len mod 2 == 0: | ||
raise newException(EInvalidValue, "Need odd and >= 3 digits") | ||
let mid = s.len div 2 | ||
return s[mid-1..mid+1] | ||
|
||
const passing = @[123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345] | ||
const failing = @[1, 2, -1, -10, 2002, -2002, 0] | ||
|
||
for i in passing & failing: | ||
var answer = try: middleThreeDigits(i) | ||
except EInvalidValue: getCurrentExceptionMsg() | ||
echo "middleThreeDigits(",i,") returned: ",answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
proc reversed(x) = | ||
for i in countdown(x.high, x.low): | ||
echo i | ||
|
||
reversed(@[-19, 7, -4, 6]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import math | ||
randomize() | ||
|
||
proc pi(nthrows): float = | ||
var inside = 0 | ||
for i in 1..int64(nthrows): | ||
if hypot(random(1.0), random(1.0)) < 1: | ||
inc inside | ||
return float(4 * inside) / nthrows | ||
|
||
for n in [10e4, 10e6, 10e7, 10e8]: | ||
echo pi(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import strutils | ||
|
||
echo parseInt "10" # 10 | ||
|
||
echo parseHexInt "0x10" # 16 | ||
echo parseHexInt "10" # 16 | ||
|
||
echo parseOctInt "0o120" # 80 | ||
echo parseOctInt "120" # 80 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import strutils | ||
|
||
for i in 0..33: | ||
echo toBin(i, 6)," ",toOct(i, 3)," ",align($i,2)," ",toHex(i,2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
iterator reversed(x: seq[char]) = | ||
for i in countdown(x.high, x.low): | ||
yield x[i] | ||
|
||
var ar = ['a','b','c','d'] | ||
var sq = @['a','b','c','d'] | ||
var st = "abcd" | ||
|
||
for i in ar: stdout.write i," " | ||
echo "" | ||
for i in sq: stdout.write i," " | ||
echo "" | ||
for i in st: stdout.write i," " | ||
echo "" | ||
|
||
for i,x in ar: stdout.write i," ",x," " | ||
echo "" | ||
for i,x in sq: stdout.write i," ",x," " | ||
echo "" | ||
for i,x in st: stdout.write i," ",x," " | ||
echo "" | ||
|
||
#for i in reversed(ar): stdout.write i," " | ||
#echo "" | ||
for i in reversed(sq): stdout.write i," " | ||
echo "" | ||
#for i in st: stdout.write i," " | ||
#echo "" |