Katy makes writing fluent Coffeescript easy by providing the
.T combinators for Coffeescript objects.
The tl;dr is that Katy adds two methods,
.T to any class or classes you desire:
KT = require'Katy'KTKTmixIntoString# K calls a function on the receiver and returns the receiver'Hello'K s + ' World'# => returns 'Hello'# T calls a function on the receiver and returns the result'Hello'K s + ' World'# => returns 'Hello World'
You can also call any method by name:
KTmixIntoArray1..10K'pop'K'pop'K'pop'T'pop'# => returns 7
You're familiar with fluent interfaces. They're great, but they rely on the author of the API making sure that each function returns its receiver. The
.K method allows you to make any function or method "fluent" even if the original author has other ideas. The
.T methods also allow you to write your own methods and 'call' them just as if they were baked into the original object. For example, you can fake an
identifiers filter for arrays of strings:
require 'underscore'=_select arrOfSymbols/^[_a-zA-Z]\w*$/teststrsomeArrayTidentifiersK'someMethodName'TsomeOtherFilter
This is cleaner than trying to mix oridinary functions with methods and adopting tenporary variables when you want to work around what the function was written to return. In this example, having extended
.T once, you need not extend it any more to add your own custom methods.
KT(foo).T(...) work just fine without mixing
.T into an existing class, much as
_(...).tap and other methods work without modifying an existing class. Also:
KT1..10chainK'pop'K'pop'K'pop'T'pop'value# => returns 7
I agree, but that being said:
.Tmethods turn any function into something you can call like a method, which makes your code read more cleanly.
Yes, but if you like jQuery and like Katy, you'll love jQuery Combinators.
KT.installStringLambdas(). The result is not to everybody's taste, but those who like it, like it a lot.
.T is known in some CS circles as the Thrush or
T combinator. Likewise,
.K is known in combinatory logic circles as the "K Combinator" or Kestrel. To simplify the explanation radically,
K are called combinators because they combine things to produce a result in different ways. Functional programmers call such things higher-order functions, but what makes combinators interesting is that combinators work by rearranging the order of things in an expression.
T reverses the order of two things. Think about it: Instead of writing
identifiers(some_array), we use
T to write
some_array.T(identifiers). That rearrangement is very handy for making our code conform to fluent style. Likewise,
K leaves them in the same order but removes something. This ability to rearrange things is what makes them so useful for taking code that would normally have function calls sprinkled throughout it and rearranging it into a nice tree of method calls in fluent style.
Many other combinators exist, and they are all interesting with applications for functional and OO programmers. With combinators you can even get rid of parentheses in a programming language! If you aren't familiar with Combinatory Logic, I encourage you to follow the links to my posts about Kestrels and Thrushes, and better still do a little digging about Combinatory Logic in general. It's a rich, fascinating field of study that is so simple it's incredibly easy to pick up, and it leads naturally into functional and concatenative languages.