这是一个测试yield使用的示例脚本......我做错了吗?它总是返回'1'......
#!/usr/bin/python
def testGen():
for a in [1,2,3,4,5,6,7,8,9,10]:
yield a
w = 0
while w < 10:
print testGen().next()
w += 1
Run Code Online (Sandbox Code Playgroud) 我传统上在C#中使用yield而没有返回,例如:
IEnumerable<T> Foobar() {
foreach( var foo in _stuff ) {
yield foo;
}
}
Run Code Online (Sandbox Code Playgroud)
但在其他例子中,我看到它写成"yield return foo;",请参阅:http://msdn.microsoft.com/en-us/library/9k7k7cf0%28VS.80%29.aspx.
有什么区别吗?
下面的代码有效,但我想使用yield或更改算法来优化代码.
public IEnumerable<Book> GetAuthorWithBookName(string keyword)
{
var bookAndAuthorList = new List<Book>();
List<Author> AuthorNameList = getAuthorName(keyword);
foreach (var author in AuthorNameList)
{
XmlNode booksNames = getBook(author);
XDocument XDOCbooksNames = XDocument.Parse(booksNames.OuterXml);
var bookNameList = (
from x1 in XDOCbooksNames.Descendants("Books")
select x1.Elements("book").Select(g => g.Attribute("name").Value))
.ToList();
foreach (var bookName in bookNameList)
{
bookAndAuthorList.Add(new Book()
{
authorName = author.authorName,
bookName = bookName
});
}
}
return bookAndAuthorList;
}
public class Book
{
public string authorName { get; set; }
public string bookName { get; set; …Run Code Online (Sandbox Code Playgroud) 我必须处理订单序列(这里是Int为了简化):
// the handleOrder methods are in fact much more complicated:
def handleOrders(prev: Double, orders: Seq[Int]): Double = prev + orders.sum
def handleOrder(prev: Double, order: Int): Double = prev / order
Run Code Online (Sandbox Code Playgroud)
由于所谓的结果
def nextGroup(prev: Double, orders: Seq[Int]): Seq[Double]
Run Code Online (Sandbox Code Playgroud)
function我得到另一个类的序列(这里是Double为了简化).
由此我实现了两个版本.
版本1(foldLeft和显式构建器):
def nextGroup1(prev: Double, orders: Seq[Int]): Seq[Double] = {
import collection.mutable.Builder
import collection.immutable.VectorBuilder
val bld: Builder[Double, Seq[Double]] = new VectorBuilder[Double]
var first = true
orders.foldLeft(prev) { (prev, order) =>
val step = if (first) handleOrders(prev, orders) …Run Code Online (Sandbox Code Playgroud) 我正在使用Ruby进行SaaS课程.在练习中,我被要求通过使用迭代器,块和产量来计算两个序列的笛卡尔积.
我最终得到了这个,通过纯粹的猜测和错误,它似乎工作.但我不确定如何.我似乎理解基本块和产量用法,但是这个?一点也不.
class CartProd
include Enumerable
def initialize(a,b)
@a = a
@b = b
end
def each
@a.each{|ae|
@b.each{|be|
yield [ae,be]
}
}
end
end
Run Code Online (Sandbox Code Playgroud)
对我这样的菜鸟的一些解释好吗?
(PS:我将所需的课程名称更改为CartProd,因此人们通过Google搜索功能无法轻松找到答案)
我正在打电话GetFiles,我必须返回在所有嵌套目录中找到的文件.我希望能够在返回时找到找到的文件.是否可以以允许收益率回报的方式调用GetFiles?或者是否有必要推出我自己的版本GetFiles使用yield return.
我在想类似的东西 DirectoryInfo("MyDir").GetFiles("*.txt",SearchOptions.All).ForEach(dostuff)
其中dostuff是委托人
Ruby Koans在about_blocks.rb 中有以下练习:
def method_with_block_arguments
yield("Jim")
end
def test_blocks_can_take_arguments
method_with_block_arguments do |argument|
assert_equal __, argument
end
end
Run Code Online (Sandbox Code Playgroud)
我知道答案是assert_equal "Jim", argument,但我很难理解发生了什么。具体来说:
argument还是assert_equal...块?yield做着因为method_with_block_arguments返回“吉姆”没有yield?在阅读了文档、问题并制作了我自己的测试代码后,我相信我已经理解了 ayield expression是如何工作的。
尽管如此,我对以下示例代码的行为感到惊讶:
def gen(n=0):
while True:
n = (yield n) or n+1
g=gen()
print( next(g) )
print( next(g) )
print( g.send(5) )
print( next(g) )
print( next(g) )
Run Code Online (Sandbox Code Playgroud)
我原以为它会返回 0, 1, 2, 5, 6,而它会产生:0, 1, 5, 6, 7。
即:我原以为会yield expression产生这些效果:
yield expression,并将其返回给调用者send()并将它们用作生成器函数代码接收的 yield 表达式的值next(g)或g.send()调用...和/或 Python 会注意避免 (1) 和 (2) 中的两个信息流之间的任何干扰,即保证它们是独立的,例如在元组分配中 a, b = f(a,b), g(a,b)
(我什至想知道在 (1) 和 (2) 之间进行暂停是否更好,但也许它会非常复杂,因为这意味着仅执行部分语句,其余部分保留用于下一份简历)
无论如何,操作的顺序是(2),然后(1),然后(3),因此(2)中的赋值发生在之前,并且可以影响(1)中的赋值。即g.send() …
用户将输入姓名电子邮件和订单信息,包括付款详细信息。单击表单中的立即购买按钮后,我计划执行以下步骤:
前端使用 React-redux-saga。
请在下面的代码:
function* addToCartCamp(action) {
try {
// User creation and login
yield put({ type: authActions.AUTH_REGISTER_REQUEST, ...createUser });
yield put({ type: authActions.AUTH_REGISTER_SUCCESS, ...userdata });
yield put({ type: authActions.AUTH_LOGIN_REQUEST, ...login });
//Create order
const { data } = yield orderAPI.addToCartCamp(action);
yield put({ type: orderActions.ADD_TO_CART_SUCCESS, ...data });
yield put({ type: orderActions.GET_DETAIL_ORDER_REQUEST, ...{orderId: order_id} });
//Handle Payment
if(action.payment.method === 'creditCard'){
yield put({ type: orderActions.TOKEN_REQUEST, ...{orderId: order_id} });
} else{
yield …Run Code Online (Sandbox Code Playgroud) 产量(): https:
//en.cppreference.com/w/cpp/thread/yield notify_one(): http : //www.cplusplus.com/reference/condition_variable/condition_variable/notify_one/
案件:
线程 A 应该完成它正在做的任何事情,然后唤醒线程 B 来完成它的工作。
我在线程 A' run() 函数中编写了一个 notify_one() 调用。
是否有可能线程 A 发出通知通知_one() 但即使线程 B 已准备就绪,线程 A 仍会再次被调度?
notify_one() 和 yield() 是等价的吗?
yield ×10
c# ×3
block ×2
generator ×2
python ×2
ruby ×2
c++ ×1
collections ×1
enumerable ×1
fold ×1
linq ×1
linq-to-xml ×1
optimization ×1
react-redux ×1
reactjs ×1
redux-saga ×1
scala ×1
testing ×1
yield-return ×1