Skip to content

Instantly share code, notes, and snippets.

View fcard's full-sized avatar

fcard

View GitHub Profile
@fcard
fcard / inner-macro.md
Last active September 26, 2016 17:11
Julia: using inner macro

There are two solutions to this problem that I like:

1: Have the inner macro body in a function then reuse the function after

macro outer()
  function inner()
  end
  quote
    macro inner()
 $(inner)()
@fcard
fcard / ReloadFile.jl
Created October 2, 2016 02:56
Reloading files without worrying about method redefinitions.
const __reload_ignore_names = union(
Set(names(Core)),
Set(names(Base)),
Set([:Main, :LastMain, :Base, :Core, :ans])
)
function reload_file(filename)
@gensym filemod_name mainmod_name
mainmod = current_module()
@fcard
fcard / dynamic_scope.jl
Last active October 18, 2016 14:11
@dynamic macro that allows one to create dynamically scoped variables
module DynamicScope
export @dynamic, @define_dynamic, UnboundDynamicVarError, InvalidDynamicPathError
type DynamicVar{T}
name::Symbol
values::Vector{T}
end
type UnboundDynamicVarError <: Exception
name::Symbol
@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...)
@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 / 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 / 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 / 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_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 / 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>)