Skip to content

Instantly share code, notes, and snippets.

View ichenhe's full-sized avatar

Chenhe ichenhe

View GitHub Profile
@ichenhe
ichenhe / Promise.kt
Last active November 23, 2022 10:59
Using Kotlin to implemant Promise (js) style coroutine. 用 Kotlin 实现 Promise (js) 风格的协程 API
import kotlin.concurrent.thread
import kotlin.coroutines.*
typealias Callback<T> = (T) -> Unit
class Promise<T>(private val run: (Callback<T>) -> Unit) {
fun then(callback: Callback<T>) {
run(callback)
}
}
@ichenhe
ichenhe / Generator.kt
Created July 20, 2022 08:35
Using Kotlin to implemant python style coroutine. 用 Kotlin 实现 Python 风格的协程 API
import kotlin.coroutines.*
fun <T> generator(block: suspend GeneratorScope<T>.() -> Unit) = Generator(block)
class Generator<T>(private val block: suspend GeneratorScope<T>.() -> Unit) {
operator fun iterator(): Iterator<T> = GeneratorIterator(block)
}
@RestrictsSuspension
interface GeneratorScope<T> {
@ichenhe
ichenhe / demo.yaml
Created May 9, 2022 05:09
Extract tag in github actions
# !! restriction: trigger must be push:tags !!
name: Demo
on:
push:
tags:
- v[0-9]+*
permissions:
contents: read
@ichenhe
ichenhe / keybase.md
Created February 5, 2022 10:59
keybase.md

Keybase proof

I hereby claim:

  • I am ichenhe on github.
  • I am chenhe (https://keybase.io/chenhe) on keybase.
  • I have a public key ASC6Lc1lhOdKxv58kI17e3X2HYkEXTT1n7N5z1ffYmhsTAo

To claim this, I am signing this object:

@ichenhe
ichenhe / AcAuto.java
Last active July 29, 2022 09:25
支持中文的 AC 自动机多模式匹配
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
public class AcAuto {
@ichenhe
ichenhe / MathParser.kt
Last active August 15, 2022 23:21
Kotlin 数学表达式解析(支持函数与负号)
import java.util.*
import kotlin.math.abs
import kotlin.math.sin
class MathParser {
companion object {
private const val ZERO_CODE = '0'.toInt() // 便于通过字符串编码快速 char -> int
private const val TOKEN_NULL: Short = -1
private const val TOKEN_NUM: Short = 1
@ichenhe
ichenhe / NestedScrollableHost.kt
Created October 12, 2020 07:26
Fix parallel scroll with a NestedScrollView inside a ViewPager2
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@ichenhe
ichenhe / proxy.md
Created September 26, 2020 06:02
Setup proxy for WSL2
  1. Add the following code to .bashrc.
  2. Run source .bashrc to reload.
  3. Run proxy to setup proxy, and unproxy to unproxy.
function getip {
        export winip=$(ip route | grep default | awk '{print $3}')
        export wslip=$(hostname -I | awk '{print $1}')
        echo "WIN IP: ${winip}"
        echo "WSL IP: ${wslip}"