src/FunctionalProgramming.jl

Wed, 22 Dec 2021 11:14:38 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 22 Dec 2021 11:14:38 +0200
changeset 35
d881275c6564
parent 33
a60d2f12ef93
child 37
f8be66557e0f
permissions
-rw-r--r--

Add metaprogramming tools and fast multidimensional loops.

###################################
# Tools for functional programming
###################################

"""

`module AlgTools.FunctionalProgramming`

This module implements:
- `curry`
- `curryflip`
"""
module FunctionalProgramming

##############
# Our exports
##############

export curry,
       curryflip

###########
# Currying
###########

"""
`curry(f)` and `curry(f, x)`

From a function `f` of parameters `(x, y...; kwargs...)` construct a function `g`
of parameter `x`, returning a function of parameter `(y...; kwargs...)`.

The version with parameter `x` already applies the first argument, returning a function
of `(y...; kwargs...)`.
"""
function curry(f::Function)
    return x -> (y...; kwargs...)-> f(x, y...; kwargs...)
end

function curry(f::Function, x)
    return (y...; kwargs...)-> f(x, y...; kwargs...)
end

"""
`curryflip(f)` and `curryflip(f, y...; kwargs...)

From a function `f` of parameters `(x, y...; kwargs...)` construct a function `g`
of parameter `(y...; kargs...)`, returning a function of parameter `x`

The version with parametesr `(y...; kwargs...)` already applies these parameters,
returning a function of `x`.
"""
function curryflip(f::Function)
    return (y...; kwargs...) -> x -> f(x, y...; kwargs...)
end

function curryflip(f::Function, y...; kwargs...)
    return x ->f(x, y...; kwargs...)
end

end # module

mercurial