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
| object Utils { | |
| val disableCache = respondWithHeaders( | |
| `Cache-Control`(`no-cache`, `max-age`(0), `must-revalidate`, `no-store`), | |
| `Expires`(DateTime.MinValue) | |
| ) | |
| } | |
| object BooksRoutes { | |
| val routes = path("books" / Segment) { id => | |
| complete(id) |
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
| case class Grand(parent: Parent) | |
| sealed trait Parent { def x: Int } | |
| case class Child1(x: Int) extends Parent | |
| case class Child2(x: Int) extends Parent | |
| val grand = Grand(Child1(3)) | |
| it should "modify a field in a sealed trait" in { | |
| modify(grand)(_.parent.x).using(_ + 1) should be (Grand(Child1(4))) | |
| } |
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
| val l2d = Map('i' -> 1, 'v' -> 5, 'x' -> 10, 'l' -> 50, 'c' -> 100) | |
| def compute(ds: Seq[Int]) = ds.foldRight((0, 0)) { | |
| case (d, (acc, last)) => if (d < last) (acc - d, last) else (acc + d, d) | |
| } | |
| def roman(numeral: String) = compute(numeral map l2d)._1 |
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
| case class Street(name: String, house: Int) | |
| case class Address(street: Street, city: String) | |
| case class Person(address: Address, age: Int) | |
| val person1 = Person(Address(Street("Functional Rd.", 1), "London"), 35) | |
| val person2 = person.modify(_.address.street.house).using(_ + 3) | |
| /* | |
| ┌───────────┐ ┌───────────┐ | |
| │Person (35)│ │Person (35)│ |
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
| load.ivy("com.nimbusds" % "nimbus-jose-jwt" % "4.21") | |
| load.ivy("org.bouncycastle" % "bcprov-jdk15on" % "1.51") | |
| @ | |
| import com.nimbusds.jose.jwk._ | |
| import org.bouncycastle.util.io.pem._ | |
| import java.io._ | |
| def main(input: String) = { |
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 reftree.{Diagram, Utils} | |
| import scala.collection.immutable.TreeSet | |
| val adding = Utils.iterate(TreeSet(1), 15)(s ⇒ s + (s.size + 1)) | |
| val removing = Utils.iterate(adding.last, 15)(s ⇒ s - s.size) | |
| Diagram().renderAnimation("treeset", tweakOptions = _.copy( | |
| delay = 200, onionSkin = 0, diffAccent = true, | |
| verticalSpacing = 1.1, highlightColor = "coral1", density = 75 | |
| ))(adding ++ removing) |
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 de.sciss.fingertree.{FingerTree, Measure} | |
| import reftree.{Diagram, Utils} | |
| import reftree.contrib.FingerTreeInstances._ | |
| implicit val measure = Measure.Indexed | |
| val trees = Utils.iterate(FingerTree(1), 22)(t ⇒ t :+ (t.measure + 1)) | |
| Diagram().renderAnimation("finger", tweakOptions = _.copy( | |
| delay = 200, loop = false, onionSkin = 0, diffAccent = 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
| // git clone https://github.com/stanch/reftree && cd reftree | |
| // sbt demo | |
| def add(n: Int)(q: Queue[Int]) = Utils.iterate(q, n + 1)(q => q :+ (q.max + 1)).tail | |
| def remove(n: Int)(q: Queue[Int]) = Utils.iterate(q, n + 1)(q => q.tail).tail | |
| def addRemove(n: Int)(q: Queue[Int]) = Utils.flatIterate(q)(add(n), rm(n)) | |
| val queues = Utils.flatIterate(Queue(1, 2, 3), 3)(addRemove(2)) | |
| diagram.renderAnimation( |
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
| case class MapImplicit[A](f: A => A)(implicit current: A) { | |
| def in[B](code: A => B) = code(f(current)) | |
| } | |
| // start with an implicit value | |
| implicit val foo: Int = 3 | |
| // prints “3” | |
| println(implicitly[Int]) |
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
| # Check whether the input array intersects the specified array of items | |
| def intersects(items): | |
| reduce .[] as $item (false; . or (items | index($item))); | |
| # Find all references to other resources | |
| def references(ref): | |
| [.. | objects | ref | values]; | |
| # Remove entries whose keys do not match the predicate | |
| def filter_keys(pred): |