This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// allocate memory for a type, and initialise it at the same time | |
#define ialloc(t, ...) ialloc_impl(sizeof(t), &(t){ __VA_ARGS__ }) | |
// helper function | |
inline static void* ialloc_impl(int size, void* src) { | |
void* dest = malloc(size); | |
memcpy(dest, src, size); | |
return dest; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
typedef struct { | |
int capacity; | |
int size; | |
void** data; | |
} list_t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import structs/HashMap | |
Vec2: cover { | |
x, y:Int | |
} | |
vec2equals: func <T> (_a, _b:T) -> Bool { | |
a := _a as Vec2 | |
b := _b as Vec2 | |
a x == b x && a y == b y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function remove(t,n) | |
for i=n, count(t) do | |
t[i] = t[i+1] | |
end | |
end | |
tbl = {10,20,30,40,50} | |
remove(tbl, 2) | |
foreach(tbl, print) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import structs/ArrayList | |
import threading/Thread | |
counter := 0 | |
mutex := Mutex new() | |
threads := ArrayList<Thread> new() | |
main: func { | |
for (i in 0..10) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pootis(f, ...) | |
local t = {} | |
for i,v in ipairs({...}) do | |
t[i] = f(v) | |
end | |
return unpack(t) | |
end | |
-- example usage | |
-- > pootis(math.floor, 1.1, 2.2, 3.3, 4.4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- example usage: | |
-- lua fix_cel_tiles.lua old_tiles.cel new_tiles.cel | |
local inpath, outpath = ... | |
local infile = assert(io.open(inpath, "rb")) | |
local outfile = assert(io.open(outpath,"wb")) | |
local bytes_per_tile = 8 * 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc `+`*(a:string, b:string):string = a & b | |
proc `+`*[T](a:string, b:T):string = a & $b | |
proc `+`*[T](a:T, b:string):string = $a & b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strformat | |
type Entity = ref object of RootObj | |
tid: int # tid uniquely identifies which subclass we are (e.g. Foo or Bar) | |
type Foo = ref object of Entity | |
x, y: float | |
name: string | |
type Bar = ref object of Entity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc foo(x:int) = echo "int ", x | |
proc foo(x:string) = echo "string ", x | |
var p1:pointer = (proc(x:int){.nimcall.})(foo) # p1 gets the int version of foo | |
var p2:pointer = (proc(x:string){.nimcall.})(foo) # p2 gets the string version of foo | |
var f1 = cast[proc(x:int){.nimcall.}](p1) | |
var f2 = cast[proc(x:string){.nimcall.}](p2) | |
f1(100) |
OlderNewer