Skip to content

Instantly share code, notes, and snippets.

View fcard's full-sized avatar

fcard

View GitHub Profile
@fcard
fcard / _generic.jl
Last active March 20, 2017 03:25
C11's _Generic-like expressions in julia with ccall and an application
@generated function _Generic(t1::Type{T1}, args...) where T1
for i in 1:2:length(args)
T2 = args[i].parameters[1]
Ex = args[i+1].parameters[1].parameters[1]
isa(T2,Type) || throw(ArgumentError("$T2 is not a type"))
if Ex == :_
T1 <: T2 && return T1
else
@fcard
fcard / fix_ubuntu_15_10_defendersquest.sh
Last active March 5, 2017 02:29
Fix compiz bug with Ubuntu 15.10 and Defenders Quest when switching windows
#!/bin/sh
DconfUserFile="$HOME/.config/dconf/user/"
CompizSchema="org.compiz.composite:$DconfUserFile"
compiz_unredirect_match() {
case $1 in
get) gsettings $1 "$CompizSchema" unredirect-match; ;;
set) gsettings $1 "$CompizSchema" unredirect-match "$2"; ;;
*) exit 1; ;;
@fcard
fcard / SELFCAT_SH_MATH.md
Last active March 2, 2017 09:56
For an overly elaborate stackexchange answer

Mathematical model

We have a base file F0, and all files considered here are F0 repeated a number of times (Natural, >0). We shall treat these files as numbers, each one can be represented by the number of characters it contains. Any other aspect of them can be ignored. Concerning these files we have two functions:

CF such that CF(F,X) is the file F concatenated with itself X times, i.e.

   CF(F,X) = CF(F,X-1) + CF(F,X-1)
   CF(F,0) = F

We also have a function AF such that AF(F,X) is the file with its initial contents repeated X times, i.e.

@fcard
fcard / optional.vim
Created February 27, 2017 01:04
Optional arguments hack in Vimscript
function OptLets(name,default)
let a:let1 = "let a:__OPT_ARGNUM__ = get(a:, '__OPT_ARGNUM__', 0)+1"
let a:let2 = "let a:".a:name." = get(a:, a:__OPT_ARGNUM__, ".a:default.")"
return a:let1."\n".a:let2
endfunction
command -nargs=+ Optional execute OptLets(<f-args>)
@fcard
fcard / muladd_macro_revised.jl
Last active February 7, 2017 20:39
Muladd macro revised
macro muladd(ex)
esc(to_muladd(ex))
end
function to_muladd(ex)
is_add_operation(ex) || return ex
all_operands = ex.args[2:end]
mul_operands = filter(is_mul_operation, all_operands)
odd_operands = filter(x->!is_mul_operation(x), all_operands)
@fcard
fcard / MuladdMacro.jl
Last active December 7, 2016 22:06
Transforms expressions of the form `a1*b1 + a2*b2 + a3*b3 + ...` into muladd expressions.
module MuladdMacro
export @muladd, UnexpectedMuladdExpression
macro muladd(add)
validate_muladd(add)
esc(to_muladd(add))
end
function to_muladd(add)
mul = operands(add)[1]
@fcard
fcard / muladd_macro.jl
Created December 6, 2016 20:26
Insert muladds in `a1*a1 + a2*b2 + ...` expressions
module Muladd
macro muladd(ex)
@assert ex.head == :call
@assert ex.args != [:+]
@assert ex.args[1] == :+
esc(_muladd_meta(ex))
end
function _muladd_meta(ex)
@fcard
fcard / letmacro.jl
Created December 5, 2016 20:10
Local macro in julia
module LetMacro
export @letmacro
macro letmacro(def, block)
sign = def.args[1]
body = def.args[2]
name = Symbol("@$(sign.args[1])")
args = sign.args[2:end]
newname = Symbol("@$(gensym(name))")
@fcard
fcard / macro_exercises_1.jl
Last active December 5, 2016 02:08
A few simple metaprogramming exercises in julia
# All expressions must evaluate to true
# Swap the first two arguments of a call expression
@swap_arguments(1 - 2) == 2 - 1
# "Invert" the operator of an expression
@invert_op(1 - 2) == 1 + 2
@invert_op(1 + 2) == 1 - 2
@invert_op(1 * 2) == 1 / 2
@invert_op(1 / 2) == 1 * 2
@fcard
fcard / benchmark_with.jl
Last active November 30, 2016 13:51
Benchmark with precomputed values
module BenchmarkWith
using BenchmarkTools
export @benchmark_with
export @benchmark_function
macro benchmark_with(args...)
esc(benchmark_with(args...))
end
macro benchmark_function(name, args...)