/tmp/duangsuse.sock
23 subscribers
303 photos
3 videos
92 files
337 links
从 duangsuse::Echo (@dsuse) 跟进出来的分支,将在作者恢复原帐号访问的时候合并删除。
Download Telegram
(剧透警告)

《平凡的世界》 虽然讲的人都不太平凡,但居然末尾是个悲剧,死了癌症了很多人,我真的不再觉得看那个电视剧有啥意思了

剧透一点意思都没有,被人强行剧透了很无聊
/tmp/duangsuse.sock
(剧透警告) 《平凡的世界》 虽然讲的人都不太平凡,但居然末尾是个悲剧,死了癌症了很多人,我真的不再觉得看那个电视剧有啥意思了 剧透一点意思都没有,被人强行剧透了很无聊
是我爸剧透的。开始他说是悲剧,后来又说谁谁谁死了。
我讨厌看那种情况,所以以后干脆就别看算了,我还以为只是风波迭起,没想到真能全写死
#疫情动态

【特别报道 | 假如武汉的警铃有机会被拉响,可以是哪天?】
这条时间线将表明,疫情在早期是如何被发现却又最终被轻视。(第一财经YiMagazine)(Telegraph 备份)
Forwarded from Deleted Account
https://github.com/duangsuse-valid-projects/Share/blob/master/Others/kt_misc/pkt_9/Parser.kt
分享一个在写的 Kotlin 库,是关于文本解析器的

比如正则的 /(.)(\1)/
val aa = Contextual(anyChar) { item(it) }
aa.read("!!") //Tuple2(first=!, second=!)
aa.rebuild("!!") //!!

还有 /(.)(.)(.)/
val three = Seq(::CharTuple, anyChar, anyChar, anyChar)
three.read("sda") //(s, d, a)
three.rebuild("sda") { this[2] = 'c' } //sdc
比如无数个奇数
val odd = satisfy<Int>("odd") { it % 2 != 0 }
val odds = Repeat(asList(), odd)
odds.read(1, 2, 3) //[1]
odds.rebuild(1, 3) //[1, 3]

比如 JSON 字符串(不完整)
val escapedChar = anyChar and !item('\\')
val s = Seq(::StringTuple, item('"').asStringPat(), *escapedChar until item('"'))
s.read("\"321\t31\"") //(", 32131, ")

有没有大佬想试试 🌝
……自从我引入了 Preety 这个 supertype,对 ParserKt Kotlin 的类型推导就不准确了
grammar hosts;

hostfile: line* EOF;
line : item
| comment
;
item: IPADDRESS HOSTNAME ;
comment: COMMENT;
HOSTNAME: [A-z0-9.]+;
IPADDRESS: NUM '.' NUM '.' NUM '.' NUM;
NUM: [0-9] +;
COMMENT: '#' .*? '\r'? '\n';
NL: ('\r\n'|'\n\r'|'\r'|'\n') ->skip;
BLANK: (' '|'\t') ->skip;

https://blog.iseki.space/2018/09/07/antlr-first/
#PTL latte-lang.org/ 草现在 layout 已经这么普及了么
我点下了第 100 个 star……
🌚 🙈 #Haha #life #school #China #nsfw 这就尴尬了
Forwarded from 电报时报
This media is not supported in your browser
VIEW IN TELEGRAM
附湖北仙桃一中女教师,网课结束忘记关直播,附音频
Forwarded from Deleted Account
String 不是 primitive,它是 Ljava/lang/String,一个 class 啊
但它的确是 java 语言层面有特殊意义的,所以在 java.lang 包里,你看那个包还有 Enum, Iterable, AutoCloseable 什么的
Forwarded from Deleted Account
没办法,CLR 里是基础类型、JVM 里不是
C 里面其实也不是
Forwarded from Deleted Account
存在即合理,每次遇到的时候想想 James Gosling 和 Java Langauge Specification 再比个自己的视角高度就清楚了,至少人家能实现 Java 编译器而咱们只会用 Java。
Forwarded from Deleted Account
有些东西也是没有绝对的对错的,就我感觉…… 其实支持也没啥大不了的,只不过 String#replace, String#matches, String#startsWith 这种方法调用会麻烦很多,而且 string 也是一样不能被 extends
Kotlin 里就不存在这个问题,因为没有所谓的 primitive,只有 boxed/unboxed array (Array<Int> vs. IntArray)
val p=Seq(::CharTuple, item('s'),item('k'),item('r'),item('~')).toStringPat()
Repeat(asList(), p).read("skr~skr~skr~")
MutableList<kotlin.String>? = [skr~, skr~, skr~]
Forwarded from 小喵 🐈
Protocol.kt
7.6 KB
Forwarded from Deleted Account
- @Deprecated("Should not use ...for internal") 全都应该用 internal 修饰符替了
- this.apply 不要用了,apply 本来就是指定 this 的。Builder 里使用要严格按照一致的缩进和形式使用
- fun ping(payload: Long) 换成异步 non-blocking 的更好
- fun pingLatency() = ping(System.currentTimeMillis()).let { System.currentTimeMillis() - it }
可以 import kotlin.time.*fun pingLatency() = measureTime { runPing() },不必把 begin_time 做成参数
好吧如果那个是做在协议上支持的就不能了
- if (serverPort !in 1..65535) throw IllegalArgumentException 可以写成 require(serverPort in 1..65535) {"Illegal port $serverPort, require value in 1..65535"}
- 最后 companion object 的那段实在是太“空”了,不易读,最好得改
- Packet.writeVarInt 是专门的位运算算法,实现应该和框架层隔离开
do {
val lowbits = i and 0x7F
i = i ushr 7
val signbit = i != 0
val byte = if (signbit) lowbits and 0x80 else lowbits
writeByte(byte)
} while (i != 0)
- Packet.Builder.append(Int|Long) 是有二进制位操作的数值上的算法,应该和 Packet.writeVarInt 泛化合并
- 不要用 Packet.Builder,用 PacketWriter,Builder 不是用来写结构化数据的。
fun <T> Iterable<T>.fliterIndex(predicate: Predicate<T>) = withIndex().filter ( predicate(value) }.map(IndexedValue<*>::index)

fun <T> Iterable<T>.shuffleFilter(predicate: Predicate<T>): Pair<List<T>, List<Int>> {
val shuffled = shuffle()
val indices = shuffled.filterIndex(predicate)
return shuffled to indices
}


fun <T> List<T>.shuffleKeepIndex(index: Int): Pair<Int, List<T>> {
val unallocated = indices.toMutableSet()
val newList = copy()
val keptIndex = Random.nextInt(unallocated.indices)
newList[keptIndex] = this[index]; unallocated.remove(keptIndex)
unallocated.forEach { newList[it] = this[index] }
return keptIndex to newList
}

不对

fun List<T>.shuffleKeepIndex(index: Int): Pair<Int, List<T>> {
val newList = copy().also { removeAt(index) }
val keptIndex = Random.nextInt(indices)
newList.insertAt(keptIndex, this[index])
return keptIndex to newList
}


也不对
依据原有 Collection 还是依据 indices 建模都不好啊…… 1 vs. N input index/predicate 的情况都不好弄啊……

fun <T> MutableList<T>.shuffleKeepIndex(index: Int): Pair<Int, List<T>> {
val keptIndex = Random.nextInt(indices)
val newList = indices.map { if (it == keptIndex) this[index] else this.removeAt(0) }
return keptIndex to newList
}


下面这个是针对 source collection 建模的,没必要用 mutable iterator

fun <T> Iterator<T>.shuffleMerge(value: T): Pair<Int, List<T>> {
val mergedIndex = Random.nextInt(0 until size)
val newList = (0 until size).shuffled().map { if (it == mergedIndex) value else next() }
return mergedIndex to newList
}


#Kotlin
总之:
import kotlin.random.*
fun <T> Collection<T>.shuffleMerge(value: T): Pair<Int, List<T>> {
val iterator = iterator()
val mergedIndex = Random.nextInt(indices)
val newList = indices.map { if (it == mergedIndex) value else iterator.next() }
return mergedIndex to newList
}

很明显需要两个数据,要么先 shuffle 再 search 要么然早就知道 index 是何,然后改另一个

fun <T> Collection<T>.shuffleMerge(value: T): Pair<Int, List<T>> {
val newRange = 0..lastIndex+1
val mergedIndex = Random.nextInt(range)
val iterator = iterator()
val newList = newRange.shuffled().map { if (it == mergedIndex) value else iterator.next() }
return mergedIndex to newList
}
This media is not supported in your browser
VIEW IN TELEGRAM