Skip to content

Instantly share code, notes, and snippets.

View hellerve's full-sized avatar

Veit Heller hellerve

View GitHub Profile
@hellerve
hellerve / client.carp
Last active February 12, 2020 16:04
A simple HTTP client
(load "[email protected]:carpentry-org/sockets@master")
(load "[email protected]:carpentry-org/http@master")
(load "https://veitheller.de/git/carpentry/[email protected]")
(defn read-all [sock s]
(let [read (Socket.read sock)]
(if (= &read "")
(Response.parse &s)
(let [acc (String.concat &[s read])]
@hellerve
hellerve / capitalize-string.carp
Last active January 19, 2020 15:13
Our solutions to the puzzles from the Clojure Berlin Meetup in January, in Carp
; as an introduction, we tried to do string capitalization by hand,
; using as many different interesting idioms as possible
(defn capitalize-char [c]
(if (Char.lower-case? c)
(=> c
(to-int)
(- 32)
(from-int))
c))
(defndynamic derive-special-internal2 [comb f ms]
(if (= (length ms) 0)
'(zero)
(if (= (length ms) 1)
(f (caar ms))
(list comb
(f (caar ms))
(derive-special-internal2 comb f (cdr ms))))))
(defndynamic derive-special-internal [comb f a]
@hellerve
hellerve / b64.h
Created April 3, 2018 14:59
A Carp CLI for commandlinefu.com
#include <string.h>
#include <stdlib.h>
#include <core.h>
static const char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
String b64(String* osrc) {
String src = *osrc;
int len = strlen(src);
char *pos;
@hellerve
hellerve / bench.carp
Created October 24, 2017 16:32
Simple benchmarking in carp
(system-include "../bench.h")
(register get-time-elapsed (Fn [] Double))
(defn min [a]
(let [m (Double.copy (Array.nth a 0))]
(do
(for [i 1 (Array.count a)]
(let [el (Double.copy (Array.nth a i))]
(if (Double.< el m)
@hellerve
hellerve / fu.clj
Created January 15, 2016 19:03
How to fuck up a clojure devs day
; Sneak this in somewhere
(defmacro = [& args] (apply (rand-nth [identical? not=]) args))
@hellerve
hellerve / Array.prototype.next.js
Created January 5, 2016 11:27
A next() and previous() method for arrays with builtin wraparound
Array.prototype.next = function(i) {
return this[++i % this.length];
}
Array.prototype.previous = function(i) {
return i === 0 ? this[this.length - 1] : this[--i % this.length];
}
@hellerve
hellerve / knapsack-combinatorics.zp
Last active November 23, 2015 11:31
This is a zepto solution to a problem described in SICP 1.2.2
;; Solution to:
;; How many different ways can we make change of $ 1.00, given half-dollars, quarters, dimes, nickels, and pennies?
;; More generally, can we write a procedure to compute the number of ways to change any given amount of money?
;;
;; Example usage for $1.00: (count-change 100) => 292
;; This computation takes around 0.6 secs on my machine in zepto.
(define (count-change amount)
(cc amount 5))
@hellerve
hellerve / templating.zp
Last active October 20, 2015 16:16
The poor man's templating engine in zepto
(define (templating template values)
(hash:keys-reduce (lambda (template k) (string:substitute template (++ "{{" k "}}") (values k))) template values))
@hellerve
hellerve / templating.js
Last active January 5, 2016 03:58
The Poor Man's Templating Engine In JS
function templating(template, values) {
for(var k in values) {
template = template.replace('{{' + k.toString() + '}}', values[k].toString());
}
return template;
}