Skip to content

Instantly share code, notes, and snippets.

@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}
@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 / 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 / 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
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 / show_sexpr.jl
Created November 20, 2012 09:46
simple lispy AST printer for julia
show_sexpr(ex) = show_sexpr(OUTPUT_STREAM, ex)
show_sexpr(io::IO, ex) = show_sexpr(io, ex, 0)
show_sexpr(io::IO, ex, indent::Int) = show(io, ex)
const sexpr_indent_width = 2
function show_sexpr(io::IO, ex::Expr, indent::Int)
inner = indent + sexpr_indent_width
if (ex.head === :block) inter, post = ("\n"*" "^inner, "\n"*" "^indent)
else inter, post = (" ", "")
@toivoh
toivoh / sexpr.jl
Created November 20, 2012 21:06
lispy AST printer and reader
# ---- @sexpr: S-expression to AST conversion ----
is_expr(ex, head::Symbol) = (isa(ex, Expr) && (ex.head == head))
is_expr(ex, head::Symbol, n::Int) = is_expr(ex, head) && length(ex.args) == n
macro sexpr(ex)
esc(sexpr_to_expr(ex))
end
sexpr_to_expr(ex) = expr(:quote, ex)
@toivoh
toivoh / get.jl
Created December 14, 2012 09:07
@get! macro
is_expr(ex, head::Symbol) = (isa(ex, Expr) && (ex.head == head))
is_expr(ex, head::Symbol, n::Int) = is_expr(ex, head) && length(ex.args) == n
# simple syntax version
macro get!(d, k, default)
quote
d, k = $(esc(d)), $(esc(k))
has(d, k) ? d[k] : (d[k] = $(esc(default)))
end
end
@toivoh
toivoh / gist:4386264
Created December 27, 2012 07:18
Type inference crash with PatternDispatch.jl
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "help()" to list help topics
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.0.0+105549818.rb65f
_/ |\__'_|_|_|\__'_| | Commit b65f0478b4 (2012-12-26 20:02:44)
|__/ |
julia> include("test_show_dispatch.jl")
type error: subtype: expected Type{T<:Top}, got (CompositeKind,{:i0, Int64, #undef, #undef, #undef … :vals, Array{Any,1}, :#s176, Int64})
@toivoh
toivoh / FermatFactors.jl
Last active August 29, 2015 14:08
Testing the maximum period property of linear feedback (xorshift) rngs
# Copyright (c) 2014: Toivo Henningsson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WIT