Usando Scala, ¿tiene sentido un paradigma funcional para analizar datos en vivo?

For example, when analyzing live stockmarket data I expose a method to my clients

def onTrade(trade: Trade) {

The clients may choose to do anything from counting the number of trades, calculating averages, storing high lows, price comparisons and so on. The method I expose returns nothing and the clients often use vars and mutable structures for their computation. For example when calculating the total trades they may do something like

var numTrades = 0

def onTrade(trade: Trade) {
    numTrades += 1

A single onTrade call may have to do six or seven different things. Is there any way to reconcile this type of flexibility with a functional paradigm? In other words a return type, vals and nonmutable data structures

preguntado el 28 de agosto de 11 a las 04:08

This is a good question though personally I would probably expose onTrade and let the client choose the level of functionalness. I'd be interested to hear how functional you can get this way. -

3 Respuestas

Es posible que desee mirar en Programación funcional reactiva. Using FRP, you would express your trades as a stream of events, and manipulate this stream as a whole, rather than focusing on a single trade at a time.

You would then use various combinators to construct new streams, for example one that would return the number of trades or highest price seen so far.

The link above contains links to several Haskell implementations, but there are probably several Scala FRP implementations available as well.

Respondido 28 ago 11, 10:08

There are two main FRP implementations in Scala under development, scala.react by Ingo Maier reactive by Naftoli Gugenheim. If you google, you should find papers and talks about react by Ingo. Naftoli's reactive está disponible aquí: See also this mailing list thread: - Kipton Barros

Una posibilidad es usar monads to encapsulate state within a purely functional program. You might check out the Biblioteca Scalaz.

Además, según informes, the Scala team is developing a compiler plug-in for an effect system. Then you might consider providing an interface like this to your clients,

def callbackOnTrade[A, B](f: (A, Trade) => B)

The clients define their input and output types A y B, and define a pure function f that processes the trade. All "state" gets encapsulated in A y B and threaded through f.

Respondido 28 ago 11, 09:08

I guess it's cause I do Haskell more than Scala, but I sort of feel like using monads here is just as imperative as onTrade. Though maybe you could make a custom monad less imperative than IO (or whatever the scalaz equivalent is). - Owen

Owen: don't think of the IO monad, but of the Haskell State monad (careful: I do mean Stateno, ST!). State s a is equivalent to `s -> (a, s)'. - Blaisorblade

Callbacks may not be the best approach, but there are certainly functional designs that can solve such a problem. You might want to consider FRP or a state-monad solution as already suggested, actors are another possibility, as is some form of dataflow concurrency, and you can also take advantage of the copy method that's automatically generated for case classes.

A different approach is to use STM (software transactional memory) and stick with the imperative paradigm whilst still retaining some safety.

Al el albergue mejor calificado approach depends on exactly how you're persisting the data and what you're actually doing in these state changes. As always, let a profiler be your guide if performance is critical.

Respondido 28 ago 11, 12:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.