Avinash
Avinash

Reputation: 13257

How is Scala suitable for Big Scalable Application

I am taking course Functional Programming Principles in Scala | Coursera on Scala. I fail to understand with immutability , so many functions and so much dependencies on recursion , how is Scala is really suitable for real world applications.

I mean coming from imperative languages I see a risk of StackOverflow or Garbage Collection kicking in and with multiple copies of everything I am running Out Of Memory

What I a missing here?

Upvotes: 0

Views: 384

Answers (2)

yǝsʞǝla
yǝsʞǝla

Reputation: 16412

  • Stack overflow: it's possible to make your recursive function tail recursive. Add @tailrec from scala.annotation.tailrec to make sure your function is 100% tail recursive. This is basically a loop.
  • Most importantly recursive solutions is only one of many patterns available. See "Effective Java" why mutability is bad. Immutable data is much better suitable for large applications: no need to synchronize access, client can't mess with data internals, etc. Immutable structures are very efficient in many cases. If you add an element to the head of a list: elem :: list all data is shared between 2 lists - awesome! Only head is created and pointed to the list. Imagine that you have to create a new deep clone of a list every time client asks for.
  • Expressions in Scala are more succinct and maybe more lazy - create filter and map and all that applied as needed. You can do the same in Java but ceremony takes forever so usually devs just create multiple temp collections on the way.
  • Martin Odersky defines mutability as a dependence on time/history. That's very interesting because you can use var inside of a function as long as no other code can be affected in any way, i.e. results are always the same.
  • Look at Option[T] and compare to null. Use them in for comprehensions. Exception becomes really exceptional and Option, Try, Box, Either communicate failures in a very nice way.
  • Scala allows to write more modular and generic code with less effort compared to Java.
  • Find a good piece of Scala code and try to see how you would do it in Java - it will be self evident.

Upvotes: 3

Akhil
Akhil

Reputation: 558

Real world applications are getting more event-driven which involves passing around data across different processes or systems needing immutable data structures

In most of the cases we are either manipulating data or waiting on a resource. In that case its easy to hook in a callback with Actors

Take a look at http://pavelfatin.com/scala-for-project-euler/ Which gives you some examples on using functions like map fllter etc. Functions like these are used routinely by Ruby applications

Combination of immutability and recursion avoids a lot of stackoverflow problems. This come in handly while dealing with event driven applications

akka.io is a classic example which could have been build very concisely in scala.

Upvotes: 0

Related Questions