Skip to content

Instantly share code, notes, and snippets.

View playground.nim
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
View jspromises.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.}
View itoa.nim
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
JS promises wrapper for nim
View jspromises.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.}
View nimblepkgstructure.md

First suggestion:

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

library with single module

@stisa
stisa / nimonandroid.md
Created Mar 23, 2017
recompile nim on android
View nimonandroid.md

Install glob

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

compiler/nim.cfg

@stisa
stisa / outc.txt
Last active Mar 22, 2017
repr test
View outc.txt
a | 0
b | 0
c | 0.0
d | '\0'
e | eA
f | 0000000000000010""
g | {}
h | {}
i | [nil, nil, nil]
@stisa
stisa / index.html
Created Mar 18, 2017
Nim js backend: input element
View index.html
<html>
<body>
<input id="input" type="text" name="fname">
<script src="nimcache/t.js"></script>
</body>
</html>
@stisa
stisa / typedjson.nim
Last active Mar 13, 2017
Json -> SomeType conversion
View typedjson.nim
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 Jul 23, 2020
Static file server using jester, should be roughly equivalent to `python -m http.server`
View jest.nim
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