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
iterator testIter: int {.closure.} =
for i in 0..5:
yield i
for r in testIter():
echo r
#outputs: 0
import math
import unsigned
import strutils
import os, osproc
import times
import json
type
TVector = tuple[x, y, z: float]
@onionhammer
onionhammer / main.nim
Created October 11, 2013 00:37
Calling something().toSeq() vs. toSeq(something()) Compile time error: Error: type mismatch: got ()
template toSeq(iter: expr): expr {.immediate.} =
var result = newSeq[type(iter)]()
for x in iter: result.add(x)
result
iterator something: string =
yield "Hello"
yield "World"
var info = something().toSeq()
@onionhammer
onionhammer / main.nim
Created October 19, 2013 01:29
Nimrod tests
#Here's my program
proc x:
echo "this is a test"
when isMainModule:
#run X
x()
@onionhammer
onionhammer / nullcoalesce.nim
Last active December 26, 2015 00:19
?? operator in nimrod
template `??`*(value, default: expr): expr =
if value == nil: default
else: value
template `??`*[T](value : ref T, default : ref T) : ref T =
if value == nil: default
else: value
template `??`*[T](value : ref T, default : T) : T =
if value == nil: default
@onionhammer
onionhammer / settings.nim
Created October 20, 2013 17:34
Error: for a 'var' type a variable needs to be passed
type
TSettingKind* = enum
SettingInt, SettingString, SettingChannel, SettingBool
TSettings = seq[TSetting]
TSetting* = object
name*: string
var settings: TTable[string, TSettings]
var newSetting = TSetting(kind: kind)
@onionhammer
onionhammer / repro.nim
Created October 22, 2013 22:31
Async IO jester issue
import sockets, asyncio, jester
import strtabs, htmlgen
let dispatch = newDispatcher()
##Import Web Handlers
get "/":
resp "Hello world"
##Procedures
@onionhammer
onionhammer / filter.tmpl
Created October 26, 2013 19:59
sourcecode filter
TEMPLATE:
#! stdtmpl | standard
#
#template view*(body: expr): stmt {.immediate.} =
#
<link href=style.css rel=stylesheet>
<title>$title</title>
<header>$title</header>
# body
<footer>the end</footer>
@onionhammer
onionhammer / websocket.nim
Created November 1, 2013 15:28
Websocket read
proc read(ws: TWebSocketServer, client: TWebSocket): string =
var buffer = cstring(ws.buffer)
var read = client.socket.recv(buffer, 2)
var length = int(uint8(buffer[1]) and 127)
template readLength(size: int) =
## Read next `size` bytes to determine length
read = client.socket.recv(buffer, size, 0)
length = 0 #Reset the length to 0
@onionhammer
onionhammer / leak.html
Last active December 27, 2015 09:39
Memory leak (boiled down)
<script>
var ws;
function connect() {
ws = new WebSocket("ws://localhost:8080");
setTimeout(function() {
ws.close();
connect();
}, 20);