ruby中有没有快捷方式(syntaxtic sugar)功能吗?
# x[5] += 3 that zeroes first if x[5] does not exists
x = {}
x.key? 5 ? x[5] = 3 : x[5] += 3
Run Code Online (Sandbox Code Playgroud) 我注意到在我的一个课程中,我有以下内容:
public void foo() {
int i = 4;
i = 9 + 0;;
return;
}
Run Code Online (Sandbox Code Playgroud)
两个;;连续.为什么这不是错误?应该是吗?根据我的理解,它只是编译器读取的"额外代码"代码.这会浪费性能吗?另外,如果它真的是空的,为什么我不能;;在返回后有两个?(如return;;)?
编辑:在实践中有什么用?
在C#中,如果委托定义如下:
delegate void A();
A a = () => {Console.WriteLine("Test")};
Run Code Online (Sandbox Code Playgroud)
它可以调用:
a.Invoke(); // Works
a(); // Also works
Run Code Online (Sandbox Code Playgroud)
在F#中,可以调用C#委托:
a.Invoke() // Works fine.
a() // Error: This value is not a function and cannot be applied
Run Code Online (Sandbox Code Playgroud)
为什么我不能在F#中使用第二种方法?在C#中,()运算符是否只是映射到Invoke方法,或者是否在其下发生了其他事情.F#中有更清晰的语法吗?
我想知道如果没有任何语法糖,以下函数会是什么样子:
tails1 :: [a] -> [[a]]
tails1 [] = [[]]
tails1 xs@(x:xs') = xs:tails1 xs'
Run Code Online (Sandbox Code Playgroud)
我最关心的是 @ 运算符的用法,我已经尝试了以下操作,但这显然不是正确的方法
tails1 ((:) x ((:) xs' [])) = xs:tails1 xs'
Run Code Online (Sandbox Code Playgroud) 在Scheme中,如果我想要一个列表,请说(1 2 3),我会写'(1 2 3).通常情况下,这很好,但它实际上相当于(quote (1 2 3)),与之不完全相同(list 1 2 3).这会产生不同结果的一个例子:
'(1 2 (+ 0 3)) -> (1 2 (+ 0 3))
(list 1 2 (+ 0 3)) -> (1 2 3)
Run Code Online (Sandbox Code Playgroud)
第二行是否有语法糖?对于矢量有.例如:
#(1 2 (+ 0 3)) -> #(1 2 3)
(vector 1 2 (+ 0 3)) -> #(1 2 3)
Run Code Online (Sandbox Code Playgroud)
如果列表中没有这样的糖,那将是非常具有讽刺意味的,因为列表的使用方式比Scheme中的向量更常用!
我正在尝试创建自己的类,其行为类似于常规类型,如下所示:
class CustomType:
def __init__(self, content):
self.content = content
def __str__(self):
return self.content
Run Code Online (Sandbox Code Playgroud)
这意味着我可以这样做:
a = CustomType("hi there")
print(a) # 'hi there'
Run Code Online (Sandbox Code Playgroud)
但问题是我必须使用a = CustomType("hi there").有没有办法可以做类似的a = /hi there\或类似的解决方案,让我创建自己的语法糖?
谢谢.
如在 java8 中:
someList.stream().map(e->e.getXXX()).toList()
Run Code Online (Sandbox Code Playgroud)
例如,我有一个Student数组/切片,该结构Student包含诸如Id、 等属性Name。
我想将所有 Id 提取到一个新的数组/切片中,其中包含像上面提到的 java8 这样的单行代码,而不是range. 有例子吗?
我遇到一种情况,我需要传递可变数量的整数(在编译时已知)。
我需要实现这样的目标:
template <unsigned N>
void myFunc(std::array<int, N> values)
{
// Do something useful with the values
}
Run Code Online (Sandbox Code Playgroud)
我希望上面的代码能够工作,并且可以像这样方便地调用,而无需创建临时对象;
myFunc<4>({1, 2, 3, 4});
这实际上确实有效,但问题是这也有效......
myFunc<4>({1, 2});
这当然会使其中两个值未初始化,而我和用户都不知道。
我相信这会起作用:
template <unsigned N>
void myFunc(std::vector<int> values)
{
assert(values.size() == N);
// Do something useful with the values
}
Run Code Online (Sandbox Code Playgroud)
myFunc<4>({1, 2}); // Goes bang
myFunc<4>({1, 2, 3, 4}); // OK
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法在我的环境中分配内存),所以我必须在堆栈上执行所有操作(并且不能只接受我可以检查其大小的向量)。
我认为自己有两个选择。
作为学习PHP的一部分,我试图将其语义形式化.简化此任务的一个好方法是尽可能多地理解其语法作为更多基本表达式的"语法糖".这样,我可以用更少的语法形式来定义语义.
PHP似乎有大量可以去糖的语法.一些例子:
if/endif和其他"替代语法"desugar if { ... }等
for (expr1; expr2; expr3) { statements; }des to to to expr1; while (expr2) { statements; expr3; }.
我认为以下是真的:$foo只是意味着${'foo'}.也就是说,通过名称引用变量只是通过将表达式计算为字符串而更通用的变量查找的实例.
我认为这:
<p>foo</p>
<?php echo 5; ?>
<p>bar</p>
Run Code Online (Sandbox Code Playgroud)
对此的厌恶:
echo "<p>foo</p>\n";
echo 5;
echo "<p>bar</p>\n";
Run Code Online (Sandbox Code Playgroud)
或者一般来说,?>blah<?php只是意味着echo 'blah';.
当然还有很多很多这样的案例.我想提出一份全面且确认准确的清单.(我的任何一个例子都可能不准确,所以请纠正我!)
我在C#6.0中有2个代码:
样本1:
var foo = "Some text, " +
$"some other {bar}, " +
"end text.";
Run Code Online (Sandbox Code Playgroud)
样本2:
var foo = $"Some text, " +
$"some other {bar}, " +
$"end text.";
Run Code Online (Sandbox Code Playgroud)
显然两个代码都会产生相同的结果,但第二个代码在美学上更加美观(这是我的意见,请记住每个代码都有你的).
问题:
如果不需要$,是否有问题(性能或其他)写一个前缀为$的字符串.
编辑: 回答一些投诉:
第二个是可怕的.不要对未插值的字符串使用字符串插值符号($).任何看过代码的人(或未来,更有经验的人)都会感到困惑
这完全是您的看法,我在这里有一个团队,有10位高级C#开发人员,都同意第二个更清晰.
@ChetanRanpariya这不是真的.插值字符串是用于调用string.Format的语法糖,必须调用它(从而影响性能).
你需要提供证明你在说什么.
顺便说一下:这样的标准typedef有助于减少C/C++ 中保留字的数量,并将void降级为简单 类型声明,例如在<stddef.h>中.(CF SRB在AB33/1972年3月 - PDF = 7KB)