Skip to content

Instantly share code, notes, and snippets.

@andyferris
andyferris / Quaternions.jl
Last active May 3, 2016 05:45
Quaternion refactor seperating algebraic and geometric properties
module Quaternions
# This is a type of number
immutable Quaternion{T <: Real} <: Number
s::T
x::T
y::T
z::T
end
@andyferris
andyferris / ImageTransformations.jl
Last active June 25, 2016 16:40
Possible image transformations design
module ImageTransformations
using CoordinateTransformations
import CoordinateTransformations.transform
export TransformedArray, ImageTransformation
abstract Filter
immutable NearestNeighbourFilter <: Filter; end
@andyferris
andyferris / SimpleSymbolic.jl
Created July 25, 2016 13:11
Simply symbolic manipulations and some matrix math for Euler angle rotations
module SimpleSymbolic
immutable S
x::Any
end
Base.show(io::IO, s::S) = print(io, s.x)
function Base.:+(s1::S, s2::S)
if s1.x == 0
@andyferris
andyferris / Maybe.jl
Created October 21, 2016 01:44
Using a `?` as a nullable typename
module Maybe
export ?
immutable ?{T}
hasvalue::Bool
value::T
?() = new(false)
?(x::T) = new(true, x)
@andyferris
andyferris / test.jl
Last active February 24, 2017 01:15
Comparison of direct generated functions vs. `unroll_tuple` as a utility function to unroll expressions
# "bespoke" generated functions
Pkg.checkout("StaticArrays","master")
using StaticArrays
f(v) = @inbounds return v[v]
v = SVector{3,Int}((1,2,3))
# results:
julia> @code_native f(v)
.text
Filename: REPL[3]
@andyferris
andyferris / recursive.jl
Last active February 25, 2017 10:44
Basic recursive static arrays
struct Vec{N,T}
data::NTuple{N,T}
end
@inline function Base.getindex(v::Vec{N}, i::Int) where {N}
@boundscheck if i < 1 || i > N
throw(BoundsError(v,i))
end
@inbounds return v.data[i]
end
@andyferris
andyferris / constructors.jl
Last active November 18, 2017 12:37
Comparing potential array constructors
# size versions
Array{T}(2, 3)
Array{T}(blah, 2, 3)
Array{T}((2,3))
Array{T}(blah, (2,3))
Array{T}(size = (2,3))
Array{T}(blah, size = (2,3))
# indices versions
Array{T}(OneTo(2), OneTo(3)) # is OneTo(2) a `blah` or the first index?
@andyferris
andyferris / dict_broadcast.jl
Last active February 21, 2018 12:00
Implementation of `broadcast` for `AbstractDict`s and `NamedTuple`s
function Base.Dict{K,V}(::Uninitialized, inds) where {K, V}
set = Set{K}(inds)
d = set.dict
n = length(d.keys)
Dict{K,V}(d.slots, d.keys, Vector{V}(uninitialized, n), d.ndel, d.count, d.age, d.idxfloor, d.maxprobe)
end
function Base.Dict{K,V}(::Uninitialized, inds::Set{K}) where {K, V}
d = inds.dict
n = length(d.keys)
function mapreduce(f, op, a::AbstractArray; init)
out = Ref(init)
foreach(a) do x
out[] = op(f(x), out[])
end
return out[]
end
function map(f, a::AbstractArray)
out = similar(a, Core.Compiler.return_type(f, Tuple{eltype(a)}))