Skip to content

Instantly share code, notes, and snippets.

macro expect(pred)
:( ($esc(pred)) ? nothing : error($"error: expected $pred == true") )
end
is_expr(ex, head::Symbol) = (isa(ex, Expr) && (ex.head == head))
function split_fdef(fdef::Expr)
@expect (fdef.head == :function) || (fdef.head == :(=))
@expect length(fdef.args) == 2
signature, body = tuple(fdef.args...)
@toivoh
toivoh / test.jl
Created August 10, 2012 09:28
AST pretty printing example
code = quote
function show_body_lines(io::IO, ex)
args = is_expr(ex, :block) ? ex.args : {ex}
for arg in args
if !is_linenumber(arg); print(io, '\n'); end
show(io, arg)
end
end
end
@toivoh
toivoh / test.jl
Created August 9, 2012 13:24
recshow example/test
require("recshow.jl")
type T
x
y
end
type Unshowable; end
show(io::IO, x::Unshowable) = error("Tried to show Unshowable()")
@toivoh
toivoh / recshow.jl
Created August 7, 2012 07:21
recshow: Prototype show() work-alike to show self-referential and DAG-structured objects
# ---- ObjNode: capture of an object's output to show() -----------------------
type ObjNode
# actual capture
obj
reused::Bool
items::Vector # ObjNode:s and strings/chars
# scratch space for recshow etc
strlen::Integer
@toivoh
toivoh / freeze.jl
Created April 26, 2012 20:14
Sketch at a Frozen class
# union of immutable types not to freeze/unfreeze
typealias NoFreeze Union(Number, String)
type Frozen{T,COUNT}
item::T
watchers::Set{WeakRef}
end
typealias FrozenArray{T,N,COUNT} Frozen{Array{T,N},COUNT}