What is function composition?
It all has to do with creating small reusable functions that you can combine to compose new functions.
Now, how can we achieve this using compose and andThen?
Let's first define two simple functions - times2 and squared.
Let's start composing functions
Let's create an example to see how we can use this approach to create small pieces of reusable code - then put them together in different ways. Consider the following. We have a list of articles and we need to filter the articles based on different requirements. Let's start by introducing two basic functions - byAuthor and byTag - that filter articles based on an author and a tag.
Since BiFunction takes two arguments, it only offers the andThen function. You can't put the result of a function into a function that takes two arguments, hence the lack of the compose function. Moving on - let's also throw in a basic function that sorts a list of articles from newest to oldest and a function that returns the first article a list.
Or maybe you don't care about the author. You just want the newest article based on your favourite tag.