Skip to content

Instantly share code, notes, and snippets.

const
Identity = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]
Identity = Matrix([
[1, 0, 0],
[0, 1, 0],
const CacheLineSize = 32 # true for most archs
type
Barrier {.compilerProc.} = object
entered: int
cv: CondVar # condvar takes 3 words at least
when sizeof(int) < 8:
cacheAlign: array[CacheLineSize-4*sizeof(int), byte]
left: int
cacheAlign2: array[CacheLineSize-sizeof(int), byte]
import threadpool, tables, strutils
var db {.threadvar.}: CountTable[string]
proc rawPut(key: string) =
inc(db, key)
proc rawInit() =
db = initCountTable[string]()
var
a: array[2, Thread[int]]
proc worker(x: int) =
while true:
echo x
createThread(a[0], worker, 1)
createThread(a[1], worker, 2)
@Araq
Araq / parseBool.nim
Created February 20, 2011 13:17
parseBool
proc parseBool(s: string): bool =
case normalize(s)
of "y", "yes", "true", "1", "on": result = true
of "n", "no", "false", "0", "off": result = false
else: raise newException(EInvalidValue, "cannot interpret as a bool: " & s)
#
#
# Nimrod's Runtime Library
# (c) Copyright 2011 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module implements big integers for Nimrod.
@Araq
Araq / gist:1235428
Created September 22, 2011 17:38
Module injection
# module A
when not defined(dolog):
template dolog(msg: string) = nil
proc whatever() =
dolog "start of whatever"
doSomeWork
dolog "end of whatever"
@Araq
Araq / widestrings.nim
Created January 22, 2012 18:48
Windows' wide strings to Nimrod
import os
type
TUtf16Char = distinct int16
WideCString = ptr array[0.. 1_000_000, TUtf16Char]
const
utf8Encoding = 65001
proc len(w: WideCString): int =
@Araq
Araq / tsortdev.nim
Created January 28, 2012 22:46
yet another showstopper (part 2)
discard """
disabled: false
"""
import math, algorithm
proc sorted[T](a: openArray[T], order: TSortOrder): bool =
result = true
for i in 0 .. < a.high:
if cmp(a[i], a[i+1]) * order > 0:
proc p =
var delta = 7
proc accumulator(start: int): proc(): int =
var x = start-1
result = proc (): int =
x = x + delta
inc delta
return x
var a = accumulator(3)