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 OneWayImplicits {
//outside implicits visible, but T is not visible inside
case class Outwards[T](value: T) extends AnyVal
//outside implicits invisible, T is visible inside
case class Inwards[T](value: T) extends AnyVal
implicit def outwardsFromT[T](implicit t: T): Outwards[T] = Outwards(t)
implicit def tFromInwards[T](implicit inw: Inwards[T]): T = inw.value
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 / 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 / post.scala
Created May 16, 2018 13:10
the description for this gist
def time() = System.currentTimeMillis()
val t1 = time()
val x = calculateSomething(args)
val t2 = time()
println(t2 - t1) //print the time it took for `x` to be computed
@kubukoz
kubukoz / post.scala
Created May 16, 2018 13:10
the description for this gist
def time(): Future[Long] = Future { System.currentTimeMillis() }
for {
t1 <- time()
x = calculateSomething(args)
t2 <- time()
} println(t2 - t1)