Skip to content

Instantly share code, notes, and snippets.

kenji yoshida xuwei-k

Block or report user

Report or block xuwei-k

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:01766ecb0a9d37e8149e1db7ecd6126b
Welcome to Scala 2.12.10 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_201).
Type in expressions for evaluation. Or try :help.
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Foo
trait NotFoo[-A]
object NotFoo {
@xuwei-k
xuwei-k / dotty_list.scala
Created Jan 12, 2020 — forked from johnynek/dotty_list.scala
Implementation of linked list using dotty features (opaque type, union types, extension methods, type lambda).
View dotty_list.scala
// unfortunately
// opaque type Fix[F[_]] = F[Fix[F]]
// won't work (no recursion in opaque type), but this implementation is safe, but scary due to asInstanceOf
object FixImpl {
type Fix[F[_]]
inline def fix[F[_]](f: F[Fix[F]]): Fix[F] = f.asInstanceOf[Fix[F]]
inline def unfix[F[_]](f: Fix[F]): F[Fix[F]] = f.asInstanceOf[F[Fix[F]]]
}
View build.sbt
lazy val getAllDependencyProjects = taskKey[Seq[ProjectRef]](
"依存してるprojectを再帰的に取得"
)
lazy val commonSettings = Def.settings(
getAllDependencyProjects := {
val map = buildDependencies.value.classpath
def loop(root: ProjectRef): Seq[ProjectRef] = {
root +: map(root).map(_.project).flatMap(loop)
}
View scalatest-fix.sh
# https://github.com/scalatest/autofix/tree/bb6af2729d61b3a87b47d43dd32a952f90554253/3.1.x
echo 'addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.8")' > project/scalafix.sbt
echo 'scalafixDependencies in ThisBuild += "org.scalatest" %% "autofix" % "3.1.0.0"
libraryDependencies in ThisBuild += compilerPlugin(scalafixSemanticdb)
scalacOptions in ThisBuild += "-Yrangepos"' > scalafix.sbt
View GCされるための辛い努力.md

単純な(リークするかもしれない)実装

def foo(lazyList: LazyList[A]) = {
  
  // Scala 2.13のlazyListだとnewLLとかいう名前のやつ
  Function0を引数に取るやつ {
    引数のlazyList使った実装
  }
}
@xuwei-k
xuwei-k / build.sbt
Created Nov 26, 2019
sbt task sequential
View build.sbt
val someTask = taskKey[Unit]("")
val commonSettings = Def.settings(
someTask := {
println(name.value + " 開始")
Thread.sleep(500)
println(name.value + " 終了")
}
)
@xuwei-k
xuwei-k / RuntimeUtils.scala
Created Oct 14, 2019 — forked from jvican/RuntimeUtils.scala
Some Scala code that uses Java APIs present in tools.jar (only JDKs) to programmatically produce a jstack-like thread dump. Useful to debug application and test deadlocks.
View RuntimeUtils.scala
object RuntimeUtils {
def requestThreadDump: String = {
// Get the PID of the current JVM process
val selfName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName()
val selfPid = selfName.substring(0, selfName.indexOf('@'))
// Attach to the VM
import com.sun.tools.attach.VirtualMachine
import sun.tools.attach.HotSpotVirtualMachine;
val vm = VirtualMachine.attach(selfPid);
@xuwei-k
xuwei-k / sonatypeBundleDirectory.md
Last active Oct 14, 2019
sbt-sonatypeのsonatypeBundleDirectoryを微妙に修正するべき?という話
View sonatypeBundleDirectory.md

前提や問題点の説明

  • sbt-sonatypeのversion 3.8時点の話
  • 最近のsbt-sonatypeは、一旦全部ローカルの特定のディレクトリにjarやpomをpublishして、それをまとめてuploadするという、bundle releaseという方式が導入された
  • sonatypeBundleDirectoryはその際のディレクトリのためのSetting Key
  • "まとめてupload" なので、もちろん基本的には、一つのディレクトリに全部一旦publishして、1回だけuploadするのが通常の使い方として想定されているはず
  • (なぜか同名の未使用のTaskKeyが定義されているが?)sonatypeBundleReleaseはcommandとして定義されている https://github.com/xerial/sbt-sonatype/blob/3.8/src/main/scala/xerial/sbt/Sonatype.scala#L144-L145
  • SettingKeyやTaskKeyは、sub project毎に違う値や動作を持てる。一方Commandは、(単純に言えば)指定されたものを順次sbtのconsoleから実行するイメージに近い
  • versionもSettingKey
  • つまり、sonatypeBundleDirectoryの値は、versionを使って定義されているが、sub project毎にversionが違えば、sonatypeBundleDirectoryの値も異なることになるはず
View Main.scala
package example
case class IO[A](private val run: () => A) {
def unsafeRun(): A = run()
def map[B](f: A => B): IO[B] = IO(() => f(run()))
def flatMap[B](f: A => IO[B]): IO[B] = IO(() => f(run()).run())
}
case class Ref[A](private var value: A) {
def get: IO[A] = IO(() => value)
View CatsRefUnsafeExample.scala
package example
import cats.effect.concurrent.Ref
import cats.effect._
import cats.syntax.traverse._
import cats.instances.list._
// https://github.com/typelevel/cats-effect/blob/v2.0.0/core/shared/src/main/scala/cats/effect/concurrent/Ref.scala#L208
object CatsRefUnsafeExample extends IOApp {
override def run(args: List[String]) = {
You can’t perform that action at this time.