Skip to content Skip to sidebar Skip to footer

Should I Change To Scala To Create A System With Rewrite Rules?

I have some classes that I developed that I am using in a Android application. I have about 6-7 classes in that core, some of them are abstract classes with abstract methods. Those

Solution 1:

Yes, it is easy to write such rules in Scala, and, in fact, there have been some questions on Stack Overflow related to rule rewriting systems in Scala. Also, there are some libraries that may help you with this, related to strategic programming and nlp, but I haven't used them, so I can't comment much.

Now, I don't know exactly where these classes are coming from. If you are parsing and building them, the parser combinator library can trivially handle it:

sealed trait Expr { def value: Int }
caseclassNumber(value: Int) extendsExprcaseclassSum(e1: Expr, e2: Expr) extendsExpr { def value = e1.value + e2.value }

objectExampleextends scala.util.parsing.combinator.RegexParsers {
  def number: Parser[Expr] = """\d+""" ^^ (n =>Number(n.toInt))
  def sum: Parser[Expr] = number ~ "+" ~ expr ^^ {
    case n ~ "+" ~ exp =>Sum(n, exp)
  }
  def expr: Parser[Expr] = sum | number
}

If you have these classes in some other way and are applying simplifications, you could do it like this:

def simplify(expr: List[Expr]): Expr = expr match {
  case expr :: Nil =>List(expr) // no further simplificationcase (n1: NumberProvider) :: Plus :: (n2: NumberProvider) :: rest =>simplify(SumProvider(n1, n2) :: rest)
  case (n: NumberProvider) :: Plus :: (s: SumProvider) :: rest =>simplify(SumProvider(n, s) :: rest)
  case (s: SumProvider) :: Plus :: (n: NumberProvider) :: rest =>simplify(SumProvider(s, n) :: rest)
  caseother => other // no further simplification possible
}

The important elements here are case classes, extractors and pattern matching.

Solution 2:

As a lone developer, Scala is expressive and powerful, so once mastered can be satisfying to write less and do more -- less boilerplate code, more compact idioms.

However, the power of Scala does come at a cost: it is a totally different language, with different (and I'd say more complex) idioms and syntax from Java. Java was designed to be intentionally simple, with the idea being that in larger organizations with more code being shared among developers, explicitness and syntax simplicity are more valuable than brilliantly concise code.

Java made an intentional choice to provide a smaller toolset to make it as quick and easy as possible for one developer to pick up where another left off, so in that sense it's geared towards team development. Scala however gives you a bit more rope to make concise but less immediately obvious constructs, which can be a minus for large enterprise environments.

Currently, Scala also has a smaller developer pool than Java, which means fewer examples and a smaller talent pool if you ever intend to hire a development team.

But as a lone developer on a solo project or in a small tight-knit team, Scala can be fun and fast to code with once you get over the significant learning hump.

Solution 3:

If you are switching to Scala switch to it for everything you can. There is hardly a point in using Java.

Is it worth the investment? From what one can read on the web (and my own impression) you won't become faster with Scala, but you will learn a lot.

So if you are only concerned with development speed: ignore Scala.

If you want to learn: Scala is a great choice as the next language to learn and use.

Post a Comment for "Should I Change To Scala To Create A System With Rewrite Rules?"