Skip to content

Instantly share code, notes, and snippets.

@mflamer
mflamer / gist:6401476
Created September 1, 2013 00:12
Experimenting with some functional programming concepts in Nimrod
#------Generic Tuples
type
Tup2[T1,T2] = tuple[x1:T1, x2:T2]
Tup3[T1,T2,T3] = tuple[x1:T1, x2:T2, x3:T3]
Tup4[T1,T2,T3,T4] = tuple[x1:T1, x2:T2, x3:T3, x4:T4]
Tup5[T1,T2,T3,T4,T5] = tuple[x1:T1, x2:T2, x3:T3, x4:T4, x5:T5]
#-------Function Composition
proc `&`[T1,T2,T3](g:proc(y:T2):T3, f:proc(x:T1):T2):proc(z:T1):T3 =
@mflamer
mflamer / gist:6409814
Created September 2, 2013 06:36
Proposed arity type trait
#---evals.nim---
proc evalTypeTrait*(trait, operand: PNode, context: PSym): PNode =
InternalAssert operand.kind == nkSym
let typ = operand.sym.typ.skipTypes({tyTypeDesc})
case trait.sym.name.s.normalize
of "name":
result = newStrNode(nkStrLit, typ.typeToString(preferName))
result.typ = newType(tyString, context)
@mflamer
mflamer / gist:6821275
Last active December 24, 2015 15:29
poor mans ADT?
#----------------------------------------------------
# ADT
#----------------------------------------------------
type
SumT[T1,T2,T3,T4] = int
proc popFlags(x: int): int {.inline.} = x and 3
proc popPtr(x: int): int {.inline.} =
@mflamer
mflamer / gist:6862918
Created October 7, 2013 05:25
This code crashes the Nimrod compiler and leaves no stack trace
const NULL* = 0
type
TagPtr = distinct int
Sum2[T1,T2] = TagPtr
proc copyHeap*[T](x: var T): ptr T =
var
C:\Users\mflamer\Dropbox\DEV\Nimrod\babel-master>babel install
c:/users/mflamer/dropbox/dev/nimrod/nimrod/dist/mingw/bin/../lib/gcc/i686-w64-mingw32/4.8.1/../../../../i686-w64-mingw32/bin/ld.exe: cannot open output file c:\users\mflamer\dropbox\dev\nimrod\babel-master\babel.exe: Permission denied
collect2.exe: error: ld returned 1 exit status
c:\users\mflamer\dropbox\dev\nimrod\nimrod\config\nimrod.cfg(36, 2) Hint: added path: 'C:\Users\mflamer\.babel\pkgs\' [Path]
Hint: used config file 'C:\Users\mflamer\Dropbox\DEV\Nimrod\Nimrod\config\nimrod.cfg' [Conf]
Hint: used config file 'c:\users\mflamer\dropbox\dev\nimrod\babel-master\babel.nimrod.cfg' [Conf]
Hint: system [Processing]
Hint: babel [Processing]
Hint: httpclient [Processing]
Hint: sockets [Processing]
tyGenericInvokation(tyGenericBody TFix(tyGenericParam T, tyDistinct TFix(tyGenericParam T)),
tyGenericInst(tyGenericBody TFix(tyGenericParam T, tyDistinct TFix(tyGenericParam T)), tyFloat float, tyDistinct TFix(tyFloat float)))
tyGenericInst(tyGenericBody TFix(tyGenericParam T, tyDistinct TFix(tyGenericParam T)),
tyGenericInst(tyGenericBody TFix(tyGenericParam T, tyDistinct TFix(tyGenericParam T)), tyInt int, tyDistinct TFix(tyInt int)),
tyDistinct TFix(tyGenericInst(tyGenericBody TFix(tyGenericParam T, tyDistinct TFix(tyGenericParam T)), tyInt int, tyDistinct TFix(tyInt int))))
@mflamer
mflamer / gist:7202443
Created October 28, 2013 18:50
NimSum 0.1 - Using generics to encode enough type information to act as statically checked Sum types commonly found in functional languages.
{.hint[XDeclaredButNotUsed]: off.}
import baseutils, typetraits, macros
#----------------------------------------------------
# SumType
#----------------------------------------------------
@mflamer
mflamer / gist:7591727
Last active December 29, 2015 01:18
Enum as tagged pointer in nimrod
type
TPnt = tuple[x,y: float]
TMaybe {.enumSumTyp.} = enum
None,
Just = TPnt
var
x = (x: 45.96, y: 25.25)
test = Just(x)
proc enumConstructor(c: PContext, head: PNode): PNode =
var sym = lookUp(c, head.sons[0])
var val = lookUp(c, head.sons[1])
result = newNodeI(nkInfix, head.info)
result.addSon(newIdentNode(getIdent("or"), head.info))
result.addSon(newIntNode(nkIntLit, sym.position))
var x = newNodeIT(nkCast, head.info, getSysType(tyInt))
x.addSon(newNodeIT(nkType, head.info, getSysType(tyInt)))
var y = newNodeIT(nkAddr, head.info, x.typ)
y.addSon(newSymNode(val, head.info))
type
TMaybeKind = enum
kNone,
kJust
TMaybe[T] = object
case kind: TMaybeKind
of kJust:
v: ref T
else: nil