[フレーム]
Uploaded byjeffz
3,152 views

Jscex: Write Sexy JavaScript (中文)

This document discusses Jscex, which allows writing asynchronous JavaScript code in a synchronous style using the $await operator. It provides examples of rewriting bubble sort to use asynchronous compare and swap functions. It also discusses using Jscex to handle parallelism, I/O, and concurrency in applications. Key benefits mentioned include just-in-time compilation for performance and ahead-of-time compilation for smaller file sizes.

Embed presentation

Downloaded 221 times
Jscex Write Sexy JavaScript - - 2011.5
• / / Jeffrey Zhao / • • http://blog.zhaojie.me/ • @ • F#, JavaScript, Scala, C#, Python, .NET, Mono... • Java
Jscex • JavaScript Computation EXpression • JavaScript • F# JavaScript • " " • JavaScript JavaScript
Jscex • • Jscex JavaScript • • Jscex / • JavaScript / • ECMAScript 3
var compare = function (x, y) { return x - y; } var swap = function (a, i, j) { var t = a[x]; a[x] = a[y]; a[y] = t; } var bubbleSort = function (array) { for (var x = 0; x < array.length; x++) { for (var y = 0; y < array.length - x; y++) { if (compare(array[y], array[y + 1]) > 0) { swap(array, y, y + 1); } } } }
var compare = function (x, y, callback) { var innerLoop = function (array, x, y, callback) { setTimeout(10, function () { if (y < array.length - x) { callback(x - y); compare(array[y], array[y + 1], function (r) { }); if (r > 0) { } swap(array, y, y + 1, function () { innerLoop(array, x, y + 1, callback); var swap = function (a, i, j, callback) { }); var t = a[x]; a[x] = a[y]; a[y] = t; } else { repaint(a); innerLoop(array, x, y + 1, callback); } setTimeout(20, callback); }); } } else { callback(); var outerLoop = function (array, x, callback) { } if (x < array) { } innerLoop(array, x, 0, function () { outerLoop(array, x + 1, callback); outerLoop(array, 0, function () { }); console.log("done!"); } else { }); callback(); } }
var compare = function (x, y, callback) { var innerLoop = function (array, x, y, callback) { setTimeout(10, function () { if (y < array.length - x) { callback(x - y); compare(array[y], array[y + 1], function (r) { }); if (r > 0) { } swap(array, y, y + 1, function () { innerLoop(array, x, y + 1, callback); var swap = function (a, i, j, callback) { }); var t = a[x]; a[x] = a[y]; a[y] = t; } else { D repaint(a); innerLoop(array, x, y + 1, callback); } M setTimeout(20, callback); }); } } else { T callback(); var outerLoop = function (array, x, callback) { } if (x < array) { } innerLoop(array, x, 0, function () { outerLoop(array, x + 1, callback); outerLoop(array, 0, function () { }); console.log("done!"); } else { }); callback(); } }
var compareAsync = eval(Jscex.compile("async", function (x, y) { $await(Jscex.Async.sleep(10)); // each "compare" takes 10 ms. return x - y; })); var swapAsync = eval(Jscex.compile("async", function (a, x, y) { var t = a[x]; a[x] = a[y]; a[y] = t; // swap repaint(a); // repaint after each swap $await(Jscex.Async.sleep(20)); // each "swap" takes 20 ms. })); var bubbleSortAsync = eval(Jscex.compile("async", function (array) { for (var x = 0; x < array.length; x++) { for (var y = 0; y < array.length - x; y++) { var r = $await(compareAsync(array[y], array[y + 1])); if (r > 0) $await(swapAsync(array, y, y + 1)); } } }));
var compareAsync = eval(Jscex.compile("async", function (x, y) { $await(Jscex.Async.sleep(10)); // each "compare" takes 10 ms. return x - y; })); var swapAsync = eval(Jscex.compile("async", function (a, x, y) { var t = a[x]; a[x] = a[y]; a[y] = t; // swap repaint(a); // repaint after each swap $await(Jscex.Async.sleep(20)); // each "swap" takes 20 ms. })); var bubbleSortAsync = eval(Jscex.compile("async", function (array) { for (var x = 0; x < array.length; x++) { for (var y = 0; y < array.length - x; y++) { var r = $await(compareAsync(array[y], array[y + 1])); if (r > 0) $await(swapAsync(array, y, y + 1)); } } }));
JavaScript • • •
• JavaScript • while / for / for...in / do • if / switch • try...catch...finally • return / break / continue / throw • • with • break continue • switch break •
• JavaScript • "bind" $await • • " " • " "
• JavaScript • • • JIT
• • • • •
// var somethingAsync = eval(Jscex.compile("async", function (...) { // } ));
function () { var res = $await(<async work>); }
function () { var res = $await(<async work>); } HTTP UI Web Service
function () { var img = $await(readAsync("http://...")); console.log("loaded!"); $await(writeAsync("./files/...")); console.log("saved!"); } = (function () { var _b_ = Jscex.builders["async"]; return _b_.Start(this, _b_.Delay(function () { _b_.Bind(readAsync(...), function (img) { console.log("loaded!"); return _b_.Bind(writeAsync(...), function () { console.log("saved!"); return _b_.Normal(); }); }); }) ); })
Express var app = express.createServer(); app.get('/', function (req, res) { /** * * * 1. * 2. * * 3. res * * " " **/ }); app.listen(3000);
app.getAsync('/', eval(Jscex.compile("async", function (req, res) { var keys = $await(db.getKeysAsync(...)); var results = []; for (var i = 0; i < keys.length; i++) { var r = $await(cache.getAsync(keys[i])); if (!r) { r = $await(db.getAsync(keys[i])); } results.push(r); } res.send(generateList(results)); })));
I/O • I/O • Web • • • I/O •
var getDataAsync = eval(Jscex.compile("async", function (key) { var res = $await(cache.getAsync(key)); if (res) return res; return $await(db.getAsync(key)); })); app.getAsync('/', eval(Jscex.compile("async", function (req, res) { var keys = $await(db.getKeysAsync(...)); // " " var tasks = keys.map(function (key) { return getDataAsync(key); }); // var results = $await(Jscex.Async.parallel(tasks)); res.send(generateList(results)); })));
• • • $await(Jscex.Async.parallel(taskA, taskB)) • $await(taskA.continueWith(taskB)) • $await • • • • taskA.start(); $await(taskB); $await(taskA);
// var i = 1; conn.onAsync("data", eval(Jscex.compile("async", function () { var id = i++; $await(step1); console.log("step 1 - request " + id); $await(step2); console.log("step 2 - request " + id); /** * * step 1 - request 1 * step 1 - request 2 * step 2 - request 2 * step 2 - request 1 **/ })));
Erlang var i = 0; var agent = Agent.start(eval(Jscex.compile("async", function (mailbox) { var id = i++; var msg = $await(mailbox.receive()); $await(step1); console.log("step 1 - request " + id); $await(step2); console.log("step 2 - request " + id); }))); conn.on("data", function (data) { // mailbox agent.send(data); });
Jscex
• • JIT • AOT • • • Python C# JavaScript 1.7 • ...
AOT // AOT Agent.start(eval(Jscex.compile("async", function (mailbox) { ... }))); // AOT // // gzip 3kb Agent.start((function (mailbox) { var _b_ = Jscex.builders["async"]; return _b_.Start(this, ... ); }));
Jscex • • " " •
// var fib = eval(Jscex.compile("seq", function () { var i = 0, j = 1; while (true) { $yield(i); // the bind operation var t = i; i = j; j += t; } })); var iter = fib().skip(10).take(10); while (iter.moveNext()) { console.log(iter.current); }
... Maybe Monad var maybeFunc = function () { var maybeA = getA(); if (maybeA == Maybe.None) return Maybe.None; var maybeB = getB(); if (maybeB == Maybe.None) return Maybe.None; return maybeA.value + maybeB.value; } // var maybeFunc = eval(Jscex.compile("maybe", function () { var a = $try(getA()); var b = $try(getB()); return a + b; }));
Jscex
• " " " " • • • • Jscex /
• • • "debugger" • • Start, Delay, Combine • Loop, Try • Normal, Return, Break, Continue, Throw • Bind
Node.js
• BSD • • https://github.com/JeffreyZhao/jscex • http://www.sndacode.com/projects/jscex
Q &A
Jscex: Write Sexy JavaScript (中文)

More Related Content

Jscex: Write Sexy JavaScript
PDF
Jscex: Write Sexy JavaScript
深入浅出Jscex
PDF
深入浅出Jscex
The Evolution of Async-Programming (SD 2.0, JavaScript)
PDF
The Evolution of Async-Programming (SD 2.0, JavaScript)
响应式编程及框架
PDF
响应式编程及框架
Javascript Uncommon Programming
PDF
Javascript Uncommon Programming
The Evolution of Async-Programming on .NET Platform (TUP, Full)
PDF
The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
PDF
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
Promise: async programming hero
PDF
Promise: async programming hero
Jscex: Write Sexy JavaScript
Jscex: Write Sexy JavaScript
深入浅出Jscex
深入浅出Jscex
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
响应式编程及框架
响应式编程及框架
Javascript Uncommon Programming
Javascript Uncommon Programming
The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
Promise: async programming hero
Promise: async programming hero

What's hot

Developer Experience i TypeScript. Najbardziej ikoniczne duo
PDF
Developer Experience i TypeScript. Najbardziej ikoniczne duo
LetSwift RxSwift 시작하기
PDF
LetSwift RxSwift 시작하기
Coding in Style
PDF
Coding in Style
SDC - Einführung in Scala
PPT
SDC - Einführung in Scala
Futures e abstração - QCon São Paulo 2015
PDF
Futures e abstração - QCon São Paulo 2015
RxSwift 활용하기 - Let'Swift 2017
PDF
RxSwift 활용하기 - Let'Swift 2017
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
PDF
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
YUI Tidbits
PPTX
YUI Tidbits
Scala in practice
PDF
Scala in practice
rx.js make async programming simpler
PDF
rx.js make async programming simpler
The Ring programming language version 1.5.3 book - Part 10 of 184
PDF
The Ring programming language version 1.5.3 book - Part 10 of 184
미려한 UI/UX를 위한 여정
PDF
미려한 UI/UX를 위한 여정
Apache PIG - User Defined Functions
PDF
Apache PIG - User Defined Functions
The Ring programming language version 1.5.4 book - Part 10 of 185
PDF
The Ring programming language version 1.5.4 book - Part 10 of 185
Swift internals
PDF
Swift internals
Intro to Pig UDF
ZIP
Intro to Pig UDF
連邦の白いヤツ 「Objective-C」
KEY
連邦の白いヤツ 「Objective-C」
Евгений Крутько, Многопоточные вычисления, современный подход.
PPT
Евгений Крутько, Многопоточные вычисления, современный подход.
Groovy 1.8の新機能について
KEY
Groovy 1.8の新機能について
Extend R with Rcpp!!!
PDF
Extend R with Rcpp!!!
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
LetSwift RxSwift 시작하기
LetSwift RxSwift 시작하기
Coding in Style
Coding in Style
SDC - Einführung in Scala
SDC - Einführung in Scala
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
RxSwift 활용하기 - Let'Swift 2017
RxSwift 활용하기 - Let'Swift 2017
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
YUI Tidbits
YUI Tidbits
Scala in practice
Scala in practice
rx.js make async programming simpler
rx.js make async programming simpler
The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184
미려한 UI/UX를 위한 여정
미려한 UI/UX를 위한 여정
Apache PIG - User Defined Functions
Apache PIG - User Defined Functions
The Ring programming language version 1.5.4 book - Part 10 of 185
The Ring programming language version 1.5.4 book - Part 10 of 185
Swift internals
Swift internals
Intro to Pig UDF
Intro to Pig UDF
連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」
Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.
Groovy 1.8の新機能について
Groovy 1.8の新機能について
Extend R with Rcpp!!!
Extend R with Rcpp!!!

Similar to Jscex: Write Sexy JavaScript (中文)

Javascript Everywhere
ZIP
Javascript Everywhere
CoffeeScript
PDF
CoffeeScript
JavaScript 101
PDF
JavaScript 101
Coffee script
PDF
Coffee script
Txjs
KEY
Douglas Crockford: Serversideness
PDF
Douglas Crockford: Serversideness
谈谈Javascript设计
KEY
谈谈Javascript设计
谈谈Javascript设计
KEY
谈谈Javascript设计
Less ismorewithcoffeescript webdirectionsfeb2012
PPTX
Less ismorewithcoffeescript webdirectionsfeb2012
Event driven javascript
PDF
Event driven javascript
Event driven javascript
PDF
Event driven javascript
Useful javascript
PDF
Useful javascript
Functional Programming in Javascript - IL Tech Talks week
PPTX
Functional Programming in Javascript - IL Tech Talks week
mobl - model-driven engineering lecture
PDF
mobl - model-driven engineering lecture
Emerging Languages: A Tour of the Horizon
PDF
Emerging Languages: A Tour of the Horizon
CoffeeScript
PDF
CoffeeScript
5 Tips for Better JavaScript
PPTX
5 Tips for Better JavaScript
Advanced JavaScript
PPTX
Advanced JavaScript
04 Advanced Javascript
PDF
04 Advanced Javascript
Javascript best practices
PPTX
Javascript best practices
Javascript Everywhere
Javascript Everywhere
CoffeeScript
CoffeeScript
JavaScript 101
JavaScript 101
Coffee script
Coffee script
Txjs
Douglas Crockford: Serversideness
Douglas Crockford: Serversideness
谈谈Javascript设计
谈谈Javascript设计
谈谈Javascript设计
谈谈Javascript设计
Less ismorewithcoffeescript webdirectionsfeb2012
Less ismorewithcoffeescript webdirectionsfeb2012
Event driven javascript
Event driven javascript
Event driven javascript
Event driven javascript
Useful javascript
Useful javascript
Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks week
mobl - model-driven engineering lecture
mobl - model-driven engineering lecture
Emerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the Horizon
CoffeeScript
CoffeeScript
5 Tips for Better JavaScript
5 Tips for Better JavaScript
Advanced JavaScript
Advanced JavaScript
04 Advanced Javascript
04 Advanced Javascript
Javascript best practices
Javascript best practices

More from jeffz

Wind.js无障碍调试与排错
PDF
Wind.js无障碍调试与排错
JavaScript现代化排错实践
PDF
JavaScript现代化排错实践
Jscex:案例、阻碍、体会、展望
PDF
Jscex:案例、阻碍、体会、展望
Jscex:案例、经验、阻碍、展望
PDF
Jscex:案例、经验、阻碍、展望
The Evolution of Async Programming (GZ TechParty C#)
PDF
The Evolution of Async Programming (GZ TechParty C#)
Mono for .NET Developers
PDF
Mono for .NET Developers
单点登录解决方案的架构与实现
PDF
单点登录解决方案的架构与实现
Documentation Insight技术架构与开发历程
PDF
Documentation Insight技术架构与开发历程
Windows Phone应用开发心得
PDF
Windows Phone应用开发心得
分布式版本管理
PDF
分布式版本管理
使用.NET构建轻量级分布式框架
PDF
使用.NET构建轻量级分布式框架
针对iPad平台的高性能网站架构
PDF
针对iPad平台的高性能网站架构
企业开发领域的语言特性
PDF
企业开发领域的语言特性
大话程序员可用的算法
PDF
大话程序员可用的算法
面向对象与生活
PDF
面向对象与生活
Windows内核技术介绍
PDF
Windows内核技术介绍
F#语言对异步程序设计的支持
PDF
F#语言对异步程序设计的支持
大众点评网的技术变迁之路
PDF
大众点评网的技术变迁之路
Better Framework Better Life
PDF
Better Framework Better Life
Why Java Sucks and C# Rocks (Final)
PDF
Why Java Sucks and C# Rocks (Final)
Wind.js无障碍调试与排错
Wind.js无障碍调试与排错
JavaScript现代化排错实践
JavaScript现代化排错实践
Jscex:案例、阻碍、体会、展望
Jscex:案例、阻碍、体会、展望
Jscex:案例、经验、阻碍、展望
Jscex:案例、经验、阻碍、展望
The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)
Mono for .NET Developers
Mono for .NET Developers
单点登录解决方案的架构与实现
单点登录解决方案的架构与实现
Documentation Insight技术架构与开发历程
Documentation Insight技术架构与开发历程
Windows Phone应用开发心得
Windows Phone应用开发心得
分布式版本管理
分布式版本管理
使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架
针对iPad平台的高性能网站架构
针对iPad平台的高性能网站架构
企业开发领域的语言特性
企业开发领域的语言特性
大话程序员可用的算法
大话程序员可用的算法
面向对象与生活
面向对象与生活
Windows内核技术介绍
Windows内核技术介绍
F#语言对异步程序设计的支持
F#语言对异步程序设计的支持
大众点评网的技术变迁之路
大众点评网的技术变迁之路
Better Framework Better Life
Better Framework Better Life
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)

Recently uploaded

Industrial RFID Landscape for 2025 - Readers, Tags, Antennas, Printers, etc
PDF
Industrial RFID Landscape for 2025 - Readers, Tags, Antennas, Printers, etc
Benefits of Using the IAC 500 Sensor for Ambient Conditions
PDF
Benefits of Using the IAC 500 Sensor for Ambient Conditions
threat-hunters-cookbook for cybersecurity
PDF
threat-hunters-cookbook for cybersecurity
How Application Performance Monitoring Tools Are Used in Performance Testing
PDF
How Application Performance Monitoring Tools Are Used in Performance Testing
Techbrains Baku 2025 by GoUP - all speaker session
PDF
Techbrains Baku 2025 by GoUP - all speaker session
Artificial Intelligence(AI) full Book.pdf
PDF
Artificial Intelligence(AI) full Book.pdf
AN Introduction to UNIX File System—An Approach
PPTX
AN Introduction to UNIX File System—An Approach
AI Bot Traffic Surge: Retail Fraud Threat for Age-Restricted Websites
PPTX
AI Bot Traffic Surge: Retail Fraud Threat for Age-Restricted Websites
Search Engine Responses to Conspiratorial Search Practices AANZCA 2025 Presen...
PPTX
Search Engine Responses to Conspiratorial Search Practices AANZCA 2025 Presen...
"Lessons from Yesterday's Tomorrowland" by Scott M. Graffius
PDF
"Lessons from Yesterday's Tomorrowland" by Scott M. Graffius
Regenerative Agriculture Finance : Environmental impact
PDF
Regenerative Agriculture Finance : Environmental impact
HDTV and DTV Standards: The United States Opts for a Digital HDTV Standard
PPT
HDTV and DTV Standards: The United States Opts for a Digital HDTV Standard
The_Boardroom_Cyber_Playbook_Research_Edition
PDF
The_Boardroom_Cyber_Playbook_Research_Edition
Unit 1.2 Components of a Computer System.pdf
PDF
Unit 1.2 Components of a Computer System.pdf
A Brief Introduction About Christopher Elwell Woburn
PDF
A Brief Introduction About Christopher Elwell Woburn
Pizza Chain Market Data Scraping for Better Insights Report.pptx
PPTX
Pizza Chain Market Data Scraping for Better Insights Report.pptx
Xemelgo - RFID Industry Predictions for 2026
PDF
Xemelgo - RFID Industry Predictions for 2026
Small Business Automation: A Comprehensive Cost and ROI Guide
PDF
Small Business Automation: A Comprehensive Cost and ROI Guide
AI Driven & AI Native Development AI native development
PDF
AI Driven & AI Native Development AI native development
Cybersecurity Basics: Understanding Threats, Protection Methods, and Safe Dig...
PPTX
Cybersecurity Basics: Understanding Threats, Protection Methods, and Safe Dig...
Industrial RFID Landscape for 2025 - Readers, Tags, Antennas, Printers, etc
Industrial RFID Landscape for 2025 - Readers, Tags, Antennas, Printers, etc
Benefits of Using the IAC 500 Sensor for Ambient Conditions
Benefits of Using the IAC 500 Sensor for Ambient Conditions
threat-hunters-cookbook for cybersecurity
threat-hunters-cookbook for cybersecurity
How Application Performance Monitoring Tools Are Used in Performance Testing
How Application Performance Monitoring Tools Are Used in Performance Testing
Techbrains Baku 2025 by GoUP - all speaker session
Techbrains Baku 2025 by GoUP - all speaker session
Artificial Intelligence(AI) full Book.pdf
Artificial Intelligence(AI) full Book.pdf
AN Introduction to UNIX File System—An Approach
AN Introduction to UNIX File System—An Approach
AI Bot Traffic Surge: Retail Fraud Threat for Age-Restricted Websites
AI Bot Traffic Surge: Retail Fraud Threat for Age-Restricted Websites
Search Engine Responses to Conspiratorial Search Practices AANZCA 2025 Presen...
Search Engine Responses to Conspiratorial Search Practices AANZCA 2025 Presen...
"Lessons from Yesterday's Tomorrowland" by Scott M. Graffius
"Lessons from Yesterday's Tomorrowland" by Scott M. Graffius
Regenerative Agriculture Finance : Environmental impact
Regenerative Agriculture Finance : Environmental impact
HDTV and DTV Standards: The United States Opts for a Digital HDTV Standard
HDTV and DTV Standards: The United States Opts for a Digital HDTV Standard
The_Boardroom_Cyber_Playbook_Research_Edition
The_Boardroom_Cyber_Playbook_Research_Edition
Unit 1.2 Components of a Computer System.pdf
Unit 1.2 Components of a Computer System.pdf
A Brief Introduction About Christopher Elwell Woburn
A Brief Introduction About Christopher Elwell Woburn
Pizza Chain Market Data Scraping for Better Insights Report.pptx
Pizza Chain Market Data Scraping for Better Insights Report.pptx
Xemelgo - RFID Industry Predictions for 2026
Xemelgo - RFID Industry Predictions for 2026
Small Business Automation: A Comprehensive Cost and ROI Guide
Small Business Automation: A Comprehensive Cost and ROI Guide
AI Driven & AI Native Development AI native development
AI Driven & AI Native Development AI native development
Cybersecurity Basics: Understanding Threats, Protection Methods, and Safe Dig...
Cybersecurity Basics: Understanding Threats, Protection Methods, and Safe Dig...

Jscex: Write Sexy JavaScript (中文)

  • 1.
  • 2.
    / / Jeffrey Zhao / • • http://blog.zhaojie.me/ • @ • F#, JavaScript, Scala, C#, Python, .NET, Mono... • Java
  • 3.
    Jscex • JavaScript Computation EXpression • JavaScript • F# JavaScript • " " • JavaScript JavaScript
  • 4.
    Jscex • • Jscex JavaScript • • Jscex / • JavaScript / • ECMAScript 3
  • 6.
    var compare = function (x, y) { return x - y; } var swap = function (a, i, j) { var t = a[x]; a[x] = a[y]; a[y] = t; } var bubbleSort = function (array) { for (var x = 0; x < array.length; x++) { for (var y = 0; y < array.length - x; y++) { if (compare(array[y], array[y + 1]) > 0) { swap(array, y, y + 1); } } } }
  • 7.
    var compare = function (x, y, callback) { var innerLoop = function (array, x, y, callback) { setTimeout(10, function () { if (y < array.length - x) { callback(x - y); compare(array[y], array[y + 1], function (r) { }); if (r > 0) { } swap(array, y, y + 1, function () { innerLoop(array, x, y + 1, callback); var swap = function (a, i, j, callback) { }); var t = a[x]; a[x] = a[y]; a[y] = t; } else { repaint(a); innerLoop(array, x, y + 1, callback); } setTimeout(20, callback); }); } } else { callback(); var outerLoop = function (array, x, callback) { } if (x < array) { } innerLoop(array, x, 0, function () { outerLoop(array, x + 1, callback); outerLoop(array, 0, function () { }); console.log("done!"); } else { }); callback(); } }
  • 8.
    var compare = function (x, y, callback) { var innerLoop = function (array, x, y, callback) { setTimeout(10, function () { if (y < array.length - x) { callback(x - y); compare(array[y], array[y + 1], function (r) { }); if (r > 0) { } swap(array, y, y + 1, function () { innerLoop(array, x, y + 1, callback); var swap = function (a, i, j, callback) { }); var t = a[x]; a[x] = a[y]; a[y] = t; } else { D repaint(a); innerLoop(array, x, y + 1, callback); } M setTimeout(20, callback); }); } } else { T callback(); var outerLoop = function (array, x, callback) { } if (x < array) { } innerLoop(array, x, 0, function () { outerLoop(array, x + 1, callback); outerLoop(array, 0, function () { }); console.log("done!"); } else { }); callback(); } }
  • 9.
    var compareAsync = eval(Jscex.compile("async", function (x, y) { $await(Jscex.Async.sleep(10)); // each "compare" takes 10 ms. return x - y; })); var swapAsync = eval(Jscex.compile("async", function (a, x, y) { var t = a[x]; a[x] = a[y]; a[y] = t; // swap repaint(a); // repaint after each swap $await(Jscex.Async.sleep(20)); // each "swap" takes 20 ms. })); var bubbleSortAsync = eval(Jscex.compile("async", function (array) { for (var x = 0; x < array.length; x++) { for (var y = 0; y < array.length - x; y++) { var r = $await(compareAsync(array[y], array[y + 1])); if (r > 0) $await(swapAsync(array, y, y + 1)); } } }));
  • 10.
    var compareAsync = eval(Jscex.compile("async", function (x, y) { $await(Jscex.Async.sleep(10)); // each "compare" takes 10 ms. return x - y; })); var swapAsync = eval(Jscex.compile("async", function (a, x, y) { var t = a[x]; a[x] = a[y]; a[y] = t; // swap repaint(a); // repaint after each swap $await(Jscex.Async.sleep(20)); // each "swap" takes 20 ms. })); var bubbleSortAsync = eval(Jscex.compile("async", function (array) { for (var x = 0; x < array.length; x++) { for (var y = 0; y < array.length - x; y++) { var r = $await(compareAsync(array[y], array[y + 1])); if (r > 0) $await(swapAsync(array, y, y + 1)); } } }));
  • 12.
  • 13.
    JavaScript • while / for / for...in / do • if / switch • try...catch...finally • return / break / continue / throw • • with • break continue • switch break •
  • 14.
    JavaScript • "bind" $await • • " " • " "
  • 15.
    JavaScript • • • JIT
  • 17.
    • • • • •
  • 18.
    // var somethingAsync = eval(Jscex.compile("async", function (...) { // } ));
  • 19.
    function () { var res = $await(<async work>); }
  • 20.
    function () { var res = $await(<async work>); } HTTP UI Web Service
  • 21.
    function () { var img = $await(readAsync("http://...")); console.log("loaded!"); $await(writeAsync("./files/...")); console.log("saved!"); } = (function () { var _b_ = Jscex.builders["async"]; return _b_.Start(this, _b_.Delay(function () { _b_.Bind(readAsync(...), function (img) { console.log("loaded!"); return _b_.Bind(writeAsync(...), function () { console.log("saved!"); return _b_.Normal(); }); }); }) ); })
  • 22.
    Express var app = express.createServer(); app.get('/', function (req, res) { /** * * * 1. * 2. * * 3. res * * " " **/ }); app.listen(3000);
  • 23.
    app.getAsync('/', eval(Jscex.compile("async", function (req, res) { var keys = $await(db.getKeysAsync(...)); var results = []; for (var i = 0; i < keys.length; i++) { var r = $await(cache.getAsync(keys[i])); if (!r) { r = $await(db.getAsync(keys[i])); } results.push(r); } res.send(generateList(results)); })));
  • 24.
    I/O • I/O • Web • • • I/O •
  • 25.
    var getDataAsync = eval(Jscex.compile("async", function (key) { var res = $await(cache.getAsync(key)); if (res) return res; return $await(db.getAsync(key)); })); app.getAsync('/', eval(Jscex.compile("async", function (req, res) { var keys = $await(db.getKeysAsync(...)); // " " var tasks = keys.map(function (key) { return getDataAsync(key); }); // var results = $await(Jscex.Async.parallel(tasks)); res.send(generateList(results)); })));
  • 26.
    • • $await(Jscex.Async.parallel(taskA, taskB)) • $await(taskA.continueWith(taskB)) • $await • • • • taskA.start(); $await(taskB); $await(taskA);
  • 28.
    // var i = 1; conn.onAsync("data", eval(Jscex.compile("async", function () { var id = i++; $await(step1); console.log("step 1 - request " + id); $await(step2); console.log("step 2 - request " + id); /** * * step 1 - request 1 * step 1 - request 2 * step 2 - request 2 * step 2 - request 1 **/ })));
  • 29.
    Erlang var i = 0; var agent = Agent.start(eval(Jscex.compile("async", function (mailbox) { var id = i++; var msg = $await(mailbox.receive()); $await(step1); console.log("step 1 - request " + id); $await(step2); console.log("step 2 - request " + id); }))); conn.on("data", function (data) { // mailbox agent.send(data); });
  • 30.
  • 31.
    • JIT • AOT • • • Python C# JavaScript 1.7 • ...
  • 32.
    AOT // AOT Agent.start(eval(Jscex.compile("async", function (mailbox) { ... }))); // AOT // // gzip 3kb Agent.start((function (mailbox) { var _b_ = Jscex.builders["async"]; return _b_.Start(this, ... ); }));
  • 33.
    Jscex • • " " •
  • 34.
    // var fib = eval(Jscex.compile("seq", function () { var i = 0, j = 1; while (true) { $yield(i); // the bind operation var t = i; i = j; j += t; } })); var iter = fib().skip(10).take(10); while (iter.moveNext()) { console.log(iter.current); }
  • 35.
    ... Maybe Monad var maybeFunc = function () { var maybeA = getA(); if (maybeA == Maybe.None) return Maybe.None; var maybeB = getB(); if (maybeB == Maybe.None) return Maybe.None; return maybeA.value + maybeB.value; } // var maybeFunc = eval(Jscex.compile("maybe", function () { var a = $try(getA()); var b = $try(getB()); return a + b; }));
  • 36.
  • 37.
    " " " " • • • • Jscex /
  • 38.
    • • "debugger" • • Start, Delay, Combine • Loop, Try • Normal, Return, Break, Continue, Throw • Bind
  • 40.
  • 41.
    BSD • • https://github.com/JeffreyZhao/jscex • http://www.sndacode.com/projects/jscex
  • 42.

AltStyle によって変換されたページ (->オリジナル) /