Skip to content

Instantly share code, notes, and snippets.

View patrick-premont's full-sized avatar

Patrick Prémont patrick-premont

  • Gatineau-Ottawa, Canada
View GitHub Profile
@patrick-premont
patrick-premont / panda.scala
Created April 27, 2019 01:53
Coded a panda with Emma (9 y.o.)
import scala.io.AnsiColor._
def espace(couleur: String) =
couleur + " " + RESET
def image(w:Int, h: Int, f: (Int, Int) => String ) =
println((for (y <- -h to h) yield (for (x <- -w to w) yield espace(f(x,y))).mkString).mkString("\n"))
// 8 ellipses
image(70, 25, (x,y) =>
import math._
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import ScalaFiddle.{Color, Epsilon}
import fiddle.Fiddle.{canvas, draw => renderer}
import scala.scalajs.js
import org.scalajs.dom
import scala.language.postfixOps
/**
* A simple ray tracer, taken from the PyPy benchmarks
# Illustrates the compile-time dispatch that has to occur with macro overrides.
# You can install Ammonite-REPL (http://www.lihaoyi.com/Ammonite/#Ammonite-REPL), or use scala instead of amm
$ amm
Loading...
Welcome to the Ammonite Repl 0.9.3
(Scala 2.12.2 Java 1.8.0_121)
@ import scala.reflect.macros.blackbox
import scala.reflect.macros.blackbox
@ import scala.language.experimental.macros
import scala.language.experimental.macros
# Prerequisite: Install Ammonite-REPL (http://www.lihaoyi.com/Ammonite/#Ammonite-REPL)
$ amm
Loading...
Welcome to the Ammonite Repl 0.8.2
(Scala 2.12.1 Java 1.8.0_66)
@ import $ivy.`org.typelevel::cats:0.9.0`
@ import scala.concurrent.Future
@ import scala.concurrent.ExecutionContext.Implicits.global
@ import cats.implicits._
@ import cats.data.OptionT
# Prerequisite: Install Ammonite-REPL (http://www.lihaoyi.com/Ammonite/#Ammonite-REPL)
$ amm
Loading...
Welcome to the Ammonite Repl 0.8.2
(Scala 2.12.1 Java 1.8.0_66)
@ import $ivy.`org.typelevel::cats:0.9.0`
@ import cats.implicits._
@ import cats.syntax.either._
@ List(1.asRight, 2.asRight).sequenceU
res3: Either[Nothing, List[Int]] = Right(List(1, 2))
# Prerequisite: Install Ammonite-REPL (http://www.lihaoyi.com/Ammonite/#Ammonite-REPL)
$ amm
Loading...
Welcome to the Ammonite Repl 0.8.2
(Scala 2.12.1 Java 1.8.0_66)
@ import $ivy.`org.typelevel::cats:0.9.0`
@ import cats.syntax.option._
@ import cats.implicits._
@ List(1.some, 2.some).sequenceU
res3: Option[List[Int]] = Some(List(1, 2))
# Prerequisite: Install Ammonite-REPL (http://www.lihaoyi.com/Ammonite/#Ammonite-REPL)
$ amm
Loading...
Welcome to the Ammonite Repl 0.8.2
(Scala 2.12.1 Java 1.8.0_66)
@ import $ivy.`org.typelevel::cats:0.9.0`
@ import cats.implicits._
@ import cats.data.Validated._
@ List(2, 4).map(a => if (a % 2 == 0) valid(a) else invalidNel(a)).sequenceU
res5: cats.data.Validated[cats.data.NonEmptyList[Int], List[Int]] = Valid(List(2, 4))

Keybase proof

I hereby claim:

  • I am patrick-premont on github.
  • I am ppremont (https://keybase.io/ppremont) on keybase.
  • I have a public key whose fingerprint is 56CE CB40 3F90 A457 B270 F89C E79F 9E8E 931B D152

To claim this, I am signing this object:

scala> import pprint.Config.Colors._
import pprint.Config.Colors._
scala> case class Test(name: String, value: Int)
defined class Test
scala> pprint.pprintln(Test("a" * 10, 1))
Test("aaaaaaaaaa", 1)
scala> pprint.pprintln(Test("a" * 10, 1))(implicitly, PPrintConfig.copy(showFieldNames = true))