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
| import scala.annotation.tailrec | |
| /** width by height grid | |
| starting top left | |
| how many ways to travel to bottom right? | |
| can only go right or down | |
| 3 methods shown | |
| **/ | |
| object WaysToTraverseGrid extends App{ |
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
| // NumberToWords converts an Int into English words | |
| object NumberToWords extends App{ | |
| val num2Word=Map((0, "Zero"), (1, "One"), | |
| (2, "Two"), (3, "Three"), | |
| (4, "Four"), (5, "Five"), | |
| (6, "Six"), (7, "Seven"), | |
| (8, "Eight"), (9, "Nine"), | |
| (10, "Ten"), (11, "Eleven"), |
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
| // Top and bottom N max/min items from an Array, with no need to sort | |
| object TopBottomN extends App { | |
| def bottomN(a: Array[Int], n: Int): Array[Int] = { | |
| val smallestVals = Array.fill(n)(Int.MaxValue) | |
| a.foreach { number => | |
| var inserted=false | |
| (n - 1 to 0 by -1).foreach { pos => |
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
| def css = Action { | |
| val css: TxtFormat.Appendable =views.txt.css() | |
| Ok(css).as("text/css") | |
| } | |
| inside HTML | |
| <link rel="stylesheet" href="@routes.HomeController.css()"/> | |
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
| implicit object NullBooleanReads extends Reads[Boolean] { | |
| def reads(json: JsValue): JsSuccess[Boolean] = json match { | |
| case JsBoolean(b) => JsSuccess(b) | |
| case _ => JsSuccess(false) | |
| } | |
| } |
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
| import cats._ | |
| import cats.implicits._ | |
| // Cats SemigroupK vs Semigroup | |
| object CatsSemigroupK extends App { | |
| // semigroupK doesn't care about contents |
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
| import cats._ | |
| import cats.implicits._ | |
| // Example usage of Cats Flatmap | |
| object CatsFlatmap extends App { | |
| val listFlatMap=FlatMap[List] | |
| val li=List(1,2,3) |
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
| import cats._ | |
| import cats.implicits._ | |
| // Cats Apply, does cartesian maps, extends Functor and Semigroupal (which does cartesian joins, unlike pairwise Align) | |
| object CatsAp extends App { | |
| val apOption=Apply[Option] | |
| val option1: Option[(Int, Int)] =apOption.product(Option(1), Option(2)) |
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
| // take out the + and see the last few lines fail as doit then only takes Animal, and not its subtypes | |
| // we output A, so + is fine, we are covariant | |
| sealed trait ThisThatValue[+A] | |
| object ThisThatValue { | |
| final case class This[+A](value: A) extends ThisThatValue[A] | |
| final case class That[+A](value: A) extends ThisThatValue[A] | |
| } |
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
| import cats._ | |
| import cats.data._ | |
| import cats.syntax._ | |
| import cats.implicits._ | |
| object CatsBi extends App { | |
| val fail = Left("failed") | |
| val ok = Right(123) |