Skip to content

Instantly share code, notes, and snippets.

@zah
zah / transitive_implication.nim
Last active August 26, 2015 11:14
Proof-carrying Nim
# Hello world program
type
Implies[A, B] = proc(a: A): B
# example Propositions
type
X = object
Y = object
Z = object
type
DenseMatrixLayout* {.pure.} = enum
ColumnMajor,
RowMajor
DenseMatrix*[R, C: static[int], T; L: static[DenseMatrixLayout]] = object
elements: array[R*C, T]
Matrix4 = DenseMatrix[4, 4, float, DenseMatrixLayout.ColumnMajor]
type
Matrix[Rows, Cols: static[int]; E] = generic M
M.T is T
Rows == M.M
Cols == M.N
m[range[0..(R - 1)], range[0..(C - 1)]] is T
proc foo(m: Matrix) =
echo Matrix.M, m.N
let x = Matrix.E()
int MakeGetRequest(const TCHAR* server, const TCHAR* url, string& out)
{
auto inet = InternetOpen(_T("AdRotate"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, 0, 0);
if(inet == NULL)
return 1;
auto session = InternetConnect(inet, server, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL);
if(session == NULL)
return 2;
const htmlElements = ["a", "div", "head"]
for e in htmlElements:
template (e) (body) =
echo "<" & e & ">"
body
echo "</" & e & ">"
@zah
zah / gist:9463398
Created March 10, 2014 11:31
implicit generics, vector
type
V[T; I: static[int]] = array[0..I, T]
proc foo(x: V): V.T =
result = x[0]
@zah
zah / gist:8576001
Created January 23, 2014 10:04 — forked from mflamer/gist:6862918
const NULL* = 0
type
TagPtr = distinct int
Sum2[T1,T2] = TagPtr
proc copyHeap*[T](x: var T): ptr T =
var
@zah
zah / libnim
Created December 17, 2013 21:09
import ast, parser
{.pragma: libnim, exportc: "nim_$1", dynlib, cdecl.}
proc parse(code: cstring): int {.libnim.} =
var n = parseString($code)
return ord(n != nil)
@zah
zah / match.nim
Last active December 29, 2015 16:19
Pattern Matching
# my alternative grammar is build around blocks as the most prominent syntax element:
# foo [anything] means foo do(anything):
# ... ...
#
# foo is foo:
# ... ...
#
# Then we have "match" magic/macro that looks like this:
match expr # The compiler automatically looks for a field used in a case
@zah
zah / gist:7440962
Created November 12, 2013 23:55
typedesc iterator
import typetraits
iterator foo(x: int, T: typedesc): int =
echo x, T.name
yield 10
for x in foo(10, int):
echo x