Skip to content

Instantly share code, notes, and snippets.

@njwilson23
Created September 10, 2013 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njwilson23/6504130 to your computer and use it in GitHub Desktop.
Save njwilson23/6504130 to your computer and use it in GitHub Desktop.
A few convenience functions to supplement those in Base
# Misc higher-level functions for Julia
function foldr(f::Function, xs::Number)
xs
end
function foldr(f::Function, xs::Vector)
L = length(xs)
if L == 0
return []
elseif L == 1
return xs[1]
elseif L == 2
return f(xs[1], xs[2])
else
return foldr(f, vcat(f(xs[1], xs[2]), xs[3:end]))
end
end
function foldl(f::Function, xs::Number)
xs
end
function foldl(f::Function, xs::Vector)
L = length(xs)
if L == 0
return []
elseif L == 1
return xs[1]
elseif L == 2
return f(xs[1], xs[2])
else
return foldl(f, vcat(xs[1:end-2], f(xs[end-1], xs[end])))
end
end
function partial(f::Function, x...)
return (xn...) -> f(x..., xn...)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment