函数参数解构是ES6中的一个惊人特性.假设我们希望一个functionnamed f接受一个Object有a密钥的
function f({ a }) {
return a;
}
Run Code Online (Sandbox Code Playgroud)
对于未提供参数的情况,我们有默认值以避免 Type Error
function f({ a } = {}) {
return a;
}
Run Code Online (Sandbox Code Playgroud)
这将有助于以下情况
const a = f(); // undefined
Run Code Online (Sandbox Code Playgroud)
虽然,它会失败
const a = f(null); // TypeError: Cannot match against 'undefined' or 'null'.
Run Code Online (Sandbox Code Playgroud)
您可以在这里看到Babel如何将功能转换为ES5 .
通过参数验证和预处理可以避免这种情况.在Python我可以使用装饰器,但在JS中我们没有它们标准化,所以使用它们不是一个好主意.但是,假设我们有一个装饰器checkInputObject,它使用给定的项目列表(或嵌套解构的情况下的树)进行必要的检查并提供默认值.我们可以在没有@表示法的情况下以下列方式使用它
const f = checkInputObject(['a'])(({ a }) => a);
Run Code Online (Sandbox Code Playgroud)
它可能看起来像带@符号
@checkInputObject(['a'])
function f({ a }) { …Run Code Online (Sandbox Code Playgroud) 我正在练习 C++ 并尝试实现不可变列表。在我的一项测试中,我尝试递归创建一个包含大量值(100 万个节点)的列表。所有值都是const,所以我无法执行常规循环,而且这也不够功能,你知道。测试失败并显示Segmentation fault。
我的系统是 64 位 Xubuntu 16.04 LTS,Linux 4.4。我使用标志使用 g++ 5.4 和 clang++ 3.8 编译我的代码--std=c++14 -O3。
我写了一个简单的例子,它展示了尾部调用应该很容易优化,但出现问题时的情况Segmentation fault。该函数f只是等待amount迭代,然后创建一个指向 single 的指针int并返回它
#include <memory>
using std::shared_ptr;
shared_ptr<int> f(unsigned amount) {
return amount? f(amount - 1) : shared_ptr<int>{new int};
}
int main() {
return f(1E6) != nullptr;
}
Run Code Online (Sandbox Code Playgroud)
请注意,此示例仅在出现 时失败g++,而则clang++正常。不过,在更复杂的示例中,它也没有优化。
这是一个带有递归插入元素的简单列表的示例。我还添加了destroy函数,这有助于避免销毁期间堆栈溢出。在这里我得到了Segmentation fault两个编译器
#include …Run Code Online (Sandbox Code Playgroud) c++ recursion tail-recursion shared-ptr compiler-optimization
我有这样的代码
if condition:
a = f(x)
else:
a = g(y)
Run Code Online (Sandbox Code Playgroud)
a块内部的初始化对我来说很糟糕.可以写得更好吗?
我不能使用三元运算符,因为函数和/或参数列表的名称很长.说"长"我的意思是以下表达式
a = f(x) if condition else g(y)
Run Code Online (Sandbox Code Playgroud)
需要超过79(有时甚至大于119)的符号使用实名的代替a,f,g,x,y和condition.使用多个斜杠会使代码变得丑陋和混乱.
我不想a用默认函数初始化其中一个函数的结果,因为这两个函数都很慢,我不能允许这样的开销
a = g(y)
if condition:
a = f(x)
Run Code Online (Sandbox Code Playgroud)
我可以初始化变量None,但这个解决方案是否已足够?
a = None
if condition:
a = f(x)
else:
a = g(y)
Run Code Online (Sandbox Code Playgroud)
让我解释一下我的立场:在C和C++中,块内部的变量都以块作为其范围.在ES6 let中引入了关键字 - 它允许使用与C和C++中的变量相同的范围规则创建变量.使用old var关键字定义的变量具有与Python类似的范围规则.这就是为什么我认为如果我想在这些块之外使用变量,那么变量的初始化应该在块外部进行.
这是一个更复杂的例子
for obj in gen:
# do something with the `obj`
if …Run Code Online (Sandbox Code Playgroud) 我有一个代码
module Main where
import Text.Printf
main = printf "%s%s" ("Content-type: text/html; charset=utf-8\n\n" :: String ) ("And ??????? ????????" :: String)
Run Code Online (Sandbox Code Playgroud)
如果我在终端执行它,我有我想要的:
Content-type: text/html; charset=utf-8
And ??????? ????????
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将其作为cgi-program执行时,我只是And(因为在这个词之后我有西里尔字符并且他们不想显示).
有没有问题,当我使用putStr $ fromString的Data.ByteString.Char8和Data.ByteString.UTF8,所以我不认为有一个与我LAMPP服务器有问题.我也包含AddDefaultCharset utf-8在httpd.conf中.
而我想要做的就是读取带有特殊符号的模板文件%s,%d等等,然后在帮助下printf,根据查询字符串和显示,将它们(符号)替换为我需要的符号.
我这样做只是为了好玩,我希望这个问题只用纯Haskell解决.
我用OpenGL 3和PyOpenGL。
我有大约 50,000 (53'490) 个顶点,每个顶点都有 199 个vec3属性来确定它们的位移。不可能将此数据存储为常规顶点属性,因此我使用纹理。
问题是:非并行化C函数计算顶点位移的速度与计算顶点位移的速度一样快GLSL,在某些情况下甚至更快。我已经检查过:问题是纹理读取,我不明白如何优化它。
我写了两个不同的着色器。一种在约 0.09 秒内计算出新模型,另一种在约 0.12 秒内计算出新模型(包括属性分配,这两种情况都是相同的)。
两个着色器都以
#version 300 es
in vec3 vin_position;
out vec4 vin_pos;
uniform mat4 rotation_matrix;
uniform float coefficients[199];
uniform sampler2D principal_components;
Run Code Online (Sandbox Code Playgroud)
速度较快的是
void main(void) {
int c_pos = gl_VertexID;
int texture_size = 8192;
ivec2 texPos = ivec2(c_pos % texture_size, c_pos / texture_size);
vec4 tmp = vec4(0.0);
for (int i = 0; i < 199; i++) { …Run Code Online (Sandbox Code Playgroud) 我有一个linked_list,目前我的析构函数不能正常工作.不完全确定原因.有人可以解释一下如何解决这个问题吗?
class linked_list {
private:
struct node
{
// String in this node
std::string data;
// Pointer to next node
struct node *next;
};
//First item in the list
struct node *first;
Run Code Online (Sandbox Code Playgroud)
这是我的析构函数
linked_list::~linked_list(void)
{
while (first)
{
delete first;
first = first->next;
}
}
Run Code Online (Sandbox Code Playgroud) c++ ×2
cgi ×1
destructor ×1
ecmascript-6 ×1
glsl ×1
haskell ×1
javascript ×1
linked-list ×1
opengl ×1
performance ×1
printf ×1
python ×1
recursion ×1
scope ×1
shared-ptr ×1
textures ×1
utf-8 ×1
variables ×1