yield

yield from a list of generators created from an array

主宰稳场 提交于 2019-11-27 15:31:41
I've got this recursive generator var obj = [1,2,3,[4,5,[6,7,8],9],10] function *flat(x) { if (Array.isArray(x)) for (let y of x) yield *flat(y) else yield 'foo' + x; } console.log([...flat(obj)]) It works fine, but I don't like the for part. Is there a way to write it functionally? I tried if (Array.isArray(x)) yield *x.map(flat) which didn't work. Is there a way to write the above function without for loops? You could use rest parameters ... and check the length of the rest array for another calling of the generator function* flat(a, ...r) { if (Array.isArray(a)) { yield* flat(...a); } else

How to Pythonically yield all values from a list?

戏子无情 提交于 2019-11-27 15:27:28
问题 Suppose I have a list that I wish not to return but to yield values from. What is the most pythonic way to do that? Here is what I mean. Thanks to some non-lazy computation I have computed the list ['a', 'b', 'c', 'd'] , but my code through the project uses lazy computation, so I'd like to yield values from my function instead of returning the whole list. I currently wrote it as following: my_list = ['a', 'b', 'c', 'd'] for item in my_list: yield item But this doesn't feel pythonic to me. 回答1

What happens when promise is yielded in javascript?

我是研究僧i 提交于 2019-11-27 15:17:50
Didn't find full answer .. What happens when promise is yielded? Is such construction var p = new Promise() p.resolve(value) function * (){ yield p } equivalent to function * (){ yield value } ? UPDATE How to mix different styles of async programming, for example for such framework as koa? Koa middlewares are working with generators, but there are lots of good packages which are promise-based (sequelize for ex.) As Felix says, a promise is just another value to be yielded. However, there is a style of writing asynchronous code which makes use of yielded promises in a particular way. This

Python学习笔记---第三周

隐身守侯 提交于 2019-11-27 15:16:19
1.文件操作 (1)找到文件位 (2)双击打开 (3)进行一些操作(r - read(读)w - write(写)a - 追加 rb -读字节 wb - 写字节 ab - 追加写 r+ - 读写 w+ - 写读 a+ - 追加读 (4)关闭文件 open() -- 打开 open通过python控制操作系统进行打开文件 1.1 r模式 文件只能读取一次 格式:f = open("文件位置\文件名",mode="r"(不写默认为r),encoding="utf-8") print(f.read()) --- 全部读取 print(f.read(11)) -- 按照字符读取 print(f.readline()) -- 读取一行,默认尾部有个换行符 print(f.readline().strip()) -- 去除换行符 print(f.readlines()) -- 一行一行读取,全部储存在列表中 1.2 路径 绝对路径:从磁盘(C盘)开始查找 f = open(r"D:\Python_s25\day08\t1",mode="r",encoding="utf-8") 相对路径:相对于某个文件进行查找 f = open("t1",mode="r",encoding="utf-8") 路径转义:"E:\Python全栈"\tt电话 -- \t会被认为是制表符 解决办法:1.每个\变成\

生成器

烂漫一生 提交于 2019-11-27 13:54:55
普通函数 def generator(): print(1) return 'a' ret = generator() print(ret) View Code 生成器 #生成器函数/yield必须再函数里面且不能与return共用。 def generator(): print(1) yield 'a' #生成器函数执行之后会得到一个生成器作为一个返回值 ret = generator() print(ret) print(ret.__next__()) View Code 注意一点: 在生成器中yield不会结束一个程序。但是比较有意思的一点是当一个函数里面有多个yield的时候,需要多次调用和函数里面yield次数一样多的调用,例如: #生成器函数/yield必须再函数里面且不能与return共用。 def generator(): print(1) yield 'a' print(2) yield 'b' #生成器函数执行之后会得到一个生成器作为一个返回值 ret = generator() print(ret) print(ret.__next__()) print(ret) print(ret.__next__()) View Cod 如果只调用一次的话就会再第一次返回值时跳出函数并打印之前的代码,但是函数并没有结束,而是再等待下一次的调用。 来源: https:/

PythonDay12

大憨熊 提交于 2019-11-27 13:47:07
day12内置_函数 今日内容 生成器 推导式 内置函数一 生成器 什么是生成器?生成器的本质就是一个迭代器 迭代器是python自带的 生成器是程序员自己写的一种迭代器 生成器编写方式: 1.基于函数编写 2.推导式方式编写 def func(): print('这是一个函数') return '函数'func()​def func(): print("这是一个生成器") yield '生成器'​# 坑!!!,其实这个生成器只能执行一个next,因为只有一个yieldfunc() # 生成一个生成器print(func().__next__()) # 启动生成器print(func().__next__())# 上方这句代码的意思是:创建一个生成器并且执行一个next方法,两句一样但互不相干 函数体中出现yield代表要声明一个生成器 def func(): msg = input("请输入内容") yield msg print("这是第二次启动") yield "生成器2" yield "生成器3" yield "生成器4"​g = func()print(next(g))print(next(g))print(next(g))print(next(g)) # 不能超过yield次数 yield和return的区别 相同点: 都是返回内容 都可以返回多次

dvaJS Model之间的调用

ぐ巨炮叔叔 提交于 2019-11-27 13:01:26
const Model: ModelType = { namespace: 'namesps', state: { data: {} }, effects: { *fetch({ payload, callback }, { call, put, select }) { const res = yield call(queryApplyInvoiceInfo, payload); if (!res || !res.data) return; const total = yield select((state) => state.user.currentUser ) console.log(total); yield put({ type: 'change', payload: res }) if (callback) callback(); } }, reducers: { change (state, { payload }) { return { data: (state && state.data) || { list: [], pagination: {} } } } } }; export default Model; // 选择 state + '全局属性名(namespace)' + state属性名 const total = yield select((state

How to use 'yield' inside async function?

六月ゝ 毕业季﹏ 提交于 2019-11-27 12:02:35
I want to use generator yield and async functions. I read this topic , and wrote next code: import asyncio async def createGenerator(): mylist = range(3) for i in mylist: await asyncio.sleep(1) yield i*i async def start(): mygenerator = await createGenerator() for i in mygenerator: print(i) loop = asyncio.get_event_loop() try: loop.run_until_complete(start()) except KeyboardInterrupt: loop.stop() pass But i got the error: SyntaxError: 'yield' inside async function How to use yield generator in async function? Mikhail Gerasimov Upd: Starting with Python 3.6 we have asynchronous generators and

函数面试题合集

南楼画角 提交于 2019-11-27 11:02:31
是否使用过functools中的函数?他的作用是什么? functools.wraps() 在装饰器中用过,如果不使用wraps,则原始函数的__name__和__doc__的值就会丢失 functools.reduce() 第一个参数是一个函数,第二个参数是一个可迭代对象,代码如下: # 下面代码相当于从1加到9 from functools import reduce a=reduce(lambda x,y:x+y,range(10)) print(a) 如何判断一个值是方法还是函数? 参考链接 使用type()来判断,如果是method为方法,如果是function则是函数。 与类和实例无绑定关系的function都属于函数(function) 与类和实例有绑定关系的function都属于方法 请编写一个函数将ip地址转换成一个整数。如10.3.9.12转换成00001010 00000011 00001001 00001100,然后转换成整数 def ip2int(ip): nums=ip.split('.') # zfill()函数是补0 to_bin=[bin(int(i))[2:].zfill(8) for i in nums] return int(''.join(to_bin),2) i=ip2int('127.0.0.1') print(i)

Ruby's yield feature in relation to computer science

六月ゝ 毕业季﹏ 提交于 2019-11-27 10:39:36
I recently discovered Ruby's blocks and yielding features, and I was wondering: where does this fit in terms of computer science theory? Is it a functional programming technique, or something more specific? Chuck Ruby's yield is not an iterator like in C# and Python. yield itself is actually a really simple concept once you understand how blocks work in Ruby. Yes, blocks are a functional programming feature, even though Ruby is not properly a functional language. In fact, Ruby uses the method lambda to create block objects, which is borrowed from Lisp's syntax for creating anonymous functions