scala - Clojure pattern matching on argument's class -



scala - Clojure pattern matching on argument's class -

i have simple programme written in scala, operates logical expressions members of case classes, extend expr trait (code below). have functions pattern match on class of argument , perform actions.

sealed trait expr case class implication(lhs: expr, rhs: expr) extends expr { override def tostring = "(" + lhs.tostring + "->" + rhs.tostring + ")" } case class negation(body: expr) extends expr { override def tostring = "!" + body.tostring } case class conj(lhs: expr, rhs: expr) extends expr { override def tostring = "(" + lhs.tostring + "&" + rhs.tostring + ")" } case class disj(lhs: expr, rhs: expr) extends expr { override def tostring = "(" + lhs.tostring + "|" + rhs.tostring + ")" } case class variable(name: string) extends expr { override def tostring = name }

example of function:

def substitute(map: m.hashmap[string, expr]): expr = match { case variable(name) => map.getorelse(name, this) case conj(a, b) => conj(a.substitute(map), b.substitute(map)) case disj(a, b) => disj(a.substitute(map), b.substitute(map)) case implication(a, b) => implication(a.substitute(map), b.substitute(map)) case negation(a) => negation(a.substitute(map)) }

the question is: how can imitate same functionality in clojure? want distinguish class of function argument , pattern match on it, preferably guards.

the problem can solved using if, cond, condp or other conditional operators. find class utilize class or type functions.

use zippers walk , edit map tree. http://ravi.pckl.me/short/functional-xml-editing-using-zippers-in-clojure/

there many ways tag entries in map:

{:tag :variable :value 42} (defrecord variable [value])

take @ enlive inspiration - https://github.com/cgrand/enlive

scala clojure pattern-matching

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -