Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Good Will Hrunting
Oct 8, 2012

I changed my mind.
I'm not sorry.
Could use some help in going from my comfortable OOP/imp Java style to Scala functional style. Basically, I have a stream of events coming in, all of which are one of a set of discrete types, and for every type we would like to have a set of "enhancement" functions executed based on the type. In Java, I'd probably just have created an "Event" class and extended that for each of the events, keeping a set of implementations of an "enhancer" function and overriding the "enhance" function in each Event extensions with the execution of all of the enhancers kept in that Set. I'm struggling a bit with how to structure this "properly" with Scala. Obviously I could probably implement it very similarly to how I did it in Java, but I don't know if that's the best method.

On a similar note, while picking up syntax and understanding what's going on behind the scenes of Scala -> JVM code, picking up the functional paradigms has been hard for me.

Adbot
ADBOT LOVES YOU

Good Will Hrunting
Oct 8, 2012

I changed my mind.
I'm not sorry.
Everything could return the same event type. Basically I need to turn a JSON record like {eventType, timestamp, key} into {eventType, timestamp, key, enhancement1, enhancement2, enhancement3} where those "enhancements" are just results of running the enhancement functions which range from a few calculations to lookups in local files.

Good Will Hrunting
Oct 8, 2012

I changed my mind.
I'm not sorry.
Is "cake pattern" the only way to mock out something like this? I need to mock out MaxmindDatabases.getConnectionTypeResult in a unit test but you can't mock objects. I guess another option is to create a companion object of the class and access it like that? I'm brand new to Scala so I really can't evaluate tradeoffs of such things too well.

code:
object MaxmindDatabases {
  lazy val CONNECTION_TYPE_READER: DatabaseReader = initializeDb(ConnectionTypeFile.fileName)

  def getConnectionTypeResult(ipAddress: InetAddress): ConnectionTypeResponse = {
    CONNECTION_TYPE_READER.connectionType(ipAddress)
  }

  def initializeDb(mmdbFile: String): DatabaseReader = {
    //bunch of useless crap
  }
}

Good Will Hrunting
Oct 8, 2012

I changed my mind.
I'm not sorry.
I'm half asleep right now so I'll give this a shot tomorrow but I think the thing tripping me up was that a few things I was trying to mock were Objects which can't be mocked as easily as classes. I have a parent class that has a function with an arbitrary number of Objects being called and those functions don't really make sense as classes because they'll never have any non-instance properties or anything so I made them Scala objects. I guess I could change everything to classes, expose via companion, and mock accordingly.

Good Will Hrunting
Oct 8, 2012

I changed my mind.
I'm not sorry.
What's the most efficient way in Scala to do pass a String through a number of possible regex pattern and match based on which pattern is matched?

code:
  val A: Regex = "([^/]+)/([.0-9]+) \\((iPhone|iPad); iOS ([.0-9]+); Scale/[.0-9]+\\)".r
  val B: Regex = "(69 420 gains)".r

  def resolveCustomRegex(ua: String): Data = {
    ua match {
      case A => //need the captured groups here
      case B => //need the captured groups here
    }
  }
This seems like it would be less efficient than piping the two together like ([^/]+)/([.0-9]+) \\((iPhone|iPad); iOS ([.0-9]+); Scale/[.0-9]+\\) | (69 420 gains) but in the second case capturing match groups doesn't seem feasible. Also I can't figure out how to get the groups in each case's corresponding function

  • Locked thread