Skip to content

Instantly share code, notes, and snippets.

View onionhammer's full-sized avatar

Erik O'Leary onionhammer

  • CRF USA
  • Minneapolis, MN
View GitHub Profile
@onionhammer
onionhammer / pubsub.nim
Created February 12, 2015 15:25
Arrowlike confusion
type Pub*[T] = object
subs: seq[proc(e: T)]
proc init*[T](pub: var Pub[T]) =
if pub.subs == nil:
pub.subs = @[]
proc `()`*[T](pub: Pub, e: T) =
if pub.subs != nil:
for sub in pub.subs:
@onionhammer
onionhammer / newNonRef.nim
Last active August 29, 2015 14:12
new non-ref
import macros
proc makeRef(obj: PNimrodNode): PNimrodNode {.compiletime.} =
var typeName: string
var assignments = newSeq[PNimrodNode]()
for i in obj.children:
if i.kind == nnkIdent:
typeName = $i
elif i.kind == nnkExprColonExpr:
import jester, templates
proc view(obj: Todo): string = tmpli html"""
<ul>
$for item in obj {
<li>$item</li>
}
</ul>
"""
# Dont remove any ()'s!
import sequtils, future
iterator items(num = 10): int =
for i in 1.. num:
yield i
let values = toSeq(numbers())
.filter(x => (x mod 2 == 1))
@onionhammer
onionhammer / file.nim
Created September 7, 2014 21:16
clibpp
## Old
# Import "test" class from C++:
class(test, ns: pp, header: "../test.hpp"):
proc multiply[T](value, by: T): int
proc output: void {.isstatic.}
proc max[T](a, b: T): T
var fieldName, notherName: int
## New
@onionhammer
onionhammer / test.nim
Last active August 29, 2015 14:04
Indentation fix
when someModule:
proc a =
const someHtml = """
<div>breaks indentation</div>
"""
proc b =
const someHtml = """
<div>extra indentation</div>
"""
@onionhammer
onionhammer / test.nim
Last active August 29, 2015 13:56
pnoise
# Imports
import math
# Constants
const RAND_MAX = 0x7fff
# Types
type
TVec2 = object
x, y: float
# Imports
import zlib
import sockets, jester
# Types
type TCompressStream* = ref object of TObject
client: TSocket
when defined(usegzip):
stream: TZStream
import templates, macros
template master(content: stmt): stmt {.immediate.} =
template body = content
tmpli html"""
<html>
<body>
${ body() }
</body>
import templates
let NO_PROC = proc(n:var string) = discard
proc master(result: var string, head = NO_PROC, body = NO_PROC, footer = NO_PROC) = tmpl html"""
<head>
${ head(result) }
</head>
<body>
${ body(result) }