Skip to content

Instantly share code, notes, and snippets.

Avatar

David Gold davidagold

View GitHub Profile
@davidagold
davidagold / .bash_profile
Created May 24, 2018 — forked from natelandau/.bash_profile
Mac OSX Bash Profile
View .bash_profile
# ---------------------------------------------------------------------------
#
# Description: This file holds all my BASH configurations and aliases
#
# Sections:
# 1. Environment Configuration
# 2. Make Terminal Better (remapping defaults and adding functionality)
# 3. File and Folder Management
# 4. Searching
# 5. Process Management
View mymap.jl
macro fcall(Xs, nargs)
res = Expr(:call, :f)
for i in 1:nargs
push!(res.args, :($(Xs)[$i][i]))
end
return res
end
mymap(f, Xs...) = _map(f, Xs...)
@davidagold
davidagold / liftmacro.jl
Created Dec 21, 2015
Revised lift macro
View liftmacro.jl
Base.isnull(x) = false
Base.get(x) = x
macro ^(call, T...)
arg_cache = Dict{Union{Symbol, Expr}, Expr}()
# if !(isa(call, Expr)) || call.head != :call
# throw(ArgumentError("@^: argument must be a function call"))
# end
if length(T) == 1
e_type = T[1]
@davidagold
davidagold / pwise_meandist.jl
Created Sep 25, 2015
average pairwise distance over array of Float 2-tuples
View pwise_meandist.jl
function pwise_meandist(A::Array{Tuple{FLoat64, Float64}})
s = 0.0
i = 1
n = length(A)
while i < n
(x0, y0) = A[i]
for j in (i+1):length(A)
(x1, y1) = A[j]
s += sqrt((x0 - x1)^2 + (y0 - y1)^2)
end
@davidagold
davidagold / autoreason.jl
Last active Oct 7, 2018
Simplify arithmetic expressions
View autoreason.jl
function simplify(e::Expr)
_simplify(e)
end
_simplify(e) = e
function _simplify(e::Expr)
# apply the following only to expressions that call a function on arguments
if e.head == :call
op = e.args[1] # in such expressions, `args[1]` is the called function
simplified_args = [ _simplify(arg) for arg in e.args[2:end] ]
@davidagold
davidagold / nullityindex.jl
Created Jul 25, 2015
`NullityIndex` -- indexing non-empty entries of a `NullableArray`
View nullityindex.jl
immutable NullityIndex
non_nulls::Vector{Int}
end
function NullityIndex(X::NullableArray)
inds = Vector{Int}()
sizehint!(inds, length(X.isnull))
for i in eachindex(X.isnull)
!X.isnull[i] && push!(inds, i)
end
@davidagold
davidagold / .jl
Last active Aug 29, 2015
`NullityIndex` -- indexing non-empty entries of a `NullableArray`
View .jl
immutable NullityIndex
ranges::Vector{UnitRange{Int}}
singletons::Vector{Int}
end
function NullityIndex(X::NullableArray)
ranging = false
push = false
first = 0
last = 0
@davidagold
davidagold / gist:0de522ac52973936ef9d
Last active Aug 29, 2015
NullableArrays `map` perf
View gist:0de522ac52973936ef9d
using NullableArrays
srand(1)
N = 5_000_000
A = rand(N)
B = rand(Bool, N)
C = Array(Float64, N)
X = NullableArray(A)
@davidagold
davidagold / profile_reduce.jl
Created Jul 2, 2015
Reduce methods testing/profiling
View profile_reduce.jl
using NullableArrays
A = rand(5_000_000)
X = NullableArray(A)
f(x) = 5 * x
f{T<:Number}(x::Nullable{T}) = Nullable(5 * x.value, x.isnull)
function Base.mapreduce_impl{T}(f, op::Base.MinFun, X::NullableArray{T}, first::Int, last::Int)
# locate the first non-null entry
View gist:145e0a0f41f6d1baa915
function Base.map!{F}(f::F,
dest::NullableArray,
A::AbstractArray) # -> NullableArray{T, N}
local func
function func(dest, A)
for i in 1:length(dest)
dest[i] = f(A[i])
end
end
func(dest, A)