Skip to content

Instantly share code, notes, and snippets.

@mikeedwards83
Forked from timperrett/conjugations.scala
Created October 8, 2011 20:16
Show Gist options
  • Save mikeedwards83/1272817 to your computer and use it in GitHub Desktop.
Save mikeedwards83/1272817 to your computer and use it in GitHub Desktop.
import org.omg.CosNaming.NamingContextPackage.NotFound
// these are the data types
trait Tense{
}
case object Past extends Tense
case object Present extends Tense
case object Future extends Tense
trait Subject{
}
case object Je extends Subject
case object Ils extends Subject
trait Verb{
protected val trim : Map[Tense, String]
protected val infin: String
protected val subjects : Map[Subject, Map[Tense,(String,String)]]
protected def getConstructs(tense: Tense, subject:Subject) = subjects(subject)(tense)
def conjugate(subject: Subject, tense: Tense)={
val constructs = getConstructs(tense, subject)
"%s %s%s".format(constructs._1, infin.stripSuffix(trim(tense)), constructs._2)
}
}
case class ErVerb(infinitive:String) extends Verb{
protected val trim : Map[Tense, String] = Map(Present-> "er", Past->"er")
protected val infin = infinitive
protected val subjects: Map[Subject, Map[Tense,(String,String)]] = Map(
Je -> Map(Present -> ("Je","e"), Past -> ("J'ai", "e")),
Ils -> Map(Present -> ("Ils","ent"), Past ->("Ils ont","e"))
)
}
case class IrVerb(infinitive:String) extends Verb{
protected val trim : Map[Tense, String] = Map(Present->"ir", Past->"r")
protected val infin = infinitive
protected val subjects: Map[Subject, Map[Tense,(String,String)]] = Map(
Je -> Map(Present -> ("Je","is"), Past -> ("J'ai", "")),
Ils -> Map(Present -> ("Ils","issent"), Past ->("Ils ont",""))
)
}
implicit def stringToVerb(verb:String) = {
if( verb.endsWith("ir")) IrVerb(verb)
else if( verb.endsWith("er")) ErVerb(verb)
else throw new NullPointerException("") //just some random error
}
println {
// je regarde
"regarder" conjugate(Je, Past)
"regarder" conjugate(Ils, Past)
"finir" conjugate(Je, Past)
"finir" conjugate(Ils, Past)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment