Skip to content

Instantly share code, notes, and snippets.

View rayshih's full-sized avatar

Ray Shih rayshih

View GitHub Profile

我就直接回在下面了

回應 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'
@rayshih
rayshih / async-generator.md
Last active May 17, 2017 03:17
如何實作 async 的 generator

但只用純 Python 的 generator 沒辦法為 Python 的所有功能實作 async variant,因為這會有雞生蛋生雞問題。最簡單的矛盾:如果你只用 generator 實作 async API,那要怎麼實作 async 的 generator?

嗯?為什麼不行?

import asyncio
import time

def async_generator():
    @asyncio.coroutine

Re: asyncio 好寫嗎?

首先,其實還滿開心有機會可以做個小小辯論的啦。我的回應如下

這把好幾個主題混在一起了,需要一個一個看。

1. Async Program 好寫嗎?

不好寫。東西好不好寫的意思,代表它和 programmer 的內在思考模式符合程度。Async program 不符合人類習慣思維,所以才會被說一開始的學習 overhead 很高,寫起來容易卡。尤其如果同步與異步 paradigms 並立(例如 Python),就更容易在切換的時候出問題。

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

test header 1

test header 2

test header 3

test header 4

@rayshih
rayshih / promises.js
Last active September 5, 2016 07:57 — forked from christianalfoni/promises.js
A comparison of promises and signals
// ----- 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)
})
})
}
})