Skip to content

Instantly share code, notes, and snippets.

@davegurnell
Created May 13, 2019 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davegurnell/9f52d1db2378342c4843304fc9b9e0f0 to your computer and use it in GitHub Desktop.
Save davegurnell/9f52d1db2378342c4843304fc9b9e0f0 to your computer and use it in GitHub Desktop.
package code
import cats.implicits._
// Main.scala
object Main extends App {
val database = PersonDatabase.createTestDatabase
// 1. Write code to:
// - Determine whether one person knows another through a chain of intermediate friends.
//
// - Given a key person P and a number N,
// find a list of N transitive friends of P to invite to a dinner party.
//
// - Given a list of dinner party attendees, work out what to serve!
//
// 2. Change the return type of PersonDatabase to Option[Person].
// Make everything work!
//
// 3. Change the return type of PersonDatabase to Future[Person].
// Make everything work!
//
// 4. Change the return type of PersonDabase to F[Person] where F is a monad.
// Make everything work!
}
// Person.scala
case class Person(name: String, favoriteFood: String, friends: List[String])
// PersonDatabase.scala
class PersonDatabase(people: List[Person]) {
def find(name: String): Person =
people.find(_.name == name).getOrElse(throw new Exception(s"Person not found: $name"))
}
object PersonDatabase {
def createTestDatabase: PersonDatabase =
new PersonDatabase(List(
Person(name = "Alice" , favoriteFood = "Pizza" , friends = List("Bob", "Gina" )),
Person(name = "Bob" , favoriteFood = "Ice cream" , friends = List("Iris", "Liam" )),
Person(name = "Charlie" , favoriteFood = "Roast" , friends = List("Bob", "Iris" )),
Person(name = "Declan" , favoriteFood = "Pizza" , friends = List("Gina", "Juan" )),
Person(name = "Erica" , favoriteFood = "Ice cream" , friends = List("Iris", "Charlie" )),
Person(name = "Frank" , favoriteFood = "Roast" , friends = List("Liam", "Henrick" )),
Person(name = "Gina" , favoriteFood = "Pizza" , friends = List("Declan", "Bob" )),
Person(name = "Henrick" , favoriteFood = "Ice cream" , friends = List("Declan", "Erica" )),
Person(name = "Iris" , favoriteFood = "Pizza" , friends = List("Kevin", "Frank" )),
Person(name = "Juan" , favoriteFood = "Ice cream" , friends = List("Alice", "Bob" )),
Person(name = "Kevin" , favoriteFood = "Roast" , friends = List("Alice", "Gina" )),
Person(name = "Liam" , favoriteFood = "Pizza" , friends = List("Erica", "Frank" )),
))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment