Rick

Rick
Rick

Monday, September 28, 2015

Scala book review, notes, random thoughts part 4

Read chapter 5, 6, and 7 pretty quickly. Now on chapter 8 of the Scala book. Chapter 8 Functions.
I want to write some head to head comparisons of Scala first class Function support vs. Java 8 lambda expressions and Function. More for my own edification.
Function values are objects, so you can store them in variables if you like. They are functions, too, so you can invoke them using the usual parentheses function-call notation. --Odersky, Martin; Spoon, Lex; Venners, Bill (2010-12-13). Programming in Scala: A Comprehensive Step-by-Step Guide (Kindle Locations 2911-2912). Artima Press. Kindle Edition.
You could not do this with Java functions. The best you could do would be to pass it around and then call apply (https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html). It has a certain amount of awkwardness, which you would not find in Groovy, Scala, or Python. Changes in the Java syntax are more conservative due to its large amount of users. Backwards compatible can be a crutch.
I am on chapter 8 and there seems to be 35 chapters. Perhaps I need a different book. This is a great book. But I have already started to use Scala at work and need a quicker introduction. Perhaps.
This is a really good book on Scala, but I am in a hurry so to speak. Hmmm... I will soldier on for now.
I will rethink this after this chapter.
To make a function literal even more concise, you can use underscores as placeholders for one or more parameters, so long as each parameter appears only one time within the function literal. --Odersky, Martin; Spoon, Lex; Venners, Bill (2010-12-13). Programming in Scala: A Comprehensive Step-by-Step Guide (Kindle Locations 2942-2943). Artima Press. Kindle Edition.
That is pretty short, but man there are so many ways to do the same thing and unlike Java where I get to use one of those ways for five years until the new improved ways comes with Scala they are all here at once. Seems daunting.
The function literal _ > 0, therefore, is equivalent to the slightly more verbose x => x > 0, --Odersky, Martin; Spoon, Lex; Venners, Bill (2010-12-13). Programming in Scala: A Comprehensive Step-by-Step Guide (Kindle Locations 2960-2964). Artima Press. Kindle Edition.
Ok.. so it sounds like partially applied functions seem a lot like currying.
Now, although sum _ is indeed a partially applied function, it may not be obvious to you why it is called this. --Odersky, Martin; Spoon, Lex; Venners, Bill (2010-12-13). Programming in Scala: A Comprehensive Step-by-Step Guide (Kindle Locations 3018-3019). Artima Press. Kindle Edition.
Why don't they just say that. Not sure. I get the concept. Used it before with Groovy and even wrote my own functional lib that allowed currying. The syntax, to me, seems rough or should I say foriegn to my experience.
Closures seem the same (so far) in Java and Scala. One big difference is Scala does not require final for closures.
This example brings up a question: what happens if more changes after the closure is created? In Scala, the answer is that the closure sees the change. --Odersky, Martin; Spoon, Lex; Venners, Bill (2010-12-13). Programming in Scala: A Comprehensive Step-by-Step Guide (Kindle Locations 3093-3095). Artima Press. Kindle Edition.
POP!
Scala allows you to indicate that the last parameter to a function may be repeated. This allows clients to pass variable length argument lists to the function. --Odersky, Martin; Spoon, Lex; Venners, Bill (2010-12-13). Programming in Scala: A Comprehensive Step-by-Step Guide (Kindle Locations 3127-3128). Artima Press. Kindle Edition.
We have String... args in Java and args: String* in Scala. Ok. Unlike Java no auto conversion from Array to vararg. We have to use _* which is more like Python to spread the arguments.
Named arguments are most frequently used in combination with default parameter values. --Odersky, Martin; Spoon, Lex; Venners, Bill (2010-12-13). Programming in Scala: A Comprehensive Step-by-Step Guide (Kindle Locations 3146-3147). Artima Press. Kindle Edition.
Named arguments and default arguments is one of the things that made me love Python and later Groovy.
Functions like approximate, which call themselves as their last action, are called tail recursive. --Odersky, Martin; Spoon, Lex; Venners, Bill (2010-12-13). Programming in Scala: A Comprehensive Step-by-Step Guide (Kindle Locations 3178-3179). Artima Press. Kindle Edition.
Ok. I hear this term a lot.
Does Java support it? I am not sure. I hear several mentions of it, but the answer seem inconclusive.
From what I understand, the JIT might do it, if certain conditions are met, maybe. So in practice you can't rely on it. --http://programmers.stackexchange.com/questions/272061/why-doesnt-java-have-optimization-for-tail-recursion-at-all
If Scala supports it, and it generates byte-code, it seems like Java could support it as well. And there are vague references to Java 8 supporting it under the right conditions of which I do not know.
Java does not support TCO at compiler level but it is possible to implement it with Java 8 using lambda expressions. It is described by Venkat Subramaniamin in "Functional Programming in Java". --http://stackoverflow.com/questions/22866491/does-java8-have-tail-call-optimization
I guess I have to buy Venkat Subramaniamin book to find out exactly how/when tail call recursion is supported.
And according to this article, it is not yet supported.http://jvmgeek.com/2015/04/02/java-8-functional-programming/
Is it? Is it not? I still don't know but am leaning to not. If that is the case, then Scala has a really large advantage in functional programming compared to Java.
The use of tail recursion in Scala is fairly limited, because the JVM instruction set makes implementing more advanced forms of tail recursion very difficult. --Odersky, Martin; Spoon, Lex; Venners, Bill (2010-12-13). Programming in Scala: A Comprehensive Step-by-Step Guide (Kindle Locations 3200-3201). Artima Press. Kindle Edition.
Scala does the best it can with the byte code options that it is given.

No comments:

Post a Comment

Kafka and Cassandra support, training for AWS EC2 Cassandra 3.0 Training