Skip to content

Instantly share code, notes, and snippets.

#!strongSpaces
import jester, strtabs, htmlgen, strutils, os
proc fib(n: int): int =
if n < 2:
result = n
else:
result = fib(n-1) + fib(n-2)
get "/fib/?":
@jots
jots / lc.c
Last active August 29, 2015 14:00
count lines with memchr
#include <stdio.h>
#define BUFSIZE 8*1024
int main()
{
FILE *fp = stdin; /* or use fopen to open a file */
char buf[BUFSIZE+1];
unsigned long c, n = 0;
int i, chars_read;
# nimrod c -d:ssl connect.nim
# connect.nim(12, 4) Error: undeclared identifier: 'isSSL='
import sockets, strutils
var
sock: TSocket
server = "www.google.com"
port = 443
# nimrod c -d:ssl connect.nim
import sockets, strutils
let defaultSSLContext = newContext(verifyMode = CVerifyNone)
var
s = socket()
server = "www.google.com"
# length of data in 32-bit unsigned, network (big-endian) byte order
# plus the data (and increase length by 4 to account for length field
def packed(xml)
[xml.size + 4].pack("N") + xml
end
puts packed("a test packet")
@jots
jots / j.nim
Last active August 29, 2015 14:00
var r = 0
var tmp:int32 = cast[int32](pkt.len)
var thenum:int32 = 0
bigEndian32(addr(thenum), addr(tmp))
echo repr(addr(thenum))
r = s.send(addr(thenum), 4)
import strutils
# use mod == 0 a lot...
proc mod0(a,b:int):bool = (a mod b) == 0
# if i try to add to "result" directly I get a SIGSEGV
# so use "res" instead. why?
# oh yeah and there's probably a much better way to do this...
proc addCommas(s:string): string =
var buckets: array[0..9, string]
# initialize. wish they were automagically initialized
for i in 0..buckets.high: buckets[i] = ""
# XXX not work
for i in 0..1:
for buck in buckets:
# following line doesn't work. compile error:
# Error: for a 'var' type a variable needs to be passed
import unsigned
type hp = array[0..1,uint32]
var
cells: seq[hp] = @[]
ncells = 3
cells.add( [uint32(1),uint32(0)] )
echo repr(cells)
# => 0x7f8197d0b050[[(invalid data!), (invalid data!)]]
#!/usr/bin/env ruby
require "pp"
def cdb_hash(s)
r = 5381
# & 0xffffffff forces to 32 bit. (lots of hair for that)
s.split("").each { |c| r = (((r << 5) + r) ^ c.ord) & 0xffffffff }
return r
end