Skip to content

Instantly share code, notes, and snippets.

View lgzh1215's full-sized avatar
🤒
Out sick

Dexlind lgzh1215

🤒
Out sick
  • Gensokyo
View GitHub Profile
@lgzh1215
lgzh1215 / ByteArrayWapper.kt
Created December 10, 2018 13:03
kotlinx.serialization protobuf bytes
import kotlinx.serialization.*
import kotlinx.serialization.internal.SerialClassDescImpl
import java.lang.reflect.Field
@Serializable
class ByteArrayWapper(val byteArray: ByteArray) {
@Serializer(forClass = ByteArrayWapper::class)
companion object : KSerializer<ByteArrayWapper> {
private val decoderField: Field = Class.forName("kotlinx.serialization.protobuf.ProtoBuf\$ProtobufReader")
.getDeclaredField("decoder")
@lgzh1215
lgzh1215 / PhantomType.kt
Last active November 6, 2018 17:17
关于phantom type的一个简陋的kotlin的例子,作用如main所示
fun main(): Unit = with(SomeBuilder) {
SomeBuilder.getBuilder().setS2("2").setS1("1").buildSome() // ok
SomeBuilder.getBuilder().setS1("1").setS2("2").buildSome() // ok
SomeBuilder.getBuilder().setS1("1").buildSome() // error
SomeBuilder.getBuilder().setS2("2").buildSome() // error
SomeBuilder.getBuilder().buildSome() // もerror
}
class Some(val s1: String, val s2: String)
@lgzh1215
lgzh1215 / tsundb.kt
Created October 16, 2018 03:17
no description
package tsundb
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path
// ==UserScript==
// @name 知乎隐私政策应对
// @description 不同意!
// @match *://www.zhihu.com/*
// ==/UserScript==
(function() {
'use strict';
var __t = 0;
var __timer = setTimeout(function () {
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
public class LinearProbingHashMap<K, V> implements Map<K, V> {
@lgzh1215
lgzh1215 / Puzzle.kt
Last active May 21, 2017 09:41
求该程序的运行结果。(保证都是下划线,不是什么其他的看起来像下划线的字符)(嘻嘻~)(逃~
fun main(args: Array<String>) {
println(吔屎啦下划线地狱(127, 127, 128))
}
fun 吔屎啦下划线地狱(______________: Int?, ____________: Int?, _____________: Int?) = ______________ === _____________
// 这道题目利用了 超长的下划线名称 和 Java自动装箱的破事 以及 出题者对回答者的诱导(如果有的话) 糊弄眼球转移注意力。
// 答案很简单:
@lgzh1215
lgzh1215 / Switch.kt
Last active September 8, 2017 03:13
Kotlin劝退流
fun main(vararg args: String) {
val str = "4"
var i = 0
switch(str) {
case("5") { i++; println("case(\"5\")"); 打断() }
case("4") { i++; println("case(\"4\")") }
case("3") { i++; println("case(\"3\")") }
case("2") { i++; println("case(\"2\")"); 打断() }
case("1") { i++; println("case(\"1\")") }
}
@lgzh1215
lgzh1215 / ApplicativesPlayground.kt
Created April 25, 2017 14:13
Haskell as Kotlin
import Option.Some
fun main(args: Array<String>) {
println(Some({ a: Int -> a + 3 }).apply(Some(2)))
// => Some(5)
println(Some(2).apply(Some({ a: Int -> a + 3 })))
// => Some(5)
val array = arrayOf<(Int) -> Int>({ it + 3 }, { it * 2 }) apply arrayOf(1, 2, 3)
fun main(args: Array<String>) {
val r = "hello" next ::length next ::double
println(r)
}
fun length(s:String) = s.length
fun double(i : Int) = 2 * i
infix fun <T, R> T.next(map : (T) -> R) : R = map(this)