Skip to content

Instantly share code, notes, and snippets.

View joel113's full-sized avatar

Johannes Ehm joel113

View GitHub Profile
// Type parameter placeholder: S[_]
def str[T, S[_]](ss: S[T]): String = ss.toString
val s1 = str(Seq(1, 2, 3)) // "List(1, 2, 3)"
// Function parameter placeholder: _.toUpperCase
val loud = Seq("hello world!").map(_.toUpperCase) // List(HELLO WORLD!)
// Match placeholder: _ in the first two case clauses
Seq(Seq('a', 'b', 'c'), Seq(1, 2, 3), Seq(1.1, 2.2)).map {
seq => seq match {
@joel113
joel113 / CirceSupport.scala
Created January 29, 2021 16:32 — forked from mattroberts297/CirceSupport.scala
Akka HTTP Circe Custom Marshaller and Unmarshaller
import io.circe._
import io.circe.parser._
import io.circe.syntax._
import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
import akka.http.scaladsl.model.{ContentTypeRange, HttpEntity}
import akka.http.scaladsl.model.MediaTypes.`application/json`
import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller}
import scala.concurrent.Future
import io.circe._
import io.circe.parser._
import io.circe.syntax._
import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
import akka.http.scaladsl.model.{ContentTypeRange, HttpEntity}
import akka.http.scaladsl.model.MediaTypes.`application/json`
import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller}
import scala.concurrent.Future
abstract class Animal
case class Cat(name: String) extends Animal
case class Dog(name: String, owner: String) extends Animal
def checkAnimal(animal: Animal): String = {
animal match {
case Cat(name) => "The name of the cat is $name."
case _ => "Its just a dog."
def sequencesPatternMatching(sequence: Any): String = {
sequence match {
case List(singleElement) => s"I'm a list with one element: $singleElement"
case List(_, _*) => s"I'm a list with one or multiple elements: sequence"
case Vector(1, 2, _*) => s"I'm a vector: $sequence"
case _ => s"I'm an unrecognized sequence. My value: $sequence"
}
}
sequencesPatternMatching(Seq(1,2,3))
@joel113
joel113 / gist:f7464467647496b9ba25a502d57868b6
Created November 19, 2020 21:46
DiverToWithAsync.scala
package com.joel.akka.streams
import akka.actor.ActorSystem
import akka.stream.scaladsl._
import akka.Done
import scala.concurrent._
object Main {
@joel113
joel113 / DivertTo.scala
Created November 19, 2020 20:41
Investigation into the Streaming Semantics of DivertTo
package com.joel.akka.streams
import akka.actor.ActorSystem
import akka.stream.scaladsl._
import akka.Done
import scala.concurrent._
object Main {
@joel113
joel113 / InstanceOf.scala
Created October 4, 2020 07:43
Investigation of the compile and runtime behaviour of the cast operation asInstanceOf of Scala.
import scala.collection.immutable.HashMap
object InstanceOf {
/**
* Investigation of the compile and runtime behaviour of the cast operation asInstanceOf of Scala.
*
* @param args
*/
def main(args: Array[String]): Unit = {
import io.circe.Json.Null
import io.circe.{Encoder, Json, ObjectEncoder}
import io.circe.generic.auto._
import io.circe.generic.semiauto.deriveEncoder
import io.circe.syntax._
object example {
def main(args: Array[String]) {
import java.util.Optional
import scala.jdk.javaapi.OptionConverters
object example {
case class ExampleClassD(e: Int)
case class ExampleClassC(d: Optional[Int])