Skip to content

Instantly share code, notes, and snippets.

@timperrett
Created October 7, 2011 11:57
Show Gist options
  • Select an option

  • Save timperrett/1270134 to your computer and use it in GitHub Desktop.

Select an option

Save timperrett/1270134 to your computer and use it in GitHub Desktop.
// these are the data types
trait Tense
case object Past extends Tense
case object Present extends Tense
case object Future extends Tense
trait Subject {
protected val usage: Map[Tense, String]
def apply(tense: Tense): String = usage(tense)
}
case object Je extends Subject {
protected val usage: Map[Tense, String] = Map(Past -> "je", Present -> "j'ai", Future -> "je")
}
case object Ils extends Subject {
protected val usage: Map[Tense, String] = Map.empty
}
// it seemed reasonable for verbs to be able to stem themsevles
// as (as far as I know) the stemming is the first thing done before
// applying any of the conjugations
case class Verb(given: String, tense: Tense, subject: Subject){
val suffix: Option[String] = List("e", "es", "ons", "er").find(given.endsWith(_))
def asStem = this.copy(given = suffix.map(given.stripSuffix(_)).getOrElse(given))
def inFrench: String = "%s %s".format(subject(tense), given)
}
val say: Verb => Verb = { verb =>
def suffix(letters: String) = verb.copy(given = verb.asStem.given + letters)
(verb.suffix, verb.subject) match {
case (Some("er"), Je) => suffix("e")
case (Some("er"), Ils) => suffix("ez")
case _ => verb // FIXME: pass through for all other cases
}
}
println {
// je regarde
say(Verb("regarder", Past, Je)) inFrench
// j'ai regarde
say(Verb("regarder", Present, Je)) inFrench
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment