Skip to content

Instantly share code, notes, and snippets.

View petitviolet's full-sized avatar
🕶️
😃

petitviolet petitviolet

🕶️
😃
View GitHub Profile
@petitviolet
petitviolet / StringFormatter.scala
Created February 7, 2018 09:28
String converter for camelCase <-> snake_case
object StringFormatter {
private val spacesPattern = "[-\\s]".r
private val firstPattern = "([A-Z]+)([A-Z][a-z])".r
private val secondPattern = "([a-z\\d])([A-Z])".r
private val replacementPattern = "$1_$2"
implicit class stringFormatter(val word: String) extends AnyVal {
def snakenize: String = {
spacesPattern
.replaceAllIn(secondPattern.replaceAllIn(
@petitviolet
petitviolet / defer.scala
Created February 7, 2018 03:23
Scala `defer` like Golang
class Deferred() {
private var fs: Seq[() => Any] = Nil
def apply(f: => Any): Unit = {
fs = { () =>
f
} +: fs
}
def runDeferred(): Unit = fs.reverse foreach { _.apply }
@petitviolet
petitviolet / TaskPrac.scala
Last active November 10, 2017 08:16
Run scalaz Task in sequencial and parallel
import scalaz._
import scalaz.concurrent.Task
object TaskPrac extends App {
private def task(i: Int) = Task {
println(s"sleeping: $i")
Thread.sleep(i * 100)
println(s"wakeup: $i")
i
}
@petitviolet
petitviolet / DateTimeConverters.scala
Created August 13, 2017 06:46
JodaTime <-> LocalDateTime snippet
object DateTimeConverters {
implicit class DateTimeConverter(val ldt: LocalDateTime) extends AnyVal {
def asJoda: DateTime = {
val instant = ldt.atZone(ZoneId.systemDefault()).toInstant
new DateTime(instant.toEpochMilli)
}
}
implicit class JodaConverter(val jdt: DateTime) extends AnyVal {
def asLocalDateTime: LocalDateTime = {
LocalDateTime.of(
@petitviolet
petitviolet / Pipe.md
Created May 9, 2017 06:58
pipeline operation in Scala
class Conf(private var host: String, private var port: Int) {
 def setHost(host: String): Unit = this.host = host
 def setPort(port: Int): Unit = this.port = port
 override def toString: String = s"Conf($host, $port)"
}
> "hoge" |> { _.size } |> { i => 1 to i } |> { _.mkString(",") }
@petitviolet
petitviolet / lightbend_emoji.scala
Last active April 12, 2017 01:34
using lightbend-emoji library in Ammonite-REPL.
import Resolvers._
val res = Resolver.Http(
"Bintray typesafe ivy-releases",
"http://repo.typesafe.com/typesafe/releases/",
IvyPattern,
false)
interp.resolvers() = interp.resolvers() :+ res
@petitviolet
petitviolet / FutureTrap.md
Last active July 30, 2018 11:27
[Scala]Pay attention to the timing to call `Future.apply`.

what is this?

scala.concurrent.Future let asynchronous programming easily. We can process a code block asynchronously by providing it to Future.apply. The timing to start processing is just Future.apply invoked. (also, needs a idling thread.) Therefore, pay attention to the timing to call it.

not in parallel

@petitviolet
petitviolet / AbstractOverrideSample.scala
Created January 24, 2017 03:56
how works `abstract override def`
trait Hoge {
def receive: String
}
trait HogeChild extends Hoge {
abstract override def receive: String = {
println(s"child")
super.receive
}
}
@petitviolet
petitviolet / Foo.jad
Last active January 20, 2017 04:51
abstract type member and explicit type annotation
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: target.scala
public interface Foo
{
public abstract Utility utility();
@petitviolet
petitviolet / thread_local_random.md
Last active February 11, 2021 15:51
[Scala]ThreadLocalRandom is fast.

ThreadLocalRandom is faster than Random

val n = 100
Random.nextInt(n)
ThreadLocalRandom.current().nextInt(n)

why ThreadLocalRandom is faster