This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# "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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Maybe | |
export ? | |
immutable ?{T} | |
hasvalue::Bool | |
value::T | |
?() = new(false) | |
?(x::T) = new(true, x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ImageTransformations | |
using CoordinateTransformations | |
import CoordinateTransformations.transform | |
export TransformedArray, ImageTransformation | |
abstract Filter | |
immutable NearestNeighbourFilter <: Filter; end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Quaternions | |
# This is a type of number | |
immutable Quaternion{T <: Real} <: Number | |
s::T | |
x::T | |
y::T | |
z::T | |
end |