View Scala3TypeClassWithUsingGiven
This file contains 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
object Scala3TypeClassWithUsingGiven { | |
trait MyMonoid[A] { | |
def combine(x: A, y: A): A | |
def empty: A | |
} | |
def combineList[A](li: List[A])(using monoid: MyMonoid[A]): A = li.foldLeft(monoid.empty)(monoid.combine) | |
def main(args: Array[String]): Unit = { |
View Fs2Queues.scala
This file contains 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.effect._ | |
import fs2._ | |
import scala.concurrent.duration._ | |
import cats.effect.std._ | |
// FS2 cats.effect.Queue example using flatMap or for comprehension | |
// Both streams emit nothing, but are effectful, communicating via the queue and updating a sum via the ref | |
object Fs2Queues extends IOApp.Simple { |
View MyErrsIO.scala
This file contains 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 MyErrsIO.Controller.{Request, postTransfer} | |
import MyErrsIO.Models.Account | |
import cats.data.Validated.{Invalid, Valid} | |
import cats.data.{EitherT, OptionT, Validated, ValidatedNec} | |
import cats.effect._ | |
import cats.implicits._ | |
import scala.util.control.NonFatal | |
// A simple example of cat-effect and error handling, with MonadTransformers |
View TreeFunctorExample.scala
This file contains 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._ | |
object TreeFunctorExample { | |
def main(args: Array[String]): Unit = { | |
sealed trait Tree[+T] | |
case class Leaf[+T](value: T) extends Tree[T] | |
case class Branch[+T](value: T, left: Tree[T], right: Tree[T]) extends Tree[T] |
View Intersect.scala
This file contains 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
object Intersect extends App{ | |
import scala.annotation.tailrec | |
// finds the elements shared by 2 lists, FP style, recursively | |
// O(n log n) | |
@tailrec | |
def scan(x: List[Int], y: List[Int], out: List[Int] = List.empty[Int]): List[Int] = | |
(x, y) match { |
View WaysToTraverseGrid.scala
This file contains 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{ |
View NumberToWords.scala
This file contains 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"), |
View TopBottomN.scala
This file contains 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 => |
View scala_play_css_tip.txt
This file contains 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()"/> | |
View NullBooleanReads.scala
This file contains 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) | |
} | |
} |
NewerOlder