Skip to content

Instantly share code, notes, and snippets.

View zachallaun's full-sized avatar

Zach Allaun zachallaun

View GitHub Profile
@zachallaun
zachallaun / shitty_objects.jl
Created June 23, 2013 18:20
Don't look too hard at this.
abstract Object
type Instance <: Object
class::Object
attrs::Dict
get::Function
call::Function
self::Object
@zachallaun
zachallaun / gist:5833659
Last active December 18, 2015 19:29
Zsh function to copy pygmentized code as RTF to clipboard. Can be pasted directly into applications like Keynote. Based on https://gist.github.com/cormacrelf/2282280.
# copy pygmentized code to clipboard as rtf
#
# $ copyrtf code.py
# $ copyrtf code.py monokai
# $ copyrtf code.py monokai 24
copyrtf() {
local STYLE
local FONTSIZE
if [[ -z $2 ]]; then
STYLE="monokai"
@zachallaun
zachallaun / guarded_list_comprehensions.jl
Last active December 18, 2015 17:39
First pass at adding guards to list comprehensions with macros
# TODO:
#
# - figure out why you can't have two guards in a row
# - see if the code can be simplified
# - find a way to infer return Array type
# @guarded (Int, Symbol)[(x, y) for x in 1:5, :when=iseven(x), y in [:a, :b, :c]]
# => [(2, :a), (4, :a), (2, :b), (4, :b), (2, :c), (4, :c)]
[(x, y) for x in 1:5, y in 1:5]
@zachallaun
zachallaun / bfnumbering.jl
Created May 19, 2013 18:27
First attempt as solving the problem presented in "Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design" – http://www.cs.tufts.edu/~nr/cs257/archive/chris-okasaki/breadth-first.pdf
abstract BinaryTree
immutable Node <: BinaryTree
value
left::BinaryTree
right::BinaryTree
end
immutable Leaf <: BinaryTree
end
@zachallaun
zachallaun / unify.jl
Last active December 17, 2015 10:28
Some simple unification in Julia… on its way to simple pattern matching.
module Unify
using FunctionalCollections
export unify, extended, lvar, Anything
const _LogicVarKey = 0x1f75f6e80ac3828f
immutable LogicVar
name
@zachallaun
zachallaun / gist:5447466
Last active December 16, 2015 14:19
Pattern matching on Julia syntax
remove_quote_block(val) = val
remove_quote_block(ex::Expr) =
ex.head == :block && length(ex.args) == 2 ? ex.args[2] : ex
remove_quote_block(exs...) = map(remove_quote_block, exs)
function syntax_bindings(ex, template::Symbol, collected::Dict)
s = string(template)
if s[1] == '_' && s[end] == '_'
collected[template] = ex
# time
# ====
macro time(ex)
quote
local t0 = time_ns()
local val = $(esc(ex))
local t1 = time_ns()
println("elapsed time: ", (t1-t0)/1e9, " seconds")
val

#| #| #| #| #|

Building List Comprehensions from Scratch

... now with syntax!!!

#|

# Using @async
immutable Continuation
args
end
cont(args...) = Continuation(args)
immutable Actor
ref::RemoteRef
mailbox::RemoteRef
julia> function transform(ex)
:(println("transformed"); $(esc(ex)))
end
# methods for generic function transform
transform(ex) at none:2
julia> macro t(ex)
transform(ex)
end