Last active
January 14, 2018 18:21
-
-
Save waxzce/411bc69b1b41bea557beee38b999a5b0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
name := """minimal-scala""" | |
version := "1.0" | |
scalaVersion := "2.11.7" | |
// Change this to another test framework if you prefer | |
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.4" % "test" | |
// Uncomment to use Akka | |
libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.3.11" | |
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.5.6" | |
fork in run := true |
This file contains hidden or 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 com.example | |
object Hello { | |
trait CoffeeDrinker { | |
def getName: String | |
def drinkCoffee = println(getName + " drinks coffe") | |
} | |
sealed trait Gender | |
case object Male extends Gender | |
case object Female extends Gender | |
case class Trans(pref:Gender) extends Gender | |
case class FrankCreature(name:String){ | |
def getName = name | |
} | |
abstract class Person(val name: HumanName, val age: Int, val gender:Gender) extends CoffeeDrinker { | |
def getName = name | |
} | |
type HumanName = String | |
type Students = Set[Student] | |
case class Student(override val name: HumanName, override val age: Int, override val gender:Gender) extends Person(name, age, gender){ | |
def this(name:String, age:Int) = this(name, age, Male) | |
} | |
case class Teacher(override val name: HumanName, override val age: Int, override val gender:Gender = Female, liveInNantes:Boolean = true) extends Person(name, age, gender) | |
case class School(students: Students, teachers: List[Teacher]) { | |
def persons: Set[Person] = students ++ teachers | |
def ages: Set[Int] = persons.map(_.age) | |
def ages(f:Int => Int):Set[Int] = { | |
ages.map(f) | |
} | |
def moyenne: Int = ages.foldLeft(0)((acc, curr) => acc + curr) / ages.size | |
} | |
def ages(sch: School): Set[Int] = sch.persons.map((p: Person) => p.age) | |
def main(args: Array[String]): Unit = { | |
implicit class ToStudent(t:(String, Int)){ | |
def toStudent = { | |
Student(t._1, t._2, Male) | |
} | |
} | |
val students = Set(Student("Foo", 18, Female), Student("Bar", 29, Trans(Female)), | |
("yolo", 42).toStudent | |
) | |
val mymap = Map( | |
"hey1" -> 3, | |
"key2" -> 4 | |
) | |
Student("Foo", 18, Female).gender match { | |
case Male => println("c'est un homme") | |
case Female => println("c'est une femme") | |
case Trans(_) => println("c'est un/une trans") | |
} | |
val kweenie = new FrankCreature("kweenie") | |
val frankenstein = new FrankCreature("johnny") with CoffeeDrinker | |
frankenstein.drinkCoffee | |
println(frankenstein.getClass()) | |
println(kweenie.getClass()) | |
val school = School( | |
students, | |
List(Teacher("Baz", 12, liveInNantes = false, gender= Male))) | |
println(ages(school)) | |
println(school.moyenne) | |
println(school.ages(_+1)) | |
school.persons.filter(_ match { | |
case Student(name, age, gender) if age > 20 => true | |
case _ => false | |
}) | |
val l = List("a", "b", "c") | |
l match { | |
case List("a" , _ , "d") => println("matched") | |
case "a" :: _ :: _ => println("matched") | |
} | |
import play.api.libs.json.Json | |
val json = Json.obj( | |
"plop" -> 23 | |
) | |
println(Json.prettyPrint(json)) | |
println((json.toString)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment