Skip to content

Instantly share code, notes, and snippets.

@Idorobots
Idorobots / gist:5236642
Created March 25, 2013 11:53
Seriously... Wat!?
class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample
# Wat
end
@Idorobots
Idorobots / gist:5178102
Created March 16, 2013 20:12
fmincon example
function [c, ceq] = h(x)
ceq = [];
c = (x(1) - 1)^2 + (x(2) + 1)^2 - 1;
end
function [xp, yp] = circle(x,y,r)
ang=0:0.01:2*pi;
xp= x + r * cos(ang);
yp= y + r * sin(ang);
end
@Idorobots
Idorobots / gist:5147755
Created March 12, 2013 22:40
Matlab fminunc example.
function zad5()
foo = @(x1, x2) 100*(x2 - x1^2)^2 + (sqrt(2) - x1)^2;
bar = @(x) foo(x(1), x(2));
X0 = [0; 0];
Ymin = fminunc(bar, X0);
Ymin
@Idorobots
Idorobots / gist:5021523
Last active December 14, 2015 03:29
Simple Hillis-Steele inclusive scan algorithm implementation.
V hillis_steele(alias op, V)(V array) {
for(size_t i = 1; i < array.length; i *= 2) {
for(size_t j = array.length; j > 0; --j) {
if(j > i) {
array[j-1] = op(array[j-1-i], array[j-1]);
}
}
}
return array;
}
@Idorobots
Idorobots / gist:5016087
Last active December 14, 2015 02:49
Code that writes code that writes code that defines a virtual machine emulator.
(defmacro defvm (regs @tuple body)
(append `(do (defmacro defop (name @tuple body)
(append (tuple 'defun name '$regs
(append (tuple 'dump (stringof name)) '$regs))
body))
(defmacro go (name)
(cons name '$regs)))
(map (lambda (op)
(append `(defop $(car op))
@Idorobots
Idorobots / gist:4968636
Created February 16, 2013 20:37
Splines in Scheme-like
(define (splines-1 x X Y)
(foldl (lambda (((X0 . Y0) . (X1 . Y1)) result)
(if (<= X0 x X1)
(+ Y0 (* (/ (- Y1 Y0)
(- X1 X0))
(- x X0))
result)))
(let ((zipped (zip X Y)))
(zip zipped (cdr zipped)))))
@Idorobots
Idorobots / gist:4968517
Created February 16, 2013 20:11
Function roots
(define (bisection f a b tol)
(let ((c (* (+ a b) 0.5)))
(cond ((<= (abs (f c)) tol) c)
((> (* (f a) (f c)) 0) (bisection f c b tol))
(else (bisection f a c tol)))))
(define (newton f fp fb a b tol)
(let loop ((c (if (> (* (f a) (fb a)) 0) a b)))
(if (<= (abs (f c)) tol)
c
@Idorobots
Idorobots / gist:4560378
Last active December 11, 2015 06:38
ASM MOP example featuring inheritance and multimethod dispatch.
(defclass Point (Object)
'x 'y)
#; A constructor
(defmethod initialize ((p Point) initargs)
(call-next-method)
(initialize-slots p initargs))
#; Point3D inherits the ctor
(defclass Point3D (Point)
@Idorobots
Idorobots / gist:4228636
Created December 6, 2012 21:27
Org-Capture targets
(setq org-capture-templates
(list* `("1" "New easy quest." entry (file "")
,(concat "* TODO %?\nSCHEDULED: %t\n"
":PROPERTIES:\n"
":gamify_exp: %(gamify-assign-some-exp 5 2)\n"
":END:\n"
"%U\n%a\n %i\n"))
`("2" "New medium quest." entry (file "")
,(concat "* TODO %?\nSCHEDULED: %t\n"
":PROPERTIES:\n"
@Idorobots
Idorobots / gist:4228302
Created December 6, 2012 20:59
Gamify config
(require 'gamify)
(gamify-start)