For the entire past year I've been written Scala code (coming from a Java background). I really liked how you could create simpler and cleaner code, with vals, case classes, map/filter/lambda functions, implicits and the type inference. I've used it mostly for an Akka-based application.
This year I'm on a Scala project with a new team, who really like functional programming. They heavily use Scalaz, and the code is filled everywhere with applicatives, context bounds, reader/writer/state monad, even the main method is "wrapped" in an I/O monad. Their reasoning is that this makes the compiler "work for us" in asserting that the code is correct, and each function is free from side effects.
Even so, from my point of view all this syntax really gets in the way of the business logic. For instance, a type of "MyBusinessObject" is fine, as well are types like "List[MyBusinessObject]", "Option[MyBusinessObject]" or even "Future[MyBusinessObject]". They all have a clear meaning and purpose. On the other hand, code like:
def method[M[_]: Applicative] = {
case (a, b) => (ca[M](a) |@| cb[M](b)) {
case t @ (ra, rb) =>
if (ra.result && rb.result) t.right
else t.left
}
}
does it add complexity to the program, or is it just me that I'm not used to this way of programming?
>>=
and<$>
, which mean nothing until you know what they do. After learning what they mean, however, they read very naturally and quickly to me now. Not really an answer, just my objective experience with things like this. I use Scala as well, but have no experience with the Scalaz library. – KChaloux Apr 04 '14 at 12:24for(i=0; i<7; ++i) { trivialOperation(i); }
with some awkwardtrivialOperationCount
variable, would you?) Now, functional programming languages with pattern matching will sometimes introduce some more variables where you'd just write out accessor method calls in OO. The result is generally more concise; perhaps a little less self-explanatory, but looking up the data declaration normally makes it clear quickly. Static typing helps a lot, it's not like in APL. – leftaroundabout Apr 04 '14 at 18:56ca
andcb
they're all introduced no more than two lines above from where they're used. The problems with short names arise mainly when there's potential for confusion, and tight scoping prevents that. And because these short names contribute to conciseness, you can more often keep the scopes so small. (Sometimes it's even better to avoid introducing local variables completely by using point-free style.) – leftaroundabout Apr 04 '14 at 23:13ca
andcb
, but they are apparently vital to what this code does. That's a problem. What I want to know is, what would this look like written in some other style of programming--then we can see whether functional programming made it more complicated. – David K Apr 05 '14 at 00:44