Created
May 19, 2014 06:35
-
-
Save jedws/f4b0f24e851856689d52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test | |
// Type definitions | |
object Types { | |
type Tag = String | |
} | |
import Types._ | |
// Marker definitions | |
case class OperatingHours(hours: String) | |
case class Metadata(url: Option[String], hours: Option[OperatingHours]) | |
case class Marker(id: String, name: String, lat: Double, lng: Double, symbol: String, tags: List[Tag]=List.empty[Tag], metadata: Option[Metadata]=None) | |
// Filter definntions | |
trait FilterPrimitive | |
trait FilterLogic extends FilterPrimitive | |
case class TagFilter(tag: String) extends FilterPrimitive | |
case class NotFilter(filter: FilterPrimitive) extends FilterLogic | |
case class AndFilter[F <: FilterPrimitive, G <: FilterPrimitive](left: F, right: G) extends FilterLogic | |
case class OrFilter(left: FilterPrimitive, right: FilterPrimitive) extends FilterLogic | |
trait FilterConversions { | |
implicit def TagFilterHandler(m: Marker, f: TagFilter) = m.tags contains f.tag | |
implicit def AndFilterHandler[F <: FilterPrimitive, G <: FilterPrimitive](m: Marker, f: AndFilter[F, G])(implicit F: ((Marker, F) => Boolean), G: ((Marker, G) => Boolean)) = | |
Filter(m, f.left) && Filter(m, f.right) | |
} | |
object Filter { | |
def apply[F](m: Marker, f: F)(implicit filterHandler: ((Marker, F) => Boolean)): Boolean = { | |
filterHandler(m, f) | |
} | |
def indirectApply[F <: FilterPrimitive](m: Marker, f: F)(implicit filterHandler: ((Marker, F) => Boolean)) = | |
apply(m, f) // Why doesn't this work? answer, needs implicit | |
} | |
object MyApp extends App with FilterConversions { | |
var f = AndFilter(TagFilter("food-truck"), NotFilter(TagFilter("restaurant"))) | |
var m = Marker(id="0001", name="Abu Nawas", lat=22.399474275956216, lng=39.08273218518074, symbol="/images/restaurants/logo_abunawas-180x100.png", tags=List("stationary", "restaurant")) | |
println(s"Filter: ${Filter(m, TagFilter("stationary"))}") | |
println(s"Filter: ${Filter(m, TagFilter("mobile"))}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment