Skip to content

Instantly share code, notes, and snippets.

View mratsim's full-sized avatar
:shipit:

Mamy Ratsimbazafy mratsim

:shipit:
  • Paris
View GitHub Profile
@mratsim
mratsim / unfold.nim
Last active April 1, 2017 22:13
Unfolding Nim magic
import options
import future
import sequtils
# Unfolding the Nim Magic
## Bug https://github.com/nim-lang/Nim/issues/5647 open.
## unfold proper type signature should have Option[(T, U)] instead of Option[(T, T)]
# proc unfoldr*[T, U](f: U -> Option[(T, U)], x:U): seq[T] {. inline .}=
@mratsim
mratsim / fptoolbox.nim
Created April 13, 2017 07:29
Nim functional programming
template scanr[T](s: seq[T], operation: untyped): untyped =
## Template to scan a sequence from right to left, returning the accumulation and intermediate values.
## This is a foldr with intermediate steps returned
## @[2, 2, 3, 5].scanr(a + b) = @[48, 24, 12, 4]
let len = s.len
assert len > 0, "Can't scan empty sequences"
var result = newSeq[T](len)
@mratsim
mratsim / fetch_page.py
Created April 20, 2017 10:59 — forked from Smerity/fetch_page.py
An example of fetching a page from Common Crawl using the Common Crawl Index
import gzip
import json
import requests
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
# Let's fetch the Common Crawl FAQ using the CC index
resp = requests.get('http://index.commoncrawl.org/CC-MAIN-2015-27-index?url=http%3A%2F%2Fcommoncrawl.org%2Ffaqs%2F&output=json')
@mratsim
mratsim / compiler_context_typechecking.nim
Created April 20, 2017 11:23
Type checking a "Context" with Nim compiler
type
Context*[T] = ref object
nodes: seq[T]
Variable*[T; C: static[Context[T]]] = object
tape: Context[T]
index: int
value: T
proc newContext*[T]: Context[T] {.noSideEffect.} =
## main.nim - uses a loop and a split
import strutils
let inFile = open("input.txt", fmRead)
let outFile = open("output.txt", fmWrite)
var ln: TaintedString = ""
var parts: seq[string]
while inFile.readLine(ln):
@mratsim
mratsim / whilelet.nim
Created July 4, 2017 19:49
Nim while let
proc filter[T](it: (iterator : T), f: proc(x: T): bool): (iterator: T) =
return iterator(): T =
while (let x = it(); not finished(it)):
if f(x):
yield x
@mratsim
mratsim / find_big_git.sh
Created September 8, 2017 11:07
Git: find big files
git rev-list --objects --all \
| git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' \
| awk '/^blob/ {print substr($0,6)}' \
| sort --numeric-sort --key=2 \
| cut --complement --characters=13-40 \
| numfmt --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest
@mratsim
mratsim / create_labels.sh
Created September 10, 2017 09:37 — forked from omegahm/create_labels.sh
Create Gtihub labels from Bash
#!/usr/bin/env bash
# Colours picked from https://robinpowered.com/blog/best-practice-system-for-organizing-and-tagging-github-issues/
###
# Label definitions
###
declare -A LABELS
# Platform
@mratsim
mratsim / call_cuda.nim
Last active September 16, 2017 09:26
Calling CUDA kernels from Nim
import nimcuda/[cuda_runtime_api, driver_types, nimcuda]
import sequtils, future
type GpuArray[T: SomeReal] = object
data: ref[ptr T]
len: int
{.compile: "./square.cu".}
proc cuda_square(bpg, tpb: cint, y: ptr cfloat, x: ptr cfloat) {.importc, header:"../square.cuh".}
#../square.cuh is a workaround because header is not copied to nimcache