Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
xuwei-k / 3
Created February 28, 2024 07:10
$ scala -Xsource:3
Welcome to Scala 2.13.13 -Xsource:3.0.0 (OpenJDK 64-Bit Server VM, Java 1.8.0_392).
Type in expressions for evaluation. Or try :help.
scala> def f = {
| val x: Boolean = ???
|
| x
| && x
| }
package example
import shapeless.nat._
import shapeless.syntax.std.tuple._
object Main{
def main(args: Array[String]): Unit = {
val t = (2, false, "a")
val x = t.updateAtWith(2)(a => a :: Nil)._2
import scala.compiletime.ops.int.*
object MapTupleNExample {
type TupleMapN[T <: NonEmptyTuple, N <: Int, A] =
N < Tuple.Size[T] match {
case true =>
Tuple.Concat[
Tuple.Take[T, N],
A *: Tuple.Drop[T, N + 1]
]
[info] running (fork) org.openjdk.jmh.Main -i 10 -wi 10 -f1
[info] # JMH version: 1.37
[info] # VM version: JDK 23-ea, OpenJDK 64-Bit Server VM, 23-ea+7-481
[info] # VM invoker: /Library/Java/JavaVirtualMachines/jdk-23.jdk/Contents/Home/bin/java
[info] # VM options: <none>
[info] # Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable)
[info] # Warmup: 10 iterations, 10 s each
[info] # Measurement: 10 iterations, 10 s each
[info] # Timeout: 10 min per iteration
[info] # Threads: 1 thread, will synchronize iterations
@xuwei-k
xuwei-k / Witness.scala
Last active January 27, 2024 10:40
shapeless.Witness match type
import scala.compiletime.ops.string.Length
import scala.compiletime.ops.string.CharAt
import scala.compiletime.ops.string.Substring
import scala.compiletime.ops.int
import scala.compiletime.ops.long
import scala.language.dynamics
object Witness extends Dynamic {
type StringTail[A <: String] = Substring[A, 1, Length[A]]
import play.api.BuiltInComponents
import play.api.Mode
import play.api.mvc._
import play.api.routing._
import play.api.routing.sird._
import play.core.server.PekkoHttpServer
import play.core.server.ServerConfig
object Main {
// 1つのScalaファイルで済むので、
@xuwei-k
xuwei-k / Scala-2-12
Last active October 5, 2023 05:29
scala xml pattern match
Welcome to Scala 2.12.18 (OpenJDK 64-Bit Server VM, Java 11.0.20).
Type in expressions for evaluation. Or try :help.
scala> val <a>{ b }</a> = <a></a>
scala.MatchError: <a></a> (of class scala.xml.Elem)
... 55 elided
scala> val <a>{ _ }</a> = <a>c</a>
scala> val <a>{ _* }</a> = <a>c</a>
@xuwei-k
xuwei-k / Bench.scala
Last active September 29, 2023 06:56
package p1
import org.openjdk.jmh.annotations.Benchmark
object Bench {
val values: List[Either[Int, Int]] = (1 to 100_000).map{
case n if n % 2 == 0 =>
Right(n)
case n =>
Left(n)
@xuwei-k
xuwei-k / 11
Last active September 22, 2023 07:48
public java.lang.String x(java.lang.String);
descriptor: (Ljava/lang/String;)Ljava/lang/String;
flags: (0x0001) ACC_PUBLIC
Code:
stack=1, locals=2, args_size=2
0: aload_1
1: invokedynamic #27, 0 // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;)Ljava/lang/String;
6: areturn
@xuwei-k
xuwei-k / FunctorPolyFunction.scala
Created July 29, 2023 01:39
impl Functor use PolyFunction
package example
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
object Functor {
def fromPolyFunction[F[_]](x: [A, B] => (fa: F[A]) => (f: A => B) => F[B]): Functor[F] =
new Functor[F] {
def map[A, B](fa: F[A])(f: A => B): F[B] = x(fa)(f)