Last active
January 23, 2018 08:19
-
-
Save hzhangxyz/09bd1cca87afb7e5349e9143f5829789 to your computer and use it in GitHub Desktop.
learn js Promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pool = [] | |
pool.push([1,1]) | |
plus = async (a,b)=>await a+ await b | |
time = async (a)=>2* await a | |
main = async() =>{ | |
for(var i=1;i<=10;i++){ | |
pool.push([ | |
plus(pool[i-1][0], pool[i-1][1]), | |
time(pool[i-1][1]) | |
]) | |
} | |
return await pool[10][1] | |
} | |
main().then((n)=>console.log(n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def plus(a,b): | |
return await a + await b | |
async def time(a): | |
return 2* await a | |
async def one(): | |
return 1 | |
pool = [[one(),one()]] | |
async def main(): | |
for i in range(1,11): | |
pool.append([ | |
plus(pool[i-1][0],pool[i-1][1]), | |
time(pool[i-1][1]) | |
]) | |
print(await pool[10][1]) | |
return 0 | |
import asyncio | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plus = async (l,m,i)=>{ | |
while(l[i]<m)i++; | |
return i; | |
} | |
minus = async (l,m,j)=>{ | |
while(l[j]>m)j--; | |
return j; | |
} | |
var _qsort = async (l,a,b) =>{ | |
if(a>=b)return Promise.resolve([]) | |
var m = l[parseInt((a+b)/2)]; | |
var i=a, j=b; | |
while(i<=j){ | |
[i,j] = await Promise.all([plus(l,m,i), minus(l,m,j)]) | |
if(i<=j){ | |
[l[i],l[j]]=[l[j],l[i]]; | |
i++; | |
j--; | |
} | |
} | |
left = _qsort(l,a,j) | |
right = _qsort(l,i,b) | |
return await Promise.all([left, right]) | |
} | |
var qsort = (l) => { | |
_qsort(l,0,l.length-1).then(()=> | |
console.log(l) | |
) | |
} | |
l = [56,8934,324,57,32,243,57,7,43,324,25,35,78,743,32,423,54456756] | |
qsort(l) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment