Skip to content

Instantly share code, notes, and snippets.

import macros,options
type C = ref object
c: int
macro until(cond,node,body:untyped):untyped =
# Example implementation, just to prove it's possible
# Generates:
# for raw in b:
# if cond(raw): break
@stisa
stisa / jspromises.nim
Created August 25, 2017 10:33
Js promises
import jsffi
type Promise*[T] = ref object of JsObject
proc newPromise*[T](executor:proc(resolve:proc(val:T), reject:proc(reason:auto))): Promise[T] {.importcpp: "new Promise(#)".}
proc resolve*[T](val:T):Promise[T] {.importcpp: "Promise.resolve(#)",discardable.}
proc reject*[T](reason:T):Promise[T] {.importcpp: "Promise.reject(#)",discardable.}
proc race*[T](iterable:openarray[T]):Promise[T] {.importcpp: "Promise.race(#)",discardable.}
proc all*[T](iterable:openarray[Promise[T]]):Promise[seq[T]] {.importcpp: "Promise.all(#)",discardable.}
proc itoa(n:int):string =
var num = if n<0: -n else: n
var i = 0
result = ""
while num > 0:
result.add(char(48 + num mod 10))
num = num div 10
inc i # inc char count
@stisa
stisa / jspromises.nim
Created May 13, 2017 12:20
JS promises wrapper for nim
import jsffi
type Promise*[T] = ref object of JsObject
proc newPromise*[T](executor:proc(resolve:proc(val:T), reject:proc(reason:auto))): Promise[T] {.importcpp: "new Promise(#)".}
proc resolve*[T](val:T):Promise[T] {.importcpp: "Promise.resolve(#)",discardable.}
proc reject*[T](reason:T):Promise[T] {.importcpp: "Promise.reject(#)",discardable.}
proc race*[T](iterable:openarray[T]):Promise[T] {.importcpp: "Promise.race(#)",discardable.}
proc all*[T](iterable:openarray[Promise[T]]):Promise[seq[T]] {.importcpp: "Promise.all(#)",discardable.}

First suggestion:

src/
  <pkgname>.nim
tests/
docs/
<pkgname>.nimble # with srcDir = "src"

library with single module

@stisa
stisa / nimonandroid.md
Created March 23, 2017 06:50
recompile nim on android

Install glob

Apt install libandroid-glob
Apt install libandroid-glob-dev

compiler/nim.cfg

@stisa
stisa / outc.txt
Last active March 22, 2017 14:52
repr test
a | 0
b | 0
c | 0.0
d | '\0'
e | eA
f | 0000000000000010""
g | {}
h | {}
i | [nil, nil, nil]
@stisa
stisa / index.html
Created March 18, 2017 20:43
Nim js backend: input element
<html>
<body>
<input id="input" type="text" name="fname">
<script src="nimcache/t.js"></script>
</body>
</html>
@stisa
stisa / typedjson.nim
Last active March 13, 2017 09:09
Json -> SomeType conversion
import json
#TODO: handle ref,ptr,tuple
proc into[T](json:seq[JsonNode],b:var openarray[T]) =
## Seq of Omogeneous JsonNodes into an array or seq of type T
for i,el in json:
when T is bool:
b[i] = el.bval
elif T is SomeInteger:
@stisa
stisa / jest.nim
Last active July 23, 2020 21:58
Static file server using jester, should be roughly equivalent to `python -m http.server`
import jester, asyncdispatch
from parseopt import getopt,CmdLineKind
from strutils import parseint,isDigit
from os import dirExists
proc usage():string = r"""
Usage: jest [<dir>][<port>][--host:<addr>]
addr : the address to serve to, default: 'localhost'
port : the port to serve to, default: 8000