Skip to content

Instantly share code, notes, and snippets.

View Varriount's full-sized avatar

Clay Sweetser Varriount

View GitHub Profile
@Varriount
Varriount / aws_example.nim
Created September 21, 2017 22:38
Nim AWS Snippets
# AWS Version 4 signing example
# EC2 API (DescribeRegions)
# See: http://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
# This version makes a GET request and passes the signature
# in the Authorization header.
import base64, httpclient, hmac, nimSHA2, os, times, strutils, httpcore
# ************* REQUEST VALUES *************
from x as y import nil
from a import b
# This is an example how an abstract syntax tree could be modelled in Nim
type
NodeKind = enum # the different node types
nkInt, # a leaf with an integer value
nkFloat, # a leaf with a float value
nkString, # a leaf with a string value
nkAdd, # an addition
nkSub, # a subtraction
nkIf # an if statement
@Varriount
Varriount / libtorrent.nim
Created June 29, 2017 15:44
libtorrent.nim
const libtorrentDllName = "libtorrent-rasterbar.9.dylib"
type
Tags* {.size: sizeof(cint).} = enum
TAG_END = 0,
SES_FINGERPRINT,
SES_LISTENPORT,
SES_LISTENPORT_END,
SES_VERSION_MAJOR,
SES_VERSION_MINOR,
import tables, marshal, json, strutils, macros, sequtils
type
ConstraintViolation* = object of Exception
proc newConstraintViolation(msg: string):
raise newException(ConstraintViolation, msg)
proc `*`*[M,N,T,T2,K](a: Matrix[M,N,T]; k:K): Matrix[M,N,T2]=
for i in result.low .. result.high:
for j in result[0].low .. result[0].high:
result[i][j] = k * a[i][j]
macro loadValue(dest, source: untyped, key: static[string], required=true): untyped =
## Loads a value from a JSON node to a member of the same name
let keyIdent = ident(key)
result = quote do:
when `required`:
var node = `source`[`key`]
else:
var node = `source`.getOrDefault(`key`)
if node != nil:
@Varriount
Varriount / merge_sort.nim
Created May 29, 2017 08:56 — forked from Bennyelg/merge_sort.nim
merge_sort implementation.
import strutils
import math
#[
Module name: Implementation of MergeSort recursion.
Author: Benny E.
Mail: elgazarbenny at gmail.com
]#
type UncheckedCharArray {.unchecked.} = array[0, char]
var s = "hello world"
echo repr(cast[ptr UncheckedCharArray](addr s[0]))
# ref 0x7f037ebf3060 --> ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o']
proc someCProcedure[L](arr: ptr array[L, int]) {.header: "...", importc: "...".}
proc wrapper(arr: openarray[int]) =
someCProcedure(cast[array[1, int]](unsafeAddr arr[0]))