In computer science, function composition is an act or mechanism to combine simple functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole. -- Wikipedia
var last = compose(head, reverse) last(["first", "second", "last"]) // 'last'
In the definition of compose, g will be executed before f, thus creating a right-to-left data stream. This is much more readable than nesting a bunch of function calls.
Associativity
Like many other functional programming concepts, associativity is derived from math.It is an expression in which the order of evaluation does not affect the end result provided the sequence of the operands does not get changed. -- JOSEPH REX
Because of the grouping of calls to compose is not important, so the result is the same. This also gives us the ability to write a variadic compose. Like this:
1 2 3 4 5 6 7 8 9 10
var last = compose(head, reverse) last(["first", "second", "last"]) // 'last'
var mediaUrl = _.compose(_.prop("m"), _.prop("media")) // var images = _.compose(_.map(img), _.map(mediaUrl), _.prop('items')); // use the associativity var images = _.compose(_.map(_.compose(img, mediaUrl)), _.prop("items"))
There is no standard answer on how to composition, just make it more reusable.
Tacit programming
Tacit programming, also called point-free style, is a programming paradigm in which function definitions do not identify the arguments (or "points") on which they operate. -- Wikipedia
// 1. rewrite the following with the compose. Tip: the prop is curry function. // var isLastSold = xs => { // var x = $.last(xs); // return $.prop('sold', x); // } var isLastSold = $.compose($.prop("sold"), $.last) isLastSold(dragons) // false
// 2. use the compose, prop and head to get the name of first dragon. var nameOfFirst = $.compose($.prop("name"), $.head) nameOfFirst(dragons) // 'thiny dragon'
// 3. refactoring the averagePrice with the average to make it composition. var_average = (xs) => reduce(add, 0, xs) / xs.length // var averagePrice = xs => { // var p = map(x => x.price, xs); // return _average(p); // } var averagePrice = $.compose(_average, map($.prop("price"))) averagePrice(dragons) // 1216.6666666666667
// 4. write a function that => 'gold dragon' => 'GOLD_DRAGON' var replaceSpace = replace(/\W+/g, "_") var changeName = $.map($.compose(replaceSpace, toUppercase, $.prop("name"))) changeName(dragons) //[ 'THINY_DRAGON', 'TALL_DRAGON', 'DANGEROUS_DRAGON', 'SMALL_DRAGON', 'FAT_DRAGON', 'GOLD_DRAGON' ]
var setHtml = _.curry((tag, html) => $(tag).html(html)) var getJSON = _.curry((callback, url) => $.getJSON(url, callback))
var mediaUrl = _.compose(_.prop("m"), _.prop("media")) var mediaToImg = _.compose(img, mediaUrl) var images = _.compose(_.map(mediaToImg), _.prop("items"))
var renderImages = _.compose(setHtml("body"), images) var app = _.compose(getJSON(renderImages), url)