Tell me which option is correct? Are functions sup...
# functional
v
Tell me which option is correct? Are functions supposed to run from right to left?
Or does it not matter and depends on the task?
r
in math, function composition works like this:
(f ◌ g)(x) = f(g(x))
in Java, the
Function<A, B>
interface has 2 functions,
compose
that works like mathematical one and
andThen
function that works the other way around, eg:
a.compose(b).apply(x) = a.apply(b.apply(x))
a.andThen(b).apply(x) = b.apply(a.apply(x))
(
apply
is Functions's
invoke
) From what I seen, usually programmers prefer to use
andThen
as they consider it more intuitive to work with it. (As when you are writing, you usually want to apply functions in order you are writing them) So yeah, answer is - it depends. But don't forget to name it properly - eg, the first one is
andThen
but other should be called
composition
or some similar names...
🙏 1