Skip to content

Instantly share code, notes, and snippets.

View phiSgr's full-sized avatar

George Leung phiSgr

View GitHub Profile
@phiSgr
phiSgr / vertexFuture.kt
Created December 23, 2017 02:53
Vert.x Future with sync and async callbacks and coroutines
val future1 = Future.future<Unit>()
launch (context = vertx.dispatcher()) {
future1.await()
// Code in coroutine running in vert.x-eventloop-thread-0
println("Code in coroutine running in ${Thread.currentThread().name}")
}
val future2 = Future.future<Unit>()
future2.setHandler {
// Code in handler running in thread-of-another-component
@phiSgr
phiSgr / bbi.js
Created December 20, 2018 05:31
Changing BridgeBase Java handviewer to the js version
// For the documentation on the js handviewer, see http://www.bridgebase.com/tools/hvdoc.html
const convertParam = function(key, value) {
switch (key) {
case "north":
return ["n", value.replace(/^!\*!North\*/, "")]
case "east":
return ["e", value.replace(/^!\*!East\*/, "")]
case "west":
return ["w", value.replace(/^!\*!West\*/, "")]
@phiSgr
phiSgr / pbPrism.scala
Created February 12, 2019 12:06
Prisms for scalapb, as `oneof` is tagged union
import scalapb.GeneratedOneof
import scalapb.lenses.{Lens, Mutation}
trait Prism[OneOf, Case] {
def down(o: OneOf): Option[Case]
def up(c: Case): OneOf
}
implicit class OneOfLens[A, OneOf <: GeneratedOneof](l: Lens[A, OneOf]) {
def mutateCase[B](p: Lens[A, OneOf] => Prism[OneOf, B])(m: Lens[B, B] => Mutation[B]): Mutation[A] = a => {
@phiSgr
phiSgr / mac.sh
Last active July 8, 2024 17:34
Fresh Mac
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
brew install iterm2 firefox intellij-idea-ce telegram-desktop sublime-text tunnelblick google-backup-and-sync
brew install visualvm slack
brew install --cask docker
brew install --cask zulu
brew install --cask unnaturalscrollwheels
brew install --cask notunes
@phiSgr
phiSgr / cme.kt
Last active December 26, 2019 21:07
ConcurrentModificationException with subList
val list = ArrayList<Int>()
val subList = list.subList(0, 0)
list.add(1) // modCount++
subList + 1
/*
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1444)
at java.base/java.util.ArrayList$SubList.size(ArrayList.java:1184)
at kotlin.collections.CollectionsKt___CollectionsKt.plus(_Collections.kt:2036)
*/
@phiSgr
phiSgr / remove-one-off-searches-in-urlbar.md
Last active May 5, 2021 20:37
Remove one off searches in URL bar in Firefox

This no longer works because of the code change. I have given up fighiting it.


The misery of having one-off searches in URL bar comes from a file in a "zip" file omni.ja.

To disable the feature we can edit the file, with the risk of rendering Firefox unusable.


import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.selects.select
data class AllFailed(val failures: List<Throwable>) : Throwable()
suspend fun <T : Any> requestHedging(
providers: List<suspend () -> T>,
duration: Long,
close: suspend (T) -> Unit = {}
@phiSgr
phiSgr / yinyang.kt
Last active February 18, 2023 22:33
// git clone git@github.com:7045bccac80f267a66a93e23626cd427.git yinyang
// cd yinyang
// kotlinc yinyang.kt -include-runtime -d yinyang.jar && java -jar yinyang.jar; rm yinyang.jar
// Copied from https://github.com/arrow-kt/arrow/blob/0.10.1/modules/core/arrow-core-data/src/main/kotlin/arrow/typeclasses/ContinuationUtils.kt
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
private val coroutineImplClass by lazy { Class.forName("kotlin.coroutines.jvm.internal.BaseContinuationImpl") }
@phiSgr
phiSgr / ublock
Created October 14, 2023 17:04
Crude way to block questions with a keyword in SO
stackoverflow.com##.s-post-summary--content:has-text(//)