(这个问题并不仅限于语言,所以请随意提交其他语言的解决方案.)
我只是想知道是否可以在JavaScript中编写类似的内容:
// Wait 3 seconds and then say our message in an alert box
wait(3).then(function(){alert("Hello World!");});
Run Code Online (Sandbox Code Playgroud)
传统的方式是写作
// Wait 3 seconds and then say our message in an alert box
setTimeout(function(){alert("Hello World!");}, 3000);
Run Code Online (Sandbox Code Playgroud)
对不起,如果这是一个菜鸟问题:p
我想了解更多有关Javascript中的方法链接的信息,并想知道在没有jQuery的情况下为链中的下一个函数创建延迟的正确方法:
var foo = function() {
this.delay = function(per) {
setTimeout(start, per);
return this;
};
this.start = function() {
alert('start!');
};
};
var bar = new foo().delay(1000).start();
Run Code Online (Sandbox Code Playgroud) 见下面的代码:
new ConditionCreator()
.Add()
.Or()
.Add()
.And()
.Add()
Run Code Online (Sandbox Code Playgroud)
我想为此创建一个Fluent接口但是我需要,在Add()方法开发人员看到Only Or()或And()之后,在其中一个之后,请参阅Only Add()方法.
所以没有人可以编写如下代码:
new ConditionCreator()
.Add()
.Add()
.Add()
.Or()
.And()
.Add()
.And()
.And()
Run Code Online (Sandbox Code Playgroud)
我希望有一些限制,一些方法可以接受特殊的方法等.我可以在一个类中编写所有方法,并为每一个返回这个,但这是不合适的!
请指导我如何编写Advanced Fluent Interface类.
我指的是测试断言库:http://chaijs.com/api/bdd/#false
您可以编写如下语言链断言:
expect(false).to.be.false;
Run Code Online (Sandbox Code Playgroud)
expect()显然是一个全局函数,"to.be"看起来像两个属性,但最后一部分"false"是如何工作的.我期待它必须是一个函数调用:
expect(false).to.be.false();
Run Code Online (Sandbox Code Playgroud)
这是2015 ES语法吗?我似乎无法在https://github.com/lukehoban/es6features中找到它的引用
Stack Overflow表示不可能:如何在函数调用期间实现可选括号?(函数重载)
任何人都可以了解这样的事情是如何实现的?
源代码:https://github.com/chaijs/chai/blob/master/lib/chai/core/assertions.js#L281
首先,我不是在谈论c ++ 11构造函数链接也就是构造函数委托.
类成员函数可以返回对自身(类)的引用,因此可以链接函数调用.(例如cout <<运算符如何工作以允许链调用.)
在实例化匿名对象时,可以在构造函数之外进行此类链调用.
链调用是否可以从命名对象的构造函数中进行?下面的"foo a"和"foo b"的行不能编译,所以我想知道是否有不同的语法.
#include <iostream>
using namespace std;
class foo {
public:
foo(int x) : val{x} { };
foo& inc() { ++val; return *this; }
int getVal() { return val; };
private:
int val;
};
int main() {
cout << foo(1).inc().getVal() << endl; // prints 2
cout << foo{2}.inc().inc().inc().inc().getVal() << endl; // prints 6
foo a(3).inc(); // error: expected ‘,’ or ‘;’ before ‘.’ token
foo b{4}.inc(); // error: expected ‘,’ or ‘;’ before …Run Code Online (Sandbox Code Playgroud) 我正在尝试围绕C#编译器在链接linq方法时所做的事情,特别是在多次链接同一方法时.
简单的例子:假设我试图根据两个条件过滤一系列int.
最明显的事情是这样的:
IEnumerable<int> Method1(IEnumerable<int> input)
{
return input.Where(i => i % 3 == 0 && i % 5 == 0);
}
Run Code Online (Sandbox Code Playgroud)
但我们也可以链接where方法,每个方法都有一个条件:
IEnumerable<int> Method2(IEnumerable<int> input)
{
return input.Where(i => i % 3 == 0).Where(i => i % 5 == 0);
}
Run Code Online (Sandbox Code Playgroud)
我看了反射器中的IL; 这两种方法明显不同,但目前我不知道进一步分析它:)
我想找出:
a)编译器在每个实例中做什么不同,以及为什么.
b)是否有任何性能影响(不是试图微观优化;只是好奇!)
我发现了一种在Java中调用多个方法的新方法,我并不真正了解背后发生的事情:
public class NutritionFacts {
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
public static class Builder {
// Required parameters
private final int servingSize;
private final int servings;
// Optional parameters - initialized to default values
private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 0;
public Builder(int servingSize, int servings) {
this.servingSize …Run Code Online (Sandbox Code Playgroud) Python中是否有一个空传播运算符(“空感知成员访问”运算符),所以我可以写一些类似的东西
var = object?.children?.grandchildren?.property
Run Code Online (Sandbox Code Playgroud)
就像在 C#、VB.NET 和 TypeScript 中一样,而不是
var = None if not myobject\
or not myobject.children\
or not myobject.children.grandchildren\
else myobject.children.grandchildren.property
Run Code Online (Sandbox Code Playgroud) python null-coalescing-operator null-coalescing method-chaining
我认为我在方法链接方面缺少一些东西。对我来说感觉不完整。
方法链的工作原理是让每个方法返回this,以便可以调用该对象上的另一个方法。然而,返回值是this函数的结果而不是函数的结果这一事实对我来说似乎很不方便。
这是一个简单的例子。
const Obj = {
result: 0,
addNumber: function (a, b) {
this.result = a + b;
return this;
},
multiplyNumber: function (a) {
this.result = this.result * a;
return this;
},
}
const operation = Obj.addNumber(10, 20).multiplyNumber(10).result
console.log(operation)
Run Code Online (Sandbox Code Playgroud)
关键点:
Obj.addNumber(10, 20).multiplyNumber(10)都会返回this。.result是返回 以外的值的部分this。这种方法的问题在于,它需要您附加一个属性/方法才能在末尾获取除 之外的值this。
将其与 JavaScript 中的内置函数进行比较。
const str = " SomE RandoM StRIng "
console.log(str.toUpperCase()) // " SOME RANDOM STRING " …Run Code Online (Sandbox Code Playgroud) 在 Pandas 中转换列的最流畅(或易于阅读)的方法链接解决方案是什么?
\n(\xe2\x80\x9cmethod chaining\xe2\x80\x9d 或 \xe2\x80\x9c Fluent\xe2\x80\x9d 是Tom Augspurger 等人流行的编码风格。)
\n为了示例,让我们设置一些示例数据:
\nimport pandas as pd\nimport seaborn as sns\n\ndf = sns.load_dataset("iris").astype(str) # Just for this example\ndf.loc[1, :] = "NA"\n\ndf.head()\n# \n# sepal_length sepal_width petal_length petal_width species\n# 0 5.1 3.5 1.4 0.2 setosa\n# 1 NA NA NA NA NA\n# 2 4.7 3.2 1.3 0.2 setosa\n# 3 4.6 3.1 1.5 0.2 setosa\n# 4 5.0 3.6 1.4 0.2 setosa\nRun Code Online (Sandbox Code Playgroud)\n仅举此示例:我想通过函数(sepal_length使用pd.to_numeric)映射某些列,同时保持其他列不变。以方法链接方式实现这一点的最简单方法是什么?
我已经可以使用分配,但我在这里重复列名,这是我不想要的。 …