yield

ES6的generator

纵饮孤独 提交于 2019-11-28 16:05:14
generator(生成器)是ES6标准引入的新的数据类型。一个generator看上去像一个函数,但可以返回多次。 1、generator函数的定义是function*,这与普通函数是不同的 2、yield需使用在generator函数中,所表示的意思是“执行到这,并返回” 3、yield是有返回值的,其中value代表return的值,done代表函数是否执行结束。 yield 'your value'; // { value: 'your value', done: true } yield; // { value: undefined, done: true } generator跟函数很像,定义如下: function* foo(x) { yield x + 1; yield x + 2; return x + 3; } generator和函数不同的是,generator由 function* 定义(注意多出的 * 号),并且,除了 return 语句,还可以用 yield 返回多次。用处:可以编写一个产生斐波那契数列的函数: function* fib(max) { var t, a = 0, b = 1, n = 0; while (n < max) { yield a; [a, b] = [b, a + b]; n ++; } return; }fib(5)

Where to use yield in Python best?

戏子无情 提交于 2019-11-28 15:39:35
问题 I know how yield works. I know permutation, think it just as a math simplicity. But what's yield 's true force? When should I use it? A simple and good example is better. 回答1: yield is best used when you have a function that returns a sequence and you want to iterate over that sequence, but you do not need to have every value in memory at once. For example, I have a python script that parses a large list of CSV files, and I want to return each line to be processed in another function. I don't

生成器

只愿长相守 提交于 2019-11-28 15:31:26
一、yield关键字 yield的英文单词意思是生产,在函数中但凡出现yield关键字,再调用函数,就不会继续执行函数体代码,而是会返回一个值。 def func(): print(1) yield print(2) yield g = func() print(g) <generator object func at 0x10ddb6b48> 生成器的本质就是迭代器,同时也并不仅仅是迭代器,不过迭代器之外的用途实在是不多,所以我们可以大声地说:生成器提供了非常方便的自定义迭代器的途径。并且从Python 2.5+开始,[PEP 342:通过增强生成器实现协同程序]的实现为生成器加入了更多的特性,这意味着生成器还可以完成更多的工作。这部分我们会在稍后的部分介绍。 def func(): print('from func 1') yield 'a' print('from func 2') yield 'b' g = func() print(F"g.__iter__ == g: {g.__iter__() == g}") res1 = g.__next__() print(f"res1: {res1}") res2 = next(g) print(f"res2: {res2}") # next(g) # StopIteration g.__iter__ == g: True

Python: generator expression vs. yield

别来无恙 提交于 2019-11-28 15:11:19
In Python, is there any difference between creating a generator object through a generator expression versus using the yield statement? Using yield : def Generator(x, y): for i in xrange(x): for j in xrange(y): yield(i, j) Using generator expression : def Generator(x, y): return ((i, j) for i in xrange(x) for j in xrange(y)) Both functions return generator objects, which produce tuples, e.g. (0,0), (0,1) etc. Any advantages of one or the other? Thoughts? Thanks everybody! There is a lot of great information and further references in these answers! There are only slight differences in the two.

Rails check if yield :area is defined in content_for

痞子三分冷 提交于 2019-11-28 15:00:42
问题 I want to do a conditional rendering at the layout level based on the actual template has defined content_for(:an__area) , any idea how to get this done? 回答1: @content_for_whatever is deprecated. Use content_for? instead, like this: <% if content_for?(:whatever) %> <div><%= yield(:whatever) %></div> <% end %> 回答2: not really necessary to create a helper method: <% if @content_for_sidebar %> <div id="sidebar"> <%= yield :sidebar %> </div> <% end %> then of course in your view: <% content_for

Generator ∕ yield 与 async ∕ await

给你一囗甜甜゛ 提交于 2019-11-28 14:58:38
原文引用 大专栏 https://www.dazhuanlan.com/2019/08/26/5d6340cf158ef/ 异步编程对JS的重要性应该是不言而喻了的,作为单线程的编程语言,如果没有异步编程,体验不要太酸爽 比如我们有一个读取文档并进行处理的任务。如果有异步编程,先是向操作系统发出请求,要求读取文档。然后,进程会去执行其他任务,等到操作系统返回文档,再接着处理文档的任务 相反的,如果没有异步函数,上述编程就变成了同步的执行方式。由于是连续执行,不能插入其他任务,所以操作系统从硬盘读取文档的这段时间,进程只能干等着 Promise 从 Promise 开始,JavaScript 就在引入新功能来实现更简单的方法处理异步编程,帮助我们远离回调地狱 回调地狱也就是多个回调函数嵌套带来的问题。比如读取文档A之后,再读取文档B fs.readFile(fileA, function (err, data) { fs.readFile(fileB, function (err, data) { // ... }) }) 很明显,在读取多个文档的情况下,代码就会出现多重嵌套 一个很明显的特点是, 代码不是纵向发展,而是横向发展的 ,造成的结果是代码混乱,无法管理 为了解决这个问题,就出现了 Promise 注意, 它不是新的语法功能,而是一种新的写法 ,这里就不具体介绍它的实现了

Why can't I use yield with return?

北城余情 提交于 2019-11-28 14:15:41
问题 I would like you to consider the following code: def func(alist): if len(alist) == 1: return arg * 2 for item in alist: yield item * 2 When I run it, I get this error: SyntaxError: 'return' with argument inside generator Now, I realize that I cannot do this. However, I would like to know why. What exactly is going on behind the scenes that is causing Python to throw the SyntaxError ? 回答1: Python has to decide whether a function is a generator at bytecode compilation time. This is because the

Trouble yielding inside a block/lambda

无人久伴 提交于 2019-11-28 13:31:22
I have the following Ruby code: # func1 generates a sequence of items derived from x # func2 does something with the items generated by func1 def test(x, func1, func2) func1.call(x) do | y | func2.call(y) end end func1 = lambda do | x | for i in 1 .. 5 yield x * i end end func2 = lambda do | y | puts y end test(2, func1, func2) # Should print '2', '4', '6', '8', and '10' This does not work, of course. test.rb:11: no block given (LocalJumpError) from test.rb:10:in `each' from test.rb:10 from test.rb:4:in `call' from test.rb:4:in `test' from test.rb:20 Lambdas don't implicitly accept blocks like

迭代器与生成器

浪尽此生 提交于 2019-11-28 13:18:33
迭代器与生成器 一、迭代器   迭代器可以理解为一种特殊的游标,是对循环遍历等一系列操作组成的一种抽象描述。而迭代器协议是程序的一种绑定关系,实现了该协议的对象称为可迭代对象。迭代器协议强调对象必须提供一个 next或 __next__()方法,并且执行该方法只有两种决策,要么返回迭代中的下一项,要么者引起一个 StopIteration异常,以终止迭代。 for循环的本质是循环所有对象,使用的一定是迭代器协议生成对象。因此 for循环可以遍历所有的可迭代对象(字符串、列表、元组、字典、文件对象等)。既然如此,为什么我们定义一个序列的时候没有使用 next方法呢?这是为什么呢?从理论上来讲,只有实现迭代器的对象才可称为可迭代对象。而我们在定义字符串、列表、元组、字典、文件对象的时候,本身没有给出 next方法。从这种角度上来看,他们并没有遵循迭代器协议。但是平时我们为什么还是认为他们是可迭代对象呢?   Python提供了一个可以让某种数据类型变为可迭代数据类型的方法,即让某种数据类型的对象直接调用 __iter__()或 iter()方法,此时我们再查看该数据类型的对象时就多出了 next方法。下面如我们通过一个简单的实例来分析,我们使用字符窜调用 __iter__()方法,然后使用可迭代对象调用 next方法。 string = "hello world" myiter =

python之 yield --- “协程”

烂漫一生 提交于 2019-11-28 13:17:38
在编程中我们经常会用到列表,以前使用列表时需要声明和初始化,在数据量比较大的时候也需要把列表完整生产出来,例如要存放1000给数据,需要准备长度1000的列表,这样计算机就需要准备内存放置这个列表,在Python中,这种一边循环一边计算的机制,称为生成器:generator,这个功能在列表使用时比较节省空间,使用方法: g=(i*2 for i in range(10)) data=g.__next__() print(d) 取列表时data=g.__next__(),此时才去生成。 应用:生成斐波拉契数列 def fig(num): n,a,b=0,0,1 while n<num: yield b a,b=b,a+b n+=1 return 'done' fig(10) 定义长度为10的数列 fig(10) for i in range(10): try: x=next(g) print(x) except StopIteration as e: print('error %s' % e.value) break 运行结果: 1 1 2 3 5 8 13 21 34 55 这里,最难理解的就是generator和函数的执行流程不一样。函数是顺序执行,遇到 return 语句或者最后一行函数语句就返回。而变成generator的函数,在每次调用 next() 的时候执行,遇到