以下Java代码无法编译:
@FunctionalInterface
private interface BiConsumer<A, B> {
void accept(A a, B b);
}
private static void takeBiConsumer(BiConsumer<String, String> bc) { }
public static void main(String[] args) {
takeBiConsumer((String s1, String s2) -> new String("hi")); // OK
takeBiConsumer((String s1, String s2) -> "hi"); // Error
}
Run Code Online (Sandbox Code Playgroud)
编译器报告:
Error:(31, 58) java: incompatible types: bad return type in lambda expression
java.lang.String cannot be converted to void
Run Code Online (Sandbox Code Playgroud)
奇怪的是标记为"OK"的行编译得很好,但标记为"Error"的行失败了.他们看起来基本相同.
我正在使用C#中的一些功能性东西,并继续陷入List.Add
不返回更新列表的事实.
一般来说,我想在一个对象上调用一个函数然后返回更新的对象.
例如,如果C#有一个逗号运算符会很棒:
((accum, data) => accum.Add(data), accum)
Run Code Online (Sandbox Code Playgroud)
我可以像这样编写自己的"逗号运算符":
static T comma(Action a, Func<T> result) {
a();
return result();
}
Run Code Online (Sandbox Code Playgroud)
它看起来会起作用但呼叫网站会很难看.我的第一个例子是:
((accum, data) => comma(accum.Add(data), ()=>accum))
Run Code Online (Sandbox Code Playgroud)
足够的例子!如果没有其他开发人员出现并且在代码味道上皱起鼻子,最干净的方法是什么?
在Scala中,ExecutionContext
如果您不需要通过导入来定义自己的全局,则可以使用全局scala.concurrent.ExecutionContext.Implicits.global
.
我的问题是为什么ForkJoinPool
选择这个执行者而不是ThreadPoolExecutor
.
我的理解是fork-join框架在递归分解问题方面非常出色.你应该编写将任务分成两半的代码,这样一半可以在线程中执行,另一半可以由另一个线程执行.这似乎是一种非常特殊的编程模型,而不是通常适用于处理各种可能应用程序中的常规异步任务执行的模型.
那么为什么ForkJoinPool
选择大多数人可能会使用的默认执行上下文呢?即使您不使用完整的fork-join范例,工作窃取设计是否会提高性能?
我有一个包含许多最终成员的类,可以使用两个构造函数之一进行实例化.构造函数共享一些代码,这些代码存储在第三个构造函数中.
// SubTypeOne and SubTypeTwo both extend SuperType
public class MyClass {
private final SomeType one;
private final SuperType two;
private MyClass(SomeType commonArg) {
one = commonArg;
}
public MyClass(SomeType commonArg, int intIn) {
this(commonArg);
two = new SubTypeOne(intIn);
}
public MyClass(SomeType commonArg, String stringIn) {
this(commonArg);
two = new SubTypeTwo(stringIn);
}
Run Code Online (Sandbox Code Playgroud)
问题是这段代码没有编译:Variable 'two' might not have been initialized.
有人可能从MyClass中调用第一个构造函数,然后新对象没有"两个"字段集.
那么在这种情况下,在构造函数之间共享代码的首选方法是什么?通常我会使用辅助方法,但共享代码必须能够设置最终变量,这只能从构造函数中完成.
我有一个具有一些尺寸长度,宽度,高度的盒子.
我有不同长度,宽度,高度的物品.
是否存在可以确定用于放入盒子的最佳项目的现有算法?
我刚刚发现命名变量的难题很难arguments
.
var arguments = 5;
(function () {
console.log(arguments);
})();
Run Code Online (Sandbox Code Playgroud)
Output: []
事实证明,这arguments
是"所有函数中都可用的局部变量",因此在每个新的执行上下文中arguments
都会被遮蔽.
我的问题是:还有其他任何这样的奸诈名称,例如arguments
,不是真正的保留词,但会导致仍然存在问题吗?
我最近发现可以使用Pimp Enrich My Library模式使用.type
以下方法向伴随对象添加方法:
object Whatever { }
implicit class WhateverExtensions(val obj: Whatever.type) {
def greet = println("Hi!")
}
Whatever.greet
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎不适用于包对象,如scala.math
:
implicit class MathExtensions(val obj: scala.math.type) {
def min(x: Money, y: Money): Money = ???
}
Run Code Online (Sandbox Code Playgroud)
我得到以下编译器错误:
Error:(153, 47) type mismatch; found : math.type required: AnyRef Note that math extends Any, not AnyRef. Such types can participate in value classes, but instances cannot appear in singleton types or in reference comparisons. implicit class MathExtensions(val obj: …
此代码有效:
foreach(DataColumn column in table.Columns)
{
// do whatever
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码没有:
(IEnumerable<DataColumn>)(table.Columns)
Run Code Online (Sandbox Code Playgroud)
该.Columns
属性返回一个DataColumnCollection,它是一个InternalDataCollectionBase,它实现了IEnumerable,因此它应该可以工作.
我得到的错误是
无法将类型'System.Data.DataColumnCollection'转换为'System.Collections.Generic.IEnumerable'
我有一个递归函数,可以写成:
void func(TypeName *dataStructure, LL_Node **accumulator) {
func(datastructure->left, accumulator);
func(datastructure->right, accumulator);
{
char buffer[1000];
// do some stuff
}
return;
}
Run Code Online (Sandbox Code Playgroud)
我知道实际上缓冲区是在函数的开头分配的,并且将语句放在嵌套的范围块中实际上并不使用新的堆栈帧.但是我不希望编译器一次分配一个指数数量的1000字节缓冲区,当它们可以在每个级别返回时分配和丢弃一次.
我应该使用外部全局变量吗?调用辅助函数强制在递归调用后分配缓冲区?我真正想要捕获的是关于强制这种行为的最干净,最常用的C语言方法的建议.
编辑:一个附加问题.如果将完全相同的内容accumulator
传递给每次调用func
,那么将accumulator
指针留在全局变量而不是每次调用时将其推入堆栈都是闻所未闻的吗?
我已经读过如果你声明这样的两个结构:
struct Node {
int a, b, c;
};
struct DerivedNode {
struct Node base;
int d, e, f;
};
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样使用指针:
struct DerivedNode myDerivedNode;
struct Node *regularNode = (struct Node *) &myDerivedNode;
regularNode->a = 3;
Run Code Online (Sandbox Code Playgroud)
换句话说,地址偏移量a, b, c
在struct Node
和中是相同的struct DerivedNode
.所以你可以从中获得一种多态性,在那里你可以传递一个强制(struct Node *)
广播的DerivedNode指针,无论在哪里通常都会采用Node指针.
我的问题是这种行为是否得到保证.我知道有一些奇怪的内存对齐问题,编译器有时会重新排序字段以实现更好的内存打包.base
除了开始之外,这个领域是否会位于任何地方struct DerivedNode
?