通过 call/cc 给 Ice 实现 Coroutine
前两天给 Ice 加了 call/cc, 为此还重构了一波, 实现 call/cc 还是因为看了轮子哥的大专系列(
里边说提供 continuation 语言实现 Coroutine 起来很轻松, 后来又查了一些资料, 都说 continuation 表达能力很强, 就实现了一手, 调用方式完全等同 call/cc, 既然是看 Coroutine 才要实现 call/cc, 那实现了之后当然要用 call/cc 实现一手 Coroutine 了(
不过这个 Coroutine 比较简陋, 只提供 create, resume, yield, destroy
coroutine: (@() {
@funcs: [];
@conts: [];
@deletes: [];
@co_cont: none;
@create(func) {
if deletes.empty() {
@id: funcs.size();
funcs.push(func);
conts.push(none);
return id;
} else {
@id: deletes.pop();
funcs[id]: func;
conts[id]: none;
return id;
}
}
@resume(id) {
if conts[id] = none {
conts[id]: call_with_current_continuation(@(cont) {
co_cont: cont;
funcs[id]();
});
} else {
conts[id]: call_with_current_continuation(@(cont) {
co_cont: cont;
conts[id](none);
});
}
}
@yield() {
call_with_current_continuation(co_cont);
}
@destroy(id) {
funcs[id]: conts[id]: none;
deletes.push(id);
}
return @() {};
})();
测试一下:
@foo() {
@i: 0;
while i < 5 {
println(i: i + 1);
coroutine.yield();
}
}
@id1: coroutine.create(foo);
@id2: coroutine.create(foo);
@i: 0;
while i < 5 {
coroutine.resume(id1);
coroutine.resume(id2);
i: i + 1;
}
coroutine.destroy(id1);
coroutine.destroy(id2);
输出
1
1
2
2
3
3
4
4
5
5
里边唯一我觉得有点意思的地方是 resume 里, 直接在 lambda 里将当前的 continuation 绑定到 co_cont 上, 这样用户 create 的时候传入的函数就只需要调用 yield 了
可惜除了 vsc 都没有 Ice 的高亮(
通过 call/cc 给 Ice 实现 Coroutine的更多相关文章
- clang 搭建和编译boost 和zero ICE库 (Ubuntu10 64)
相关介绍资料如下: Boost编译http://sourceforge.net/projects/boost/files/boost/1.55.0/boost_1_55_0.tar.gz/downlo ...
- call/cc 总结 | Scheme
call/cc 总结 | Scheme 来源 https://www.sczyh30.com/posts/Functional-Programming/call-with-current-contin ...
- 4800: [Ceoi2015]Ice Hockey World Championship(折半搜索)
4800: [Ceoi2015]Ice Hockey World Championship Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 622 S ...
- 栈编程和函数控制流: 从 continuation 与 CPS 讲到 call/cc 与协程
原标题:尾递归优化 快速排序优化 CPS 变换 call/cc setjmp/longjmp coroutine 协程 栈编程和控制流 讲解 本文为部分函数式编程的扩展及最近接触编程语言控制流的学习和 ...
- Coroutine in Java - Quasar Fiber实现--转载
转自 https://segmentfault.com/a/1190000006079389?from=groupmessage&isappinstalled=0 简介 说到协程(Corout ...
- The Coroutine
关于Coroutine 说到coroutine就不的不说subroutine,也就是我们常用到的一般函数.调用一个函数开始执行,然后函数执行完成后就退出,再次调用的时候,再从头开始,调用之间是没有保存 ...
- ZeroC Ice启用SSL通讯的配置
Zeroc ICE ( Internet Communications Engine )中间件号称标准统一,开源,跨平台,跨语言,分布式,安全,服务透明,负载均衡,面向对象,性能优越,防火墙穿透,通讯 ...
- Atitti.dw cc 2015 绿色版本安装总结
Atitti.dw cc 2015 绿色版本安装总结 1.1. 安装程序无法初始化.请下载adobe Support Advisor检测该问题.1 1.1.1. Adobe Application M ...
- c coroutine
今天看了下云风c coroutine 代码 博客,发现 coroutine 实现原理其实还比较简单,就用户态栈切换,只需要几十行汇编,特别轻量级. 具体实现 1. 创建一个coroutine: 也就 ...
随机推荐
- ':app@debug/compileClasspath': Could not find any version that matches com.android.support:appcompat-v7:30.+.
ERROR: Unable to resolve dependency for ':app@debug/compileClasspath': Could not find any version th ...
- @RestController和@Controller的关系
@RestController注解,相当于@Controller+@ResponseBody两个注解的结合
- vue3.0+ts+setup语法糖props写法
写法一 import defaultImg from '@/assets/images/defaultImg.png' const props = defineProps({ src: { type: ...
- PingFang(苹方)字体的引用
原文 链接:https://pan.baidu.com/s/1rw39Yqo9fv9BYz_JZ5lyRw 提取码:o7kf 苹方-简 常规体 font-family: pingFangSC-Regu ...
- Redis可视化内存分析工具--RDR:查看Redis中key的占用情况
RDR(redis datareveal)是一个解析redis rdbfile的工具.与redis-rdb-tools 相比,RDR 是由 golang 实现的,速度要快得多(5GB rdbfile ...
- day 9 scanf输入和gets输入的区别
(1).计算机高级语言程序运行方法有编译执行和编译解释两种,一下叙述中正确的是[A] A.C语言程序仅可以编译执行 B.C语言程序仅可以解释执行 C.C语言程序既可以编译执行又可以解释执行 D.以上说 ...
- [Keil 学习] printf, scanf函数的用法
C语言库函数中有一批"标准输入输出函数",它是以标准的输入输出设备(一般为终端设备)为输入输出对象的,其中用得比较多的是printf和scanf函数了. 在嵌入式设备中加入C语言的 ...
- 【小记录】解决链接libcufft_static.a库出现的错误
程序中使用了 cv::cuda::dft() 函数,需要在链接的时候使用libcufft_static.a这个库.链接出现大量类似错误:error: undefined reference to __ ...
- golang中的标准库strconv
strconv 包 strconv包实现了基本数据类型与其字符串表示的转换,主要有以下常用函数: Atoi().Itia().parse系列.format系列.append系列. string与int ...
- 理解cpu load
三种命令 1. w 2. uptime 3. top CPU负载和CPU利用率的区别 1)CPU利用率:显示的是程序在运行期间实时占用的CPU百分比 2)CPU负载:显示的是一段时间内正在使用和等待使 ...