Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

olivierverdier/ConstantFunctions.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Use Returns instead of this package


ConstantFunctions

Build Status codecov

Usage

ConstantFunction(v) models a constant function always returning the value v.

The functions value can be accessed by providing either zero or one (arbitrary) argument:

f = ConstantFunction(10)

f(3) # 10
f() # 10

It can also be multiplied:

f = ConstantFunction(10)
g = 3*f

g isa ConstantFunction # true
g() # 30

Motivation

This is useful if you use a type with functions, with a special treatment for constant functions. Here is an example.

struct DifferentialEquation{TF<:Function}
    λ::TF
end

function solve(eq::DifferentialEquation, x0)
    # solve x' = λ(x) x
    # with initial condition x(0) = x0
end

# special case of constant functions
solve(eq::DifferentialEquation{<:ConstantFunction}, x0) = exp(eq.λ())*x0

About

Constant functions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages