Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Clojure
- C++
- C#
- D
- Dart
- Erlang
- Go
- Go
- Haskell
- JS
- JS
- Java
- Lisp
- Lua
- PHP
- Perl
- Python
- Python
- Ruby
- Ruby
- Rust
- Rust
- Scheme
- Scheme
- VB
(comp g f)
Function composition is already a function in Clojure's core API. It has no restriction on numbers of parameters.
template<typename Pred1, typename Pred2>
auto compose(Pred1 f, Pred2 g){
return [f,g](auto x){ return g(f(x)); };
}
Func<T1, T3> compose<T1, T2, T3>(Func<T1, T2> f, Func<T2, T3> g) => x => g(f(x));
auto _compose(T)(T function(T) f, T function(T) g)
{
auto lambda = (T x) { return g(f(x)); };
return lambda;
}
compose(f, g) => (x) => g(f(x));
-spec compose(fun((X) -> Y), fun((Y) -> Z)) -> fun((X) -> Z).
compose(F, G) -> fun(X) -> G(F(X)) end.
func compose[T, U, V any](f func(T) U, g func(U) V) func(T) V {
return func(x T) V {
return g(f(x))
}
}
compose is generic, it accepts any type parameters T, U, V.
func composeIntFuncs(f func(int) int, g func(int) int) func(int) int {
return func(x int) int {
return g(f(x))
}
}
These specific functions take and return concrete types.
It could be done with an empty interface, but that would discard static type checks (the implementations of f and g can still be type-checked, though).
It could be done with an empty interface, but that would discard static type checks (the implementations of f and g can still be type-checked, though).
compose f g = g . f
function compose(f,g){
return function(x){
return g(f(x));
};
}
In Javascript this is valid regardless the type of the inputs and outputs.
const compose = f => g => x => g(f(x));
Curried function is idiomatic in a functional style.
Relies on ES2015 language features (lambda functions)
Relies on ES2015 language features (lambda functions)
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
This is the implementation of the compose method in java.util.function.Function
(defun compose (f g)
(lambda (x) (g (f x))))
function compose(f,g)
return function(x)
return g(f(x))
end
end
function compose($f,$g){
return function($x) use($f,$g) {
return $g($f($x));
}
}
sub compose {
my ($f, $g) = @_;
return sub {
my $x = shift;
return $g->($f->($x));
}
}
def compose(f, g):
return lambda x: g(f(x))
This is the same as for non-generic composition
compose = lambda f, g, x: \
lambda x: g(f(x))
def compose(f, g)
f >> g
end
Needs Ruby >= 2.6
def compose(f, g)
-> x { g.(f.(x)) }
end
fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<Fn(A) -> C + 'a>
where F: 'a + Fn(A) -> B, G: 'a + Fn(B) -> C
{
Box::new(move |x| g(f(x)))
}
Unfortunately, you'll need to implement another two variants if you care about FnMut and FnOnce. Also, for now, you need to box the returned function. In the future, this shouldn't be necessary.
fn compose<A, B, C>(f: impl Fn(A) -> B, g: impl Fn(B) -> C) -> impl Fn(A) -> C {
move |x| g(f(x))
}
(define (compose f g)
(lambda x
(f (apply g x))))
(define compose
(lambda (f g)
(lambda (x) (f (g x)))))