Skip to content

Instantly share code, notes, and snippets.

#include <stdio.h>
#define print(X) _Generic((X), \
double: printf("%f\n", X),\
int: printf("%d\n", X),\
char: printf("%c\n", X))
int main (void) {
int x = 42;
double z = 11.0;
print(x);
@jrfondren
jrfondren / test.d
Created October 3, 2019 04:23
jsonizer this error
#! /usr/bin/env dub
/+ dub.sdl:
dependency "jsonizer" version="~>0"
+/
import std.stdio;
import jsonizer;
struct ComponentList
{
mixin JsonizeMe;
import re
let
haystack = "example v22"
versionRe = re"v(\d+)$"
if haystack =~ versionRe:
echo "Version: ", matches[0]
type
Error1 = object of Exception
Error2 = object of Exception
proc t1(error: ref Exception) =
raise error
try:
t1(newException(Error1, "foo"))
except Error1:
type
Error1 = object of Exception
Error2 = object of Exception
proc t1(error: ref Exception) =
raise error
try:
t1(newException(Error1, "foo"))
except Error1:
proc example =
defer: echo "deferred"
raise newException(ValueError, "an exception")
example()
func ternary(a, b, c: bool): range[0..7] =
int(a) shl 2 and int(b) shl 1 and int(c)
proc renderVisibleRows(db: DbConn): string =
template rows(query: varargs[untyped]): untyped =
for row in db.fastRows(query):
result.add renderVisibleRow(row)
when defined(samplever):
rows(sql"SELECT * FROM domains WHERE rowid in (SELECT rowid FROM domains WHERE ruling IS NULL ORDER BY RANDOM() LIMIT 25) ORDER BY HOST ASC, OWNER ASC, USER ASC, DOMAIN ASC")
return
type
AnyTypeTypes = enum
ATString, ATInt, ATBool
AnyType = object
case kind: AnyTypeTypes
of ATString: str: string
of ATInt: n: int
of ATBool: b: bool
proc wrap(str: string): AnyType = result.kind = ATString; result.str = str
@jrfondren
jrfondren / delay.nim
Last active May 16, 2019 00:32
512ms
import asyncdispatch, asyncnet, times
when defined(tryfix):
# modified asyncstream
# begin -old--old--old--old--old--old--old--old--old--old--old--old--old--old--
import asyncfutures, deques
type
FutureStream*[T] = ref object
queue: Deque[T]
@jrfondren
jrfondren / relativelypainful.nim
Created May 15, 2019 22:36
ways to handle local IO
import asyncnet, asyncdispatch, threadpool
proc echoStdin =
var line = spawn stdin.readLine()
while true:
if line.isReady():
echo "You said: " & ^line
line = spawn stdin.readLine()
asyncdispatch.drain(60)