Skip to content

Instantly share code, notes, and snippets.

@i10416
i10416 / main.scala
Created March 5, 2025 22:58
cross platform naive SMTP client using fs2 and cats effect
//> using dep co.fs2::fs2-core::3.11.0
//> using dep co.fs2::fs2-io::3.11.0
//> using dep com.comcast::ip4s-core::3.6.0
//> using dep org.typelevel::cats-effect::3.5.7
//> using dep org.typelevel::otel4s-core::0.11.2
import cats.MonadThrow
import cats.effect.Concurrent
import cats.effect.IO
import cats.effect.IOApp
// This is a dumb executor for types that implement Future interface
async fn async_value() -> usize {
42
}
async fn example_task() {
let value = async_value().await;
println!("{value}")
}

The Scala 2.12/2.13 Inliner and Optimizer

tl;dr.

  • Don't inline from your dependencies when publishing a library, it breaks binary compatibility. Use -opt-inline-from:my.package.** to only inline from packages within your library.
  • When compiling with the inliner enabled, ensure that the run-time classpath is exactly the same as the compile-time classpath.
  • Don't enable the optimizer for development: it breaks sbt's incremental compilation, and it makes the compiler slower. Only enable it for testing, on CI and to build releases.
  • The @inline annotation only has an effect if the inliner is enabled. It tells the inliner to always try to inline the annotated method or callsite.
  • Without the @inline annotation, the inliner generally inlines higher-order methods and forwarder methods. The main goal is to eliminate megamorphic callsites due to functions passed as argument, and to eliminate value boxing. Other optimizations are delegated to the JVM.
//> using scala "3.3.1"
//> using lib "org.typelevel::cats-effect::3.5.1"
//> using lib "org.http4s::http4s-core::1.0.0-M39"
//> using lib "org.http4s::http4s-ember-server::1.0.0-M39"
import cats.effect.*
import cats.effect.std.Env
import com.comcast.ip4s.*
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.server.staticcontent.*
@i10416
i10416 / utf8_encode.c
Created August 20, 2023 11:27 — forked from MightyPork/utf8_encode.c
C function to encode a Unicode code point as UTF-8 byte array
#include <stdint.h>
/**
* Encode a code point using UTF-8
*
* @author Ondřej Hruška <ondra@ondrovo.com>
* @license MIT
*
* @param out - output buffer (min 5 characters), will be 0-terminated
* @param utf - code point 0-0x10FFFF
@i10416
i10416 / NaiveGapBuffer.scala
Created February 19, 2023 13:55
naive gap buffer implementation in scala
import scala.reflect.ClassTag
object NaiveGapBuffer:
def apply[T: ClassTag](): NaiveGapBuffer[T] =
NaiveGapBuffer(DEFAULT_SIZE)
def apply[T: ClassTag](capacity: Int): NaiveGapBuffer[T] =
val elms = Array.ofDim[T](capacity)
new NaiveGapBuffer(elms)
inline final val DEFAULT_SIZE = 10
// Gap buffer is a data structure famous for its efficient insertion and deletion in contiguous location.
@i10416
i10416 / doobie.scala
Created October 11, 2022 13:50
query mysql with doobie
//> using scala "3.2.0"
//> using lib "org.tpolecat::doobie-core:1.0.0-RC2"
//> using lib "org.typelevel::cats-effect:3.3.14"
//> using lib "org.typelevel::cats-core:2.8.0"
//> using lib "mysql:mysql-connector-java:8.0.30"
import doobie.*
import cats.effect.*
import cats.syntax.all.*
import doobie.implicits.*
import java.time.Instant
@i10416
i10416 / hindleyMilnerTypeInference.fs
Created May 8, 2022 01:18 — forked from pocarist/hindleyMilnerTypeInference.fs
Hindley-Milner型推論アルゴリズムをF#で書いてみた - http://d.hatena.ne.jp/pocarist/20141220/1419087653
(* Groovy:
/**
* Hindley-Milner type inference
* Ported to Groovy from Scala by Example Chapter 16 code.
* (http://www.scala-lang.org/docu/files/ScalaByExample.pdf)
* refered also
* https://github.com/namin/spots/blob/master/hindleyMilner/typeInference.scala
* Japanese page of 'Scala by Example':
* http://www29.atwiki.jp/tmiya/pages/78.html
*/ *)
@i10416
i10416 / ncent.sc
Created December 26, 2021 07:48
algo:naive coin combination
def ncent(n:Int,coins:Set[Int]):Int = {
coins.map {p =>
(0 to n / p).map { q =>
internal(p,q,n,coins.filterNot(_==p))
}.sum
}.sum
// n cent を得るとき
// 25 cent は たかだか n/25 = h
// 10 cent は たかだか n/10 = i
@i10416
i10416 / bracesV2.sc
Created December 26, 2021 06:40
algo:match braces
def matchBraces(n: Int): Set[String] = n match {
case 1 => Set("()")
case 2 => Set("()()", "(())")
case n =>
matchBraces(n - 1).flatMap { s =>
Set(
"(" + s + ")",
"()" + s,
s + "()"
)