我的问题是:鉴于提供的(非常简单的)生成器,是否可以将生成器恢复到其原始状态以便再次使用?
var generator = function*() {
yield 1;
yield 2;
yield 3;
};
var iterable = generator();
for (let x of iterable) {
console.log(x);
}
// At this point, iterable is consumed.
// Is there a method for moving iterable back
// to the start point by only without re-calling generator(),
// (or possibly by re-calling generator(), only by using prototype
// or constructor methods available within the iterable object)
// so the following code would work again?
for (let …Run Code Online (Sandbox Code Playgroud) 可以捕获来自非等待异步调用的错误,发送到原始封装try/catch,还是引发未捕获的异常?
这是我的意思的一个例子:
async function fn1() {
console.log('executing fn1');
}
async function fn2() {
console.log('executing fn2');
throw new Error('from fn2');
}
async function test() {
try {
await fn1();
fn2();
}
catch(e) {
console.log('caught error inside test:', e);
}
}
test();
Run Code Online (Sandbox Code Playgroud)
在这种情况下,抛出的错误fn2将被静默吞噬,并且绝对不会被原始文件捕获try/catch.我相信这是预期的行为,因为fn2很可能被推到事件循环以在将来某个时候完成,并且test在完成时并不关心(这是故意的).
有没有办法确保错误不会被这样的结构意外吞没,而不是放置try/catch内部fn2并做一些像发出错误的事情?我甚至会在不知道如何捕获它的情况下解决一个未被捕获的错误,我认为 - 我不希望抛出的错误是我正在编写的典型程序流程,但是吞下错误会使调试相对烦人.
旁注,我正在使用Babel来使用babel-runtime变换来转换代码,并使用node执行它.
我正在使用一个PHP类,它需要接受多种类型的迭代器并将它们包含在统一的包装器中.我需要支持的一种迭代器(并且可以!)是一个包含yield关键字的匿名函数 - 本质上是一个匿名生成器.
有没有办法在PHP中测试匿名函数是否是生成器?以下是我尝试过的方法列表(旨在显示输出,而不是我如何使用它们):
$anon = function() { yield 1; yield 2; }; // build anonymous generator
gettype($anon); // "object"
$anon instanceof \Traversable; // 0
$anon instanceof \Iterable; // 0
$anon instanceof \IteratorAggregate; // 0
$anon instanceof \Generator; // 0
$anon instanceof \Closure; // 1 -- but doesn't tell me whether or not the closure is actually generator
$anon = $anon(); // invoke, then run the same checks
gettype($anon); // "object"
$anon instanceof \Traversable; // 1 (!!!)
$anon instanceof \Iterable; …Run Code Online (Sandbox Code Playgroud) 我有一个github帐户,该帐户目前可以访问我的开源项目以及一些私有组织。
有没有办法让travis-ci仅访问我的开源项目而不访问组织,或者我最好还是为开放源代码和组织创建一个单独的帐户?
有没有办法确定打开的当前 SQL Server 会话 ID ( @@SPID),而不是DbContext直接对数据库进行 SQL 查询?
如果有,是否有任何保证 SQL Server 会话 ID 将保持不变,直到DbContext被释放并且其连接被释放回实体框架连接池?类似的东西:
using (MyEntities db = new MyEntities()) {
// the following 3 pieces of code are not existing properties and will result in compilation errors
// I'm just looking for something similar to the following 3 lines
db.CurrentSessionId; //error
db.Database.CurrentSessionId; //error
((IObjectContextAdapter)db).ObjectContext.Connection.CurrentSessionId; //error
// the following code will work, but will this session id be the same until the original DbContext …Run Code Online (Sandbox Code Playgroud) 我有一个寻找名字的功能.
我一直在用Javascript 构建一个新的函数式编程库,最近我添加了一个对我有用的新函数.我命名了它,useWith但我想知道它是否是功能程序员以不同名称已知的功能.
该函数与之相关compose,因为它返回一个新函数,它结合了几个现有函数,但方式略有不同compose.它接收的第一个参数被挑选出来; 其余部分均匀处理.当调用返回的函数时,参数将分别传递给其余每个函数,结果以及任何未配对的参数将被发送到第一个函数,然后返回其结果.因此,如果只使用两个函数调用它,并且如果结果函数传递一个参数,则这与compose完全等效.但它有多个参数的附加功能.
我想要这个函数的原因是我实现了类似projectMichal Fogus在Functional Javascript中提供的函数,project类似于Codd的类似Javascript对象的数组,类似于SQL的select动词.写这样很容易:
var project = curry(function(keys, table) {
return map(pick(keys), table);
});
// Used like this:
var kids = [{name: 'Bob', age: 3, eyes: 'blue', hair: 'brown'},
{name: 'Sue', age: 5, eyes: 'hazel', hair: 'blonde'}];
project(['name', 'eyes'], kids);
//=> [{name: 'Bob', eyes: 'blue'}, {name: 'Sue', eyes: 'hazel'}]
Run Code Online (Sandbox Code Playgroud)
但我真的想以无点的方式实现它.但当然这不起作用:
var project = compose(map, pick); // NO!
Run Code Online (Sandbox Code Playgroud)
...因为没有设施通过第二个参数, …
javascript ×3
generator ×2
async-await ×1
c# ×1
closures ×1
ecmascript-6 ×1
ecmascript-7 ×1
github ×1
php ×1
promise ×1
sql ×1
sql-server ×1
travis-ci ×1
yield ×1