Skip to content

Instantly share code, notes, and snippets.

View kubukoz's full-sized avatar
😱
I might take a week to respond. Or a month.

Jakub Kozłowski kubukoz

😱
I might take a week to respond. Or a month.
View GitHub Profile
object ChainedConversions {
implicit def chain[A, B, C](implicit ab: A => B, bc: B => C): A => C = ab andThen bc
implicit val intToString: Int => String = _.toString
class X
implicit val stringToX: String => X = _ => new X
val a: X = 5 // idea marks it as a proper conversion
//doesn't compile though
}
import shapeless._
def abc0[M[_]] = ???
def abc1[M[+_]] = ???
def abc2[M[-_]] = ???
//this one should accept an arbitrary invariant M
def abc3[M[_] <: Set[_]] = ???
type Predicate[-a] = a => Boolean
object Example {
case class Sample(age: Int)
def wrapsBlock(block1: Any) = ???
def ifHasImplicit(block2: Any)(implicit request: String) = ???
def sample(sampleObj: Sample) = wrapsBlock {
ifHasImplicit {
import sampleObj.age
public class Sample {
public static void main(String[] args) {
new Dog().speak(10); //I am 10
new Dog().speak("kek"); //I am kek wow
}
}
class Animal {
void speak(Object something){
@kubukoz
kubukoz / AsyncExecutorContextAware.scala
Last active January 15, 2019 14:06
Getting Slick 3.x + Kamon 1.x to stop forgetting the context
//based on https://github.com/kamon-io/Kamon/issues/311
class AsyncExecutorContextAware(underlying: AsyncExecutor) extends AsyncExecutor {
override def executionContext: ExecutionContext = new ExecutionContextAware(underlying.executionContext)
override def close(): Unit = underlying.close()
}
class ExecutionContextAware(underlying: ExecutionContext) extends ExecutionContext {
override def execute(runnable: Runnable): Unit = underlying.execute(new RunnableContextAware(runnable))
override def reportFailure(cause: Throwable): Unit = underlying.reportFailure(cause)
@kubukoz
kubukoz / Transpose.scala
Last active March 9, 2018 00:10
Transpose a List of HLists.
//see original SO answer: https://stackoverflow.com/a/49184270/1927425
import shapeless.ops.hlist._
import shapeless.{::, _}
trait EmptyOf[T <: HList, AsList <: HList] {
def empty(): AsList
}
object EmptyOf {
sealed trait GameCreationError extends Product with Serializable
object GameCreationError {
case class NameTooShort(min: Int) extends GameCreationError
case class NameTooLong(max: Int) extends GameCreationError
case class PlatformNotFound(platformSlug: String) extends GameCreationError
case class PlayersFromTooLow(min: Int) extends GameCreationError
case object PlayersToSmallerThanFrom extends GameCreationError
case object NameNotUnique extends GameCreationError
case object NoPlatformsSupplied extends GameCreationError
import io.circe.generic.JsonCodec
@JsonCodec(decodeOnly = true)
case class GameToCreate(name: String, platforms: List[String], players: Players)
@JsonCodec(decodeOnly = true)
case class Players(from: Int, to: Option[Int])
object map {
/**
* Allows a Map to become total forever.
* It's isomorphic to `Map.withDefaultValue`, but has a more specific return type, in particular:
* <ul>
* <li>it leaves out the information that the returned value is a Map</li>
* <li>it manifests that the returned value is a total function.</li>
* </ul>
*
* This is useful for a single reason - Maps by default aren't total functions, so `map.apply` can potentially
@kubukoz
kubukoz / HKT_Helpers.scala
Last active July 28, 2018 19:52
Mapping non-unary higher kinded types to unary higher kinded types to deal with https://youtrack.jetbrains.com/issue/SCL-12892
//works with Kleisli/ReaderT
implicit class NestedToUnary[F[_[_], _, _], G[_], I, O](private val value: F[G, I, O]) extends AnyVal {
type H[A] = F[G, I, A]
def unaryKind: H[O] = value
}
//works with fs2.Stream, Free/AuthedService from http4s
implicit class NestedToUnary2[F[_[_], _], G[_], I](private val value: F[G, I]) extends AnyVal {
type H[A] = F[G, I]
def unaryKind: H[O] = value