Skip to content

Commit

Permalink
RC
Browse files Browse the repository at this point in the history
  • Loading branch information
def- committed Jul 16, 2014
1 parent 7bd2f02 commit 043f4b6
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 29 deletions.
11 changes: 11 additions & 0 deletions abstract_type.nim
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
4 changes: 2 additions & 2 deletions boxthecompass.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import strutils, strfmt
import strfmt

const names = [
"North", "North by east", "North-northeast", "Northeast by north",
Expand All @@ -15,4 +15,4 @@ for i in 0..32:
var d = float(i) * 11.25
if i mod 3 == 1: d += 5.62
if i mod 3 == 2: d -= 5.62
printlnfmt("{:2} {:18} {:>6}", j + 1, names[j], d.formatFloat(ffDecimal, 2))
printlnfmt "{:2} {:18} {:>6.2f}", j + 1, names[j], d
66 changes: 66 additions & 0 deletions collections.nim
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)
31 changes: 4 additions & 27 deletions meanangle.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import math, complex, strutils

#const mpi: cdouble = 3.14159265358979323846
import math, complex

proc rect(r, phi): TComplex = (r * cos(phi), sin(phi))
proc phase(c): float = arctan2(c.im, c.re)
Expand All @@ -14,27 +12,6 @@ proc meanAngle(deg): float =
c += rect(1.0, radians(d))
degrees(phase(c / float(deg.len)))

#proc meanAngle(angles): float =
# var c: TComplex
# for d in angles:
# c = c + rect(1.0, radians(d))
# echo c
#
# echo (c / float(angles.len))
# echo phase(c / float(angles.len))
# degrees(phase(c / float(angles.len)))

#proc meanAngle(angles): cdouble =
# var xPart, yPart: cdouble = 0
# for i in 0 .. < angles.len:
# echo cos(angles[i] * pi / 180)
# xPart += cos(angles[i] * pi / 180)
# yPart += sin(angles[i] * pi / 180)
# echo angles[i], " ", xPart, " ", yPart
# echo yPart / float(angles.len), " ", xPart / float(angles.len)
# echo arctan2(yPart / float(angles.len), xPart / float(angles.len))
# arctan2(yPart / float(angles.len), xPart / float(angles.len)) * 180 / pi

echo "The 1st mean angle is: ", formatFloat(meanAngle([350.0, 10.0]), ffDecimal, 1), " degrees"
echo "The 2nd mean angle is: ", formatFloat(meanAngle([90.0, 180.0, 270.0, 360.0]), ffDecimal, 1), " degrees"
echo "The 3rd mean angle is: ", formatFloat(meanAngle([10.0, 20.0, 30.0]), ffDecimal, 1), " degrees"
echo "The 1st mean angle is: ", meanAngle([350.0, 10.0]), " degrees"
echo "The 2nd mean angle is: ", meanAngle([90.0, 180.0, 270.0, 360.0]), " degrees"
echo "The 3rd mean angle is: ", meanAngle([10.0, 20.0, 30.0]), " degrees"
22 changes: 22 additions & 0 deletions menu.nim
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
16 changes: 16 additions & 0 deletions middlethreedigits.nim
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
5 changes: 5 additions & 0 deletions min.nim
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])
12 changes: 12 additions & 0 deletions montecarlo.nim
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)
9 changes: 9 additions & 0 deletions nondecimalradicesinput.nim
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
4 changes: 4 additions & 0 deletions nondecimalradicesoutput.nim
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)
28 changes: 28 additions & 0 deletions reversednew.nim
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 ""

0 comments on commit 043f4b6

Please sign in to comment.