我就直接回在下面了
回應 https://gist.github.com/rayshih/4144d6b8bc045fc26daf8887bd0cb4e2。
我一直覺得你的發言和其他人沒有交集,但想不通到底哪裡出了問題。但看了這段程式之後,我覺得有點懂了。原文的程式不容易看 timestamp(加上 1th 2th 3th 讓我豆頁痛),所以我稍微改寫如下。
import asyncio
我就直接回在下面了
回應 https://gist.github.com/rayshih/4144d6b8bc045fc26daf8887bd0cb4e2。
我一直覺得你的發言和其他人沒有交集,但想不通到底哪裡出了問題。但看了這段程式之後,我覺得有點懂了。原文的程式不容易看 timestamp(加上 1th 2th 3th 讓我豆頁痛),所以我稍微改寫如下。
import asyncio
| from datetime import datetime | |
| import asyncio | |
| # The code is based on this link: | |
| # https://www.facebook.com/photo.php?fbid=1723296854349009&set=p.1723296854349009&type=3&theater | |
| # And of course, the output is exactly the same. | |
| # For conveniency, I attached the output at the end of this file. | |
| # ----- begin of polyfill ----- | |
| Await = 'await' |
但只用純 Python 的 generator 沒辦法為 Python 的所有功能實作 async variant,因為這會有雞生蛋生雞問題。最簡單的矛盾:如果你只用 generator 實作 async API,那要怎麼實作 async 的 generator?
嗯?為什麼不行?
import asyncio
import time
def async_generator():
@asyncio.coroutine| import express from 'express' | |
| import {Observable} from 'rxjs' | |
| import {pipe, append, map, reduce} from 'ramda' | |
| // ----- begin of framework layer ----- | |
| // This bridges express framework into rx world | |
| const toRoute$ = app => ([method, route, handler]) => | |
| Observable.create(observer => { | |
| app[method](route, (req, res) => { |
| data Program next = Output String next | Done | |
| p = Output "Hello World" (Output "Good Bye" Done) | |
| -- an interpreter to run | |
| run :: Program (Program (Program next)) -> IO () | |
| run (Output s1 (Output s2 Done)) = do | |
| putStrLn s1 | |
| putStrLn s2 |
| // ----- start of "extracted functions" ------ | |
| const toggleLoggedActivity = (data) => { | |
| const selectedActivity = getActivity(activities, data.activityKey); | |
| const selectedActivitySubCategory = getSubCategory(activities, data.activityKey); | |
| const transactionCallback = toggleLoggedActivity(data, activities, selectedActivity, selectedActivitySubCategory); | |
| const loggedProfileRef = firebase.database() | |
| .ref(`loggedActivities/${data.datetime}/${data.profileUid}`); | |
| return utils.getValue(loggedProfileRef) |
| Array.prototype.flatMap = function(f) { | |
| const r = [] | |
| for (var i = 0; i < this.length; i++) { | |
| f(this[i]).forEach(v => { | |
| r.push(v) | |
| }) | |
| } | |
| return r | |
| } |
| const createExecObj = exec => ({ | |
| exec, | |
| bind(f) { | |
| return createExecObj(cb => { | |
| this.exec(y => { | |
| f(y).exec(cb) | |
| }) | |
| }) | |
| } | |
| }) |