Forwarded from dnaugsuz
Kotlin 的 type inference 也可以那么做,我们知道,Java 7 Project Coins 的 diamond 可以省略 Type arguments,可那个特性很简单就可以实现,它不需要比老 type checker 多设计太多。
而我们的 Kotlin 非常的强大,比如说你甚至可以定义出这样的扩展方法,Java 是没法推导出
比如我们有一个
应该知道它的 constructor
(我用一种伪语法 <T, T1, ...> 去指定下文类型变量)
可以写明 SomeContainer<Int>、SomeContainer<String> 可对于
所以不可能只死板地"推导"
据说 unification 可以在给定 <T>
但是具体怎么做还是关系式编程,不了解…… 算是科普一下 Kotlin 的一些细节
关系式引用 https://minikanren.org/ http://tca.github.io/veneer/editor.html
List<String> list = new LinkedList<String>(); // Java6这就是利用静态类型系统的规则直接可以得到,何况开始设计的也几乎是说:『只有在类型参数很冗余的时候才简化』。
List<String> list = new LinkedList<>(); // Java7
而我们的 Kotlin 非常的强大,比如说你甚至可以定义出这样的扩展方法,Java 是没法推导出
R 这个类型参数的(不能以扩展函数的形式,而且也推不出来):fun <T, R> T.cast(): R = this as R有两种可以有类型的地方:提供值的地方如
val x: Int = "".cast()
//java.lang.ClassCastException
listOf<Int>()、需要值的地方如 val xs: List<Int> 但不止那个,比如函数参数的需求处也是需求处,这些地方都可以标明类型。比如我们有一个
data class SomeContainer<T>(val item: T) 应该知道它的 constructor
::SomeContainer 的类型是 <T> (T) -> SomeContainer<T> (这里请把 constructor 当成普通函数)(我用一种伪语法 <T, T1, ...> 去指定下文类型变量)
可以写明 SomeContainer<Int>、SomeContainer<String> 可对于
val x: SomeContainer<Int> = SomeContainer() 这种风格就不好看了。所以不可能只死板地"推导"
val 处的类型参数 <T> 而要根据所有能知道的信息『推导』出 T 的实际类型据说 unification 可以在给定 <T>
(::SomeContainer as ((T) -> SomeContainer<T>)) is SomeContainer<WTF> 的情况下推出那个 ((T) -> SomeContainer<T>) 里 T 的实际类型 = WTF,然后就不用手工写了但是具体怎么做还是关系式编程,不了解…… 算是科普一下 Kotlin 的一些细节
关系式引用 https://minikanren.org/ http://tca.github.io/veneer/editor.html
https://github.com/neilgall/KotlinKanren/
Kotlin 的 MiniKanren 也有了,可是 是从 Swift port 过去的,而且实现的易读性、设计的可扩展性都不够,我打算写基础的 "Literate Kotlin" 辅助 JS 脚本(临时),然后实现一个 Kotlin 的 MiniKanren(其实是 microKanren……)。
Kotlin 的 MiniKanren 也有了,可是 是从 Swift port 过去的,而且实现的易读性、设计的可扩展性都不够,我打算写基础的 "Literate Kotlin" 辅助 JS 脚本(临时),然后实现一个 Kotlin 的 MiniKanren(其实是 microKanren……)。
GitHub
GitHub - neilgall/KotlinKanren: Port of SwiftyKanren to Kotlin
Port of SwiftyKanren to Kotlin. Contribute to neilgall/KotlinKanren development by creating an account on GitHub.
duangsuse::Echo
https://github.com/neilgall/KotlinKanren/ Kotlin 的 MiniKanren 也有了,可是 是从 Swift port 过去的,而且实现的易读性、设计的可扩展性都不够,我打算写基础的 "Literate Kotlin" 辅助 JS 脚本(临时),然后实现一个 Kotlin 的 MiniKanren(其实是 microKanren……)。
我来谈谈 Literate Kotlin 的脚本应该怎么写。 #JavaScript
这个脚本,就是要允许我们在文章里嵌入 Kotlin 代码和依赖代码的示例,
然后每个相对独立且可以作为 Kotlin File 编译的部分完成后,显式一个按钮以归总一个部分的代码,并允许在 Kotlin Playground 执行它。
(感谢 JetBrains 特地包装的这个编辑器,使用一点也不困难,尽可能减少了我在无意义事情上花费的时间和痛苦)
至于 Kotlin Playground 的部分他们提供了很简单的 API:
每个独立部分 <template literateBegin /> 起始、 <template literateEnd /> 结束,其中可以有许多不可嵌套的 <template exampleBegin /> 和 <template exampleEnd />。
之前我是直接写 imperative 表述式来 filter 出 language-kotlin 的 <code>,后来我抽提了 nextSiblings 和 takeWhile,就不那么死板了:
这个 filterCode 函数直接给它起始 <template literateBegin> Element 就可以了
然后在页面渲染完成后,可以添加钩子逻辑,来在 <template literateEnd> 后插入一个 button 来显式此部分的代码 <code>。
但关键问题是,如何区分 literate 和 example,这个问题也很好解决,利用『模式(pattern)』来表达:
解析的结果是 literate element 的集合,以及 example element 的集合。
这个脚本,就是要允许我们在文章里嵌入 Kotlin 代码和依赖代码的示例,
然后每个相对独立且可以作为 Kotlin File 编译的部分完成后,显式一个按钮以归总一个部分的代码,并允许在 Kotlin Playground 执行它。
(感谢 JetBrains 特地包装的这个编辑器,使用一点也不困难,尽可能减少了我在无意义事情上花费的时间和痛苦)
至于 Kotlin Playground 的部分他们提供了很简单的 API:
playground('code') // enable on all <code>
所以我们只负责拼合代码。每个独立部分 <template literateBegin /> 起始、 <template literateEnd /> 结束,其中可以有许多不可嵌套的 <template exampleBegin /> 和 <template exampleEnd />。
之前我是直接写 imperative 表述式来 filter 出 language-kotlin 的 <code>,后来我抽提了 nextSiblings 和 takeWhile,就不那么死板了:
function *nextSiblings(e) { for (let c = e; c!=null; c=c.nextSibling) yield c; }
function *takeWhile(p, xs) { for (let x of xs) if (p(x)) { yield x; } else break; }
function filterCode(begin_e, p = e => e.classList.contains("language-kotlin")) {
let neighbors = nextSiblings(begin_e);
let section = takeWhile(notSectionEnd, neighbors);
return [...section].filter(p).map(e => e.innerText).join("");
} 这个 filterCode 函数直接给它起始 <template literateBegin> Element 就可以了
然后在页面渲染完成后,可以添加钩子逻辑,来在 <template literateEnd> 后插入一个 button 来显式此部分的代码 <code>。
但关键问题是,如何区分 literate 和 example,这个问题也很好解决,利用『模式(pattern)』来表达:
Sections = {Section}
Section = <template literateBegin/> (anyElement|Example)*? <template literateEnd/>
Example = <template exampleBegin/> anyElement*? <template exampleEnd/>
这么做是为了 literate|example 模式数据提取实现不止可用于 Kotlin 代码,之后我们从 anyElement 中 filter 出需要的 Kotlin code。解析的结果是 literate element 的集合,以及 example element 的集合。
GitHub
GitHub - JetBrains/kotlin-playground: Self-contained component to embed in websites for running Kotlin code
Self-contained component to embed in websites for running Kotlin code - JetBrains/kotlin-playground
duangsuse::Echo
思维江化,无法和曾经费了很长时间想的模型对应起来。
This media is not supported in your browser
VIEW IN TELEGRAM
想到了,原来我让 next() 去回 lastItem,然后 [1, 2, 3] 第一次 lastItem=1;next 流就是 [1,2,3],比之前 constructor: lastItem=gen.next() 多 next 一次是为了 next 出所有项目包括 peek 的。
而现在的 peek 实际上更加直白,就是让 iterator 把那个直接补回去,不必再用 tailConsumed 那一套 peek+yield in next 修补方案了,也没有 tail 需要特殊处理的问题。
而现在的 peek 实际上更加直白,就是让 iterator 把那个直接补回去,不必再用 tailConsumed 那一套 peek+yield in next 修补方案了,也没有 tail 需要特殊处理的问题。
class SaveIterator {
constructor(gen) {
this.gen = gen;
this.res0 = gen.next();
this.lastItem = this.res0.value;
}
*iterator() {
if (this.res0.done) return;
yield this.lastItem;
for (let item of this.gen) {
this.lastItem = item;
yield item;
}
this.lastItem = undefined;
this.res0.done = true; //no next
}
[Symbol.iterator] = this.iterator;
}let b = new SaveIterator([1,2,3].values())
[...takeWhile(x => x < 3, b)] //Array [ 1, 2 ]
b.lastItem //3
[...takeWhile(x => x <= 3, b)] //Array [ 3 ]
b.lastItem //undefinedSection = literateBegin (anyElement|Example)*? literateEnd
Example = exampleBegin anyElement*? exampleEnd
duangsuse::Echo
😂 Sticker
简直莫名其妙,我不是要写 Kotlin 文章的,怎么开始写起这个来了……
算了不要什么 exampleBegin 了,真是无聊。
算了不要什么 exampleBegin 了,真是无聊。
暂时没简化
其实 ES6 也不是不能简化 DOM 树/Event 的构建/操作,而且还很有效,可我暂时不想再设计,毕竟无关痛痒。
document.createElement 什么的,大家不喜勿喷。其实 ES6 也不是不能简化 DOM 树/Event 的构建/操作,而且还很有效,可我暂时不想再设计,毕竟无关痛痒。
今天有点晚了就写不了那个关系式的了…… 但愿明天,还有为了绝句成功完成必须的 Literate Kotlin 项目生成软件能正常写出来。
先修补下找出,绝句这几天就完善了『第一人称』『第二人称』『第三人称』和中缀链、基本语法模型。
剧透一下,那个关系式的 最后总结有 6 个基本元素:State, Variable, Introduce, Eq, Either, Both
fun <V, K> Iterable<V>.hist(key: (V) -> K): Map<K, List<V>> {
val histogram: MutableMap<K, MutableList<V>> = mutableMapOf()
for(item in this) histogram.getOrPut(key(item), ::mutableListOf).add(item)
return histogram
}
listOf(1, "abc", "emmm", -1)
[1, abc, emmm, -1]listOf(1, "abc", "emmm", -1).hist { it::class }
{class kotlin.Int=[1, -1], class kotlin.String=[abc, emmm]} #Kotlin 先修补下找出,绝句这几天就完善了『第一人称』『第二人称』『第三人称』和中缀链、基本语法模型。
剧透一下,那个关系式的 最后总结有 6 个基本元素:State, Variable, Introduce, Eq, Either, Both