📜 ⬆️ ⬇️

Transition from Java to Scala (Clojure, Haskell, Erlang ..) as an increase in programming abstraction

And what of this can be written on Scala, which is impossible on Java?
(from a conversation with a friend of mine, a man and a programmer)
It’s a different way to learn.
Chad fowler

I want to tell you not about the simplicity of Scala constructions compared to Java and not about the fact that in 1 line of Scala I can fit 20 lines of Java . On the contrary, to dig deeper, drop the foundations of the PLO and look at the reaction of the noble public.

Literally 1.5-2 months ago I asked myself the exact same question that stands under the heading. And just recently, I finally swung at Ulyam of our Shakespeare Martin Oderski . At this place, harsh Scala programmers with experience can condescendingly smile — they say a week without a year — and already write Scala articles. Yes, even in Habr himself. And nevertheless, let me continue - maybe it will be of interest to you.
I will start from afar, with a rhetorical question: what is the basis of human progress? Well, or at least confine to programming languages. If you ignore the syntax, punctuation and see the root? Well, yes, of course - to increase the level of abstraction.
When they switched from machine code to assembler, it became better to write, it became more fun to write.
And what is asm opposite basic? It’s like a carpenter against a carpenter against a man. The programmer has ceased to know the device registers, he began to think variables! Let us leave aside the nerve side effect of the profanation of this high profession, when every Hindu schoolboy could call himself a programmer.
Further more. Fortran, procedural programming, formal and actual variables, libraries of procedures appeared! Those who rode the tram for 3 kopecks may remember a continuous listing of the spaghetti code and goto.
Next step: data structures. Stop thinking variables! Give the structure-objects! If you write in the style of OOP and in some place roll down to primitives, for example, in a cache or in a configuration, several times I have met this - think about it. Are you at the same level of abstraction with your language? OOP allows you to think of a business model of your task as objects. No need to say: copy the author A to the author B, the date A to the date B and the comment A to the comment B, etc. Tell me - copy the document A to the document B. Even if there is a copy of the fields below, the programmer already thinks with the documents, and not with his fields.
With this, let me finish the long intro and go to the start:

What is this abstraction in these functional languages, which is not in the imperative ones?


Take a simple example of a simple problem: whether the number N is simple , a translation from the cycle of translated articles on IBM about functional programming where, in my opinion, the advantage of the functional approach was not convincing. I rephrase the problem in the programmer's language:
Determine whether in the sequence from 2 to sqrt (N) there is such a number that the division of N into it would be completely without remainder.
FF, the javist will say and will correctly do:
int sqrt=(int) Math.sqrt(N); //   ,       for(int i=2; i<=sqrt; i++){ if( N % i ==0){ return false; } } return true; 
If you now ask what kind of abstraction he thought, he will easily answer: with his head, a loop and an operation on a counter.
And now the culmination: a functional programmer will not think so petty. He can translate the task into the Sacla code verbatim (the same answer is in the comments on Habré):
  !(2 to math.sqrt(N).toInt).exists(N % _ == 0) 
Handsomely? Yes, of course, but ... Maybe someone will convince, of course ... Well, for the doubters, I will try to hammer the last nail into their head with the lid of their foundations nepoboba of the firmness of the PLO: operation par on the collection. In one small word of 3 letters, the Scala programmer can send java to parallelize the calculations on any collection. For example:
 !(2 to math.sqrt(N).toInt).par.exists(N % _ == 0) 
On complex tasks and 8 processors for example - the speed increase is certainly not 8 times, but about that. And all because Scala thinks not the elements of the collection, but the whole of it. Then one could announce a contest for the minimum code for Java, which will repeat this trick. But if you did not rush to try to repeat it on java, then my efforts were not in vain.
And finally, after a happy ending, let me add some more intrigue: the next abstraction of Scala is still ripening in my head and, I hope, will be splashed out in the next article.
Thanks for attention.

')

Source: https://habr.com/ru/post/168425/


All Articles