Reputation: 13257
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
Reputation: 16412
@tailrec
from scala.annotation.tailrec
to make sure your function is 100% tail recursive. This is basically a loop.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.var
inside of a function as long as no other code can be affected in any way, i.e. results are always the same.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.Upvotes: 3
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