Skip to content

Instantly share code, notes, and snippets.

View rubber-duck's full-sized avatar

Rafael Munitić rubber-duck

  • atena.tech
  • Zagreb, Croatia
  • 19:50 (UTC +02:00)
View GitHub Profile
=> (defmacro locking-read
"Utility to lock a readLock from the read-write lock provided and put body part in a try expression body where
finally unlocks the lock."
[rwlock & body]
`(let [readlock (.readLock ~rwlock)]
(.lock readlock)
(try
(do ~body)
(finally (.unlock rwlock)))))
(ns test)
(defn foo [a]
{:pre [(> a 2)]}
(println a))
(binding [*assert* false]
(println *assert*)
(foo 2))
(defmacro matchfn
"Compiles a pattern match to a lambda that matches against it's parameters.
Patterns are analyzed and if no pattern contains varargs (&) then patterns will be split in to
different fn aritity overloads. If & is present in one or more patterns all arguments are captured
in to args seq and matching is done on it as ([pattern] :seq)."
[& body]
(let [fname (if (symbol? (first body)) (first body) nil)
cases (if fname (rest body) body)
pattern-expression (partition 2 cases)
pattern-varargs (some (fn [[pattern _]] (when-not (= :else pattern) (some (partial = '&) pattern))) pattern-expression)]
import std.stdio;
class Foo
{
void foo() { writeln("foo1"); }
void foo() { writeln("foo2"); }
void bar() { writeln("bar"); }
};
int main()
module gl.gl;
enum : uint
{
MAX_TESS_CONTROL_OUTPUT_COMPONENTS = 0x8E83,
ONE_MINUS_SRC_ALPHA = 0x0303,
STENCIL_BACK_FUNC = 0x8800,
AND_REVERSE = 0x1502,
....
import xml.etree.ElementTree as ET
import string
core_3_0 = [
"VERSION_1_0",
"VERSION_1_1",
"VERSION_1_2",
"VERSION_1_3",
"VERSION_1_4",
class Foo
{
int foo;
}
int main()
{
const Foo[] foo = [new Foo(), new Foo(), new Foo()];
foo[0].foo= 1;
ReturnType!fn checkedgl(alias fn) (ParameterTypeTuple!fn params, string file = __FILE__, size_t line = __LINE__)
{
scope(success)
{
auto errorCode = gl.getError();
if(errorCode != 0)
throw new GlException(errorCode, file, line);
}
return fn(params);
}
module test;
import std.stdio;
void foo(T)(T param) { static assert(false, "Not implemented"); }
void foo(T:float)(T param) { writeln(param); }
int main()
{
foo!double(1);
return 0;
static int coordIndex(char c)() pure nothrow
{
switch(c)
{
case 'x': return 0;
case 'y': return 1;
case 'z': return N > 2 ? 2 : -1;
case 'w': return N > 3 ? 3 : -1;
default:
return -1;