Archivo de la categoría: Uncategorized

Scalera tips: How NOT to change your actor’s state

Intedezting, as a newbie, I would have done it wrong

Scalera

A common key when working with Akka, is modifying properly our actor’s state. If we jog our memory, this framework paradigm allows us to program modeling the concurrency based on actors and message passing. From that base, we could define an actor as a computing unit that may have state and perform tasks based on messages that it will receive through its mailbox and that will be processed sequentially .

That means that, in order to avoid side effects, the actor’s state modification has to take place when processing a message. So senseful so far. However, it’s a pretty common mistake to do something similar to this:

In this case, we have no more warranty that the state change (whose only responsible of keeping it consistent and thread safe is the actor) might cause side efects given that, in the precise moment where the Future modifies the var, it’s…

Ver la entrada original 139 palabras más

Anuncios

Tipos dinámicos

Scalera

Hoy traemos un poco más de magia negra. Leyendo el otro día un poco sobre los procesos de mejora de Scala (SIPs) me encontré algo que realmente me llamó la atención: tipos dinámicos.

¿Qué son?

Los tipos dinámicos permiten definir el comportamiento de un objeto cuando se invoca un método o se intenta acceder a un miembro que no existe. Por ejemplo:

dará un fallo en tiempo de compilación. Con los tipos dinámicos podemos decir que en caso de que se invoque a un método, se llame como se llame, devuelva cierto valor.

¿Cómo funcionan?

Para poder utilizar los tipos dinámicos, es preciso importar import scala.language.dynamics y extender del trait Dynamic:

La clase (o trait) que definamos puede tener todos los atributos y métodos que queramos, pero además podemos implementar los siguientes métodos:

applyDynamic

este método se encarga de gestionar las llamadas a métodos de la instancia.
Si queremos…

Ver la entrada original 340 palabras más

Parallel MapReduce in Python in Ten Minutes

Some years old but nicely explained 😀

Cvet's Blog

Almost everyone has heard of Google’s MapReduce framework, but very few have ever hacked around with the idea of map and reduce. These two idioms are borrowed from functional programming, and form the basis of Google’s framework. Although Python is not a functional programming language, it has built-in support for both of these concepts.

A map operation involves taking a function f and applying it on a per-element basis to a given list L. For example, if f was the square-root function, then the map would take the square of each element in L. A reduce operation (also known as folding) is similar in that it also applies a function g to a given list L, but instead of isolating on each element, g systematically accumulates or collapses the contents of L into a single result. The canonical example of this would g performing a…

Ver la entrada original 876 palabras más