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 / 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
@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 / 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
# 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))
import jester, templates
proc view(obj: Todo): string = tmpli html"""
<ul>
$for item in obj {
<li>$item</li>
}
</ul>
"""
@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:
@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 / alternate.nim
Last active August 29, 2015 14:20
Example
# layout.nim
import templates
proc render*(title, content: string): string =
tmpli html"""
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>$title</title>
@onionhammer
onionhammer / fsharpness.fs
Last active October 4, 2015 20:53
Compiles F# to javascript then invokes closure compiler on it, parses response w/ json data provider
#load "Scripts/load-references.fsx"
open System
open System.Net.Http
open System.Collections.Generic
open FSharp.Data
let map (items: seq<'a * 'b>) = items |> Seq.map(KeyValuePair)
type compilationResult = JsonProvider<"""
iterator testIter: int {.closure.} =
for i in 0..5:
yield i
for r in testIter():
echo r
#outputs: 0