You are on page 1of 22

Programación Scala

Carlos Andrés Méndez

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
▪ Review
▪ Functional programming
▪ Scala
▪ Scala Akka

Operating System Concepts – 10th Edition 4.2 Silberschatz, Galvin and Gagne ©2018
Functional programming

1. Functional programming aims to avoid side effects.

Operating System Concepts – 10th Edition 5.3 Silberschatz, Galvin and Gagne ©2018
Functional programming

• The only observable output is the return value.


• The only output dependency are the arguments.
• Arguments are fully determined before any output is generated.

Operating System Concepts – 10th Edition 5.4 Silberschatz, Galvin and Gagne ©2018
Functional programming

2. Functional programming avoids concepts such as state.


3. Functional programming promotes immutable data.
- It is simpler to construct concurrent systems.
4. Functional programming promotes declarative programming.

Operating System Concepts – 10th Edition 5.5 Silberschatz, Galvin and Gagne ©2018
Functional programming

5. Recursion is a natural control structure.


Functional programming languages often incorporate tail end recursive
optimizations to convert recursive routines into iterative ones at runtime.

Operating System Concepts – 10th Edition 5.6 Silberschatz, Galvin and Gagne ©2018
Example: Lisp programming language

Lisp
Programming
language
Originally specified in
1958
Multi-paradigm:
functional, procedural,
reflective, meta

Operating System Concepts – 10th Edition 5.7 Silberschatz, Galvin and Gagne ©2018
Functional programming: disadvantages
▪ Input–output is harder in a purely functional language.
▪ Functional programming languages have tended to be less efficient on
current hardware platforms.
• This is partly because current hardware platforms are not designed with
functional programming in mind.
• However, this has changed to a large extent with Scala and the
functional language Heskell.
▪ Not data oriented.
• Such data can be naturally represented via objects in an Object
Oriented language.
▪ Programmers are less familiar.
▪ Ivory tower languages.

Operating System Concepts – 10th Edition 5.8 Silberschatz, Galvin and Gagne ©2018
Example 2: Clojure programming language

Clojure is a modern, dynamic, and functional dialect of the Lisp programming


language on the Java platform. Like other Lisp dialects, Clojure treats code as
data and has a Lisp macro system. The current development process is
community-driven, overseen by Rich Hickey as its benevolent dictator for life.
Multi-paradigm: agent-oriented, concurrent, functional, logic, macro, pipeline

Operating System Concepts – 10th Edition 5.9 Silberschatz, Galvin and Gagne ©2018
What Is Scala?

▪ The name Scala is derived from Sca(lable) La(nguage) and is a multi-


paradigm language, incorporating Object-Oriented approaches with functional
programming.
▪ Scala source code is intended to be compiled to Java bytecode, so that the
resulting executable code runs on a Java virtual machine.
▪ Scala provides language interoperability with Java, so that libraries written
in either language may be referenced directly in Scala or Java code.

Operating System Concepts – 10th Edition 5.10 Silberschatz, Galvin and Gagne ©2018
Scala genealogy

Operating System Concepts – 10th Edition 5.11 Silberschatz, Galvin and Gagne ©2018
Scala genealogy: Prolog

Prolog is a logic programming language.


The program logic is represented as facts and rules.

Operating System Concepts – 10th Edition 5.12 Silberschatz, Galvin and Gagne ©2018
Scala documentation

Operating System Concepts – 10th Edition 5.13 Silberschatz, Galvin and Gagne ©2018
Scala features: Type inference

Operating System Concepts – 10th Edition 5.14 Silberschatz, Galvin and Gagne ©2018
Scala class vs Java class

Operating System Concepts – 10th Edition 5.15 Silberschatz, Galvin and Gagne ©2018
Scala class vs Java class

Operating System Concepts – 10th Edition 5.16 Silberschatz, Galvin and Gagne ©2018
Scala Akka

▪ Akka is an open-source library that helps to easily develop concurrent and


distributed applications using Java or Scala by leveraging the Actor Model.
▪ The Actor model of concurrency (which dates from 1973) is based on the
idea of having independent concurrent Actors that receive and send
asynchronous messages and that perform some behaviour based on these
message requests.

Operating System Concepts – 10th Edition 5.17 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 5.18 Silberschatz, Galvin and Gagne ©2018
Scala Akka

▪ Older versions of Scala also had their own scala.actor implementation of the
Actor model —however this is now deprecated and the recommendation is
to use the Akka implementation.
▪ These actors can hold their own state and behaviour. However, ideally only
immutable data is exchanged between them.
▪ The key idea underpinning the Actor model is that most of the problems with
concurrency, from deadlocks to data corruption, result from having shared
state.

Operating System Concepts – 10th Edition 5.19 Silberschatz, Galvin and Gagne ©2018
Actor model key principles
• No shared state.
• Lightweight processes.
• Asynchronous message passing.
• Buffering for incoming messages via an a inbox in which it receives messages.
• An actor can create other actors.
• No shared mutable data.
• No blocking operations.
• Any process can send a message to an actor.
• An actor does not do anything unless/until it receives a message.

Operating System Concepts – 10th Edition 5.20 Silberschatz, Galvin and Gagne ©2018
Actor model key principles
Messages are sent between actors and these messages are queued in an inbox in
a similar manner to email messages.

Operating System Concepts – 10th Edition 5.21 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 5.22 Silberschatz, Galvin and Gagne ©2018

You might also like