Created
September 10, 2013 02:03
-
-
Save njwilson23/6504130 to your computer and use it in GitHub Desktop.
A few convenience functions to supplement those in Base
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
# 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