Skip to content

Instantly share code, notes, and snippets.

View i10416's full-sized avatar
πŸ™ƒ

110416 i10416

πŸ™ƒ
View GitHub Profile
@i10416
i10416 / bloomfilter.sc
Created December 23, 2021 06:04
bloom filter in scala
import scala.collection.immutable
import java.math.BigInteger
import java.security.MessageDigest
class BloomFilter[T](length: Int, numHashes: Int, alorithm: String = "SHA1") {
val underlying = new Array[Int](length)
val digest = MessageDigest.getInstance(alorithm)
def contains(element: T): Boolean =
!getIndices(element).exists(underlying(_) == 0)
@i10416
i10416 / munkres_draft.worksheet.sc
Created December 23, 2021 06:03
scala munkres algorithm impl draft
import scala.collection.immutable.ArraySeq
// NxN にpadding γ™γ‚Œγ° NxMγ«γ‚‚ε―ΎεΏœγ§γγγ†. padは0
val argument: Array[Array[Double]] = Array(
Array(5, 4, 7, 6),
Array(6, 7, 3, 2),
Array(8, 11, 2, 5),
Array(9, 8, 6, 7)
)
val getRowMins = (mat: Array[Array[Double]]) => mat.map(a => a.min)
@i10416
i10416 / shapeless.worksheet.sc
Created December 23, 2021 06:02
generic vector representation using shapeless
import $ivy.`com.chuusai::shapeless:2.3.7`
import shapeless._
trait VecLike[T]{
def dim:Int
def add(a:T,b:T):T
def sub(a:T,b:T):T
}
implicit def doubleVec2D:VecLike[(Double,Double)] =
new VecLike[(Double,Double)] {
HMODULE hUser = GetModuleHandleA("user32.dll");
if (hUser)
{
pfnSetWindowCompositionAttribute setWindowCompositionAttribute = (pfnSetWindowCompositionAttribute)GetProcAddress(hUser, "SetWindowCompositionAttribute");
if (setWindowCompositionAttribute)
{
ACCENT_POLICY accent = { ACCENT_ENABLE_BLURBEHIND, 0, 0, 0 };
WINDOWCOMPOSITIONATTRIBDATA data;
data.Attrib = WCA_ACCENT_POLICY;
data.pvData = &accent;
@i10416
i10416 / jwm_bug_on_windows.java
Created August 24, 2021 11:36
window crashes when no animation is running in the current window.
package org.jetbrains.jwm.examples;
import org.jetbrains.jwm.*;
import org.jetbrains.skija.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.stream.*;
import java.io.File;
@i10416
i10416 / tuple_flatten.scala
Last active July 6, 2021 12:28
extension to flatten nested tuple in scala 3
extension[T<:Tuple] (i:T) {
def flatten = flattenTuple(i)
private def flattenTuple[A <: Tuple](tuple: A): FlattenTypeOf[A] = {
val flatten = tuple match {
case h *: remains =>
h match {
case subtpl: Tuple => flattenTuple(subtpl) ++ flattenTuple(remains)
case _ => h *: flattenTuple(remains)
}
@i10416
i10416 / leetcode_easy_solutions.sc
Created July 5, 2021 13:14
leetcode easy solutions
import java.util.Stack
import java.util.Deque
import scala.collection.mutable.ArrayDeque
// scala 2.13.6
// ammonite 2.4.0
def hammingDistance(one: Int, another: Int): Int = {
// note: xor で η•°γͺγ‚‹θ€…εŒε£«γ γ‘γƒ•γƒ©γ‚°γŒη«‹γ€
(one ^ another).toBinaryString.count { s => s == '1' }
}
@i10416
i10416 / heroku_cli_public_key_not_available_workaround.txt
Created July 5, 2021 12:01
how to solve the gpg error: public key unavailable when running apt update
curl https://cli-assets.heroku.com/apt/release.key | sudo apt-key add -
sudo apt update
@i10416
i10416 / cat_snippets.sc
Created July 5, 2021 08:58
parallel IO cats snippets in scala script
// scala 2.13.6
// ammonite 2.4.0
import $ivy.`org.typelevel::cats-effect:3.1.1`
import $ivy.`org.typelevel::cats-core:2.2.0`
import cats.effect.IO
import cats.syntax.apply._
import cats.syntax._
import scala.concurrent.duration._
import cats.effect.unsafe.implicits.global
@i10416
i10416 / bubble_sort.sc
Created July 5, 2021 08:03
recursive bubble sort implementation in scala script
// scala 2.13.6
// ammonite 2.4.0
def bubbleSort[T: Ordering](orderables: Seq[T]): Seq[T] = {
swapRec(orderables).splitAt(orderables.length - 1) match {
case (Nil, Nil) => Nil
case (Nil, head) => head
case (head, last) => bubbleSort(head) ++ last
}
}