concept word in category functional programming

This is an excerpt from Manning's book Grokking Functional Programming.
Imperative approach strongly encourages us to directly implement the algorithm—the “how”. So, we need to get the word, go through all the characters in the word, increment the score for each character that is different than ‘a’, and return the final score when finished.
static List<String> rankedWords(List<String> words) { return words.stream() #A .sorted(scoreComparator) #B .collect(Collectors.toList()); #C }
Assuming that words is a List of Strings, the above snippet returns a function that takes a two-parameter function that takes an Int and a String and returns an Int back:
words.foldLeft(0)((total, word) => total + wordScore(word)) #AInternally, foldLeft calls the provided function for each element of the words list (passing it as a word) and accumulates total based on the provided function. For the first element, the function is called using the initial value as an accumulator (in our case: 0). The result of the function called for the last element become the result of the foldLeft call.
![]()