Skip to content

Instantly share code, notes, and snippets.

@kamiyaowl kamiyaowl/LSystem.scala
Last active Aug 29, 2015

Embed
What would you like to do?
ScalaでL-System
===== L-System =====
(algae,(A,Map(A -> AB, B -> A)))
(fib,(A,Map(A -> B, B -> AB)))
(koch,(F,Map(F -> F+F-F-F+F)))
(sierpinski,(F,Map(F -> F-F-F-GG, G -> GG)))
rule> algae
count > 4
result>ABAABABA
===== L-System =====
(algae,(A,Map(A -> AB, B -> A)))
(fib,(A,Map(A -> B, B -> AB)))
(koch,(F,Map(F -> F+F-F-F+F)))
(sierpinski,(F,Map(F -> F-F-F-GG, G -> GG)))
rule> fib
count > 4
result>ABBAB
===== L-System =====
(algae,(A,Map(A -> AB, B -> A)))
(fib,(A,Map(A -> B, B -> AB)))
(koch,(F,Map(F -> F+F-F-F+F)))
(sierpinski,(F,Map(F -> F-F-F-GG, G -> GG)))
rule> count > result>F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F+F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F+F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F+F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F+F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F+F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F+F+F-F-F+F+F+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-F-F-GG-F-F-F-GG-F-F-F-GG-GGGG-F-F-F-GG-F-F-F-GG-F-F-F-GG-GGGG-F-F-F-GG-F-F-F-GG-F-F-F-GG-GGGG-GGGGGGGG-GGGGGGGGGGGGGGGG
import scala.language.postfixOps
object LSystem {
implicit class ReplaceSystem(self:String) {
def /(implicit rule:Map[Char,String]) = self map(r) mkString
def >>(n:Int)(implicit rule:Map[Char,String]):String = {
if(n == 0) self
else self./.>>(n-1)(rule)
}
}
def r(c:Char)(implicit rule:Map[Char,String]) = rule.get(c) match {
case Some(n) => n
case None => c
}
def main(args:Array[String]) = {
val rules = Map( "algae" -> ("A", Map('A' -> "AB", 'B' -> "A")),
"fib" -> ("A", Map('A' -> "B" , 'B' -> "AB")),
"koch" -> ("F", Map('F' -> "F+F-F-F+F")),
"sierpinski" -> ("F", Map('F' -> "F-F-F-GG", 'G' -> "GG")));
while(true)
{
println("\n===== L-System =====")
rules foreach(println)
print("\nrule> ")
val key = readLine
rules.get(key) match {
case Some(r) => show(r)
case None => println("retry")
}
}
def show(s:(String,Map[Char,String])) = {
implicit val rule = s._2
print("count > ")
val count = readLine.toInt
print("result>")
println(s._1 >> count)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.