Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Scala, does a functional paradigm make sense for analyzing live data?

Tags:

scala

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

like image 648
deltanovember Avatar asked Aug 28 '11 04:08

deltanovember


1 Answers

You might want to look into Functional Reactive Programming. 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.

like image 142
hammar Avatar answered Oct 11 '22 07:10

hammar