Skip to content

Instantly share code, notes, and snippets.

type
Serial = object
prefix: array[0..2, range['A'..'Z']]
digits: array[0..4, range[0..9]]
var x = Serial(prefix: [range['A'..'Z']('A'), 'B', 'X'], digits: [range[0..9](1), 1, 1, 2, 3])
echo x
@jrfondren
jrfondren / flexi.c
Created May 13, 2019 13:51
flexible array members in C
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char len;
char cap;
char data[];
} MySeq;
void dump (MySeq *seq) {
We can make this file beautiful and searchable if this error is corrected: It looks like row 3 should actually have 1 column, instead of 2. in line 2.
206 deu Theoretisch mache ich gerade Mathe. kroko \N 2010-10-14 14:36:23
207 deu Was passierte? kroko \N 2016-06-17 22:16:22
208 deu Wenn du mich nicht so gekannt hast, dann hast du mich einfach gar nicht gekannt. sugisaki 2008-05-14 18:36:22 2010-06-26 18:57:16
209 deu Es hat mir fast Angst gemacht, dich einen ganzen Tag nicht online zu sehen. kroko \N 2010-10-14 14:37:47
210 deu Ich weiß nicht, was du meinst. kroko \N 2011-04-26 18:39:53
import parsecsv
import os
var p: CsvParser
p.open("file.csv", separator='\t')
while p.readRow():
echo "new row: "
for val in items(p.row):
echo "##", val
p.close()
import os, npeg
proc linecount(str: string): int =
var count: int
let parser = peg "lines":
lines <- *line * !1 | trailing
trailing <- 1:
inc count
line <- *(1 - '\n') * '\n':
inc count
@jrfondren
jrfondren / equalproc.nim
Created May 7, 2019 18:20 — forked from iffy/equalproc.nim
How to make proc `==`(a,b: Thing):bool for a variant object.
import sequtils
import strutils
import macros
import sugar
export macros
proc replaceNodes*(ast: NimNode): NimNode =
## Replace NimIdent and NimSym by a fresh ident node
##
## Use with the results of ``quote do: ...`` to get
iterator abc: int =
defer:
echo "abc cleanup"
yield 1
yield 2
yield 3
for x in abc():
echo x
if x == 2:
proc addinto[T](x, y: openArray[T], z: var openArray[T]) =
var j = z.low
for i in x.low .. x.high:
z[j] = x[i]
inc j
for i in y.low .. y.high:
z[j] = y[i]
inc j
@jrfondren
jrfondren / getline.nim
Created May 4, 2019 10:12
26x faster on nim#head
proc getline(lineptr: ptr cstring, n: ptr cint, stream: File): cint {.importc, header: "<stdio.h>".}
proc free(s: cstring) {.importc: "free", header: "<stdlib.h>".}
iterator getlines(filename: string): string =
var
getline_line: cstring
getline_len: cint
ret: int
file = open(filename, bufSize=4096 * 2)
defer:
close(file)
func `$`*(act: Action): string =
template `-->`(str: typed, field: untyped): untyped =
str & ':' & maybeQuote($field)
macro `---`(s: untyped): untyped =
s.expectKind nnkIdent
infix(newLit(s.repr), "-->", newDotExpr(ident("act"), ident(s.repr)))
case act.kind
of Accuracy: "accuracy" --> act.acc
of Maturity: --- maturity