Vitaliy Zarubin
05/13/2021, 3:57 PMVitaliy Zarubin
05/13/2021, 4:17 PMRoukanken
05/13/2021, 4:46 PM(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...Vitaliy Zarubin
05/13/2021, 4:58 PM