Posts

Showing posts from October, 2015

Harness Scala Type Classes and Implicits

In my  previous blog , I presented an Expression ADT. In this article I will extend its functionality and serialize it using other ADT while maintaining decoupling using Scala's magic a.k.a implicits and type classes . Full code is in  this repository . Let's start by building our JSON serializer ADT sealed trait JSON case class JSeq ( elms : List [ JSON ]) extends JSON case class JObj ( bindings : Map [ String , JSON ]) extends JSON case class JNum ( num : Double ) extends JSON case class JStr ( str : String ) extends JSON case class JBool ( b : Boolean ) extends JSON case object JNull extends JSON and now we can create our JSONWriter to convert JSON objects to nice JSON String object JSONWriter { def write ( j : JSON ) : String = { j match { case JSeq ( e ) => e . map ( write ). mkString ( "[" , "," , "]" ) case JObj ( obj ) => obj . map ( o => &q

Filtering using Scala Parser Combinators

Filtering using Scala Parser Combinators So I got interesting task to enable record filtering according to some rules that we get from configuration represented as text. well so far we know two things : 1. we need a filter that accepts a record as an input and might return a record as its output : def filter ( record : Record ) : Option [ Record ] 2. we need to get a text with some kind of rule e.g  "foo = 'FOO'" , "bar = 8", "buz IS NOT NULL "  and AND / OR combinations . where foo/bar/buz are record's attributes * well I know that "null" is a dirty word in FP but that was the requirement since it was a mix-in with java project. Following  the rule of three  so is our todo list : parse the text evaluate it's Boolean expression in a form of Abstract Syntax Tree. filter according the Boolean evaluation. The complete Source Code can be found  here  (you are more than welcome to contribute ) Parser Combin