• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Does Scala give benefit of shorter code only because functions are first class unlike Java?

 
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The primary advantage of using Scala is that in Scala we need to write lesser code. For example to achieve the same result as in Java, we require to write lesser code. Is this advantage only due to the reason that in Scala functions are first class citizens unlike say Java.  

Thanks.
 
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:The primary advantage of using Scala is that in Scala we need to write lesser code. . . . .

No, it isn't. It is that you have a language with functional features from the word “go”.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Are these 2 things not the same. As Scala has functional features from the word “go”,  so it requires us to write less code as compared to say Java for this reason.
 
Bartender
Posts: 3519
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "functional features from the word “go”" mean?
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means functions are first class citizens I like languages like Java.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the typo. Unlike languages like Java functions are first class citizens In Scala.
 
Saloon Keeper
Posts: 10669
228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Thanks. Are these 2 things not the same. As Scala has functional features from the word “go”,  so it requires us to write less code as compared to say Java for this reason.


No. How terse the code is has nothing to do with whether it's functional or procedural. Java is just a wordy language, even compared to other imperative languages.

The benefit of a functional language is that it's declarative: You say *what* you want to do, not *how* you want to do it. That's what we also strive to do in object oriented programming, but it's easier in a declarative language. There usually aren't any mutable types either, and side effects are rare, making it harder to introduce bugs.
 
Piet Souris
Bartender
Posts: 3519
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

Then I would say: no, it is not that functions are first class citizens. I have not experienced that Java's functions are more verbose than Scala's. It is just that you can write many things much shorter in Scala.
For instance: a simple class with a few variables is just one line in Scala, but 10 or so in Java.

I remember having a Scala program of 50 lines. I translated that to Java 8, trying to get it as short as I could (without sacrificing readability), and however hard I tried, I could not get the Java version under 150 lines.

But that is Java.


Edit: this is my reply to Monica.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks.

I do not understand how it has to not do with functional nature of Scala and has to do with declarative nature of Scala.

Example. In Spark Scala word count we require Map whereas in Spark Java word count we require MapToPair. I think the differences are because of functions not being first class citizens In Java unlike Scala.

Is this not because of functional nature of Scala ( functions being first class citizens In Scala unlike Java)?
 
Stephan van Hulst
Saloon Keeper
Posts: 10669
228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody said declarative languages are necessarily shorter.

The fact that Spark for Java requires a mapToPair function while Spark for Scala doesn't, is because Scala supports "structural typing" which is similar to "ducktyping". I explained this in one of your earlier topics.

This has absolutely nothing to do with functional languages. Imperative languages can also have ducktyping. Java just doesn't. Java is strongly and statically typed, and sure enough, that is a reason why it's more verbose.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Is ducktyping one of the primary advantages of Scala over languages such Java?.
 
Stephan van Hulst
Saloon Keeper
Posts: 10669
228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't say advantage. It's just different. It has advantages and disadvantages.

Ducktyping makes your code smaller, and lets you focus on the 'real' logic of the application instead of performing typesafe conversions.

Strong static typing requires more effort and code on the part of the programmer to write a program that the compiler will accept, but the compiler will also be much more helpful in determining whether you made some silly mistake. It also makes it much easier to look up the documentation of a field or method that is mentioned in the code.

Personally I'd take strong typing over ducktyping any day of the week.
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!