我的意思是,例如,
f :: (Enum a) => a -> a --without this line, there would be an error
f = succ
Run Code Online (Sandbox Code Playgroud)
这是因为succ需要它的参数是可枚举的(succ :: (Enum a) => a -> a)
但对于 (+)
f = (+) --ok
Run Code Online (Sandbox Code Playgroud)
虽然(+)声明是(+) :: (Num a) => a –> a –> a.
我的意思是,我为什么不需要申报f的f :: (Num a) => a –> a –> a?
template<typename T>
struct foo{
void f(){
decltype(*this) a(*this);
do_some_test(a);
}
T data;
};
//compiler won't accept this
Run Code Online (Sandbox Code Playgroud)
在我的解释中,decltype应该返回一个类型,以便我们可以在声明中使用它.但谷歌表示,在decltype(x),如果x是一个左值,它将返回T&这里T是x的类型.
但他们设计的是什么来返回参考?此外,我应该怎么做才能创建与*this模板中的类型相同的类的实例?
似乎Haskell的IO相对较慢.
例如,将Haskell与Python进行比较
#io.py
import sys
s=sys.stdin.read()
sys.stdout.write(s)
Run Code Online (Sandbox Code Playgroud)
,
-- io.hs
main = do
s <- getContents
putStr s
Run Code Online (Sandbox Code Playgroud)
他们的表现(gen.py将512k数据写入stdout):
Python版本:
$ time python gen.py | python io.py > /dev/null
real 0m0.203s
user 0m0.015s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)
Haskell版本:
$ time python gen.py | runhaskell io.hs > /dev/null
real 0m0.562s
user 0m0.015s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)
似乎Haskell的价格要低得多.我的测试有问题吗?或者它只是Haskell的固有问题?
谢谢.
基本上我想做的是
class Parent{
public class Nested {
private Nested(){
//do something
}
/* ??? */ Nested CreateNested(){
return new Nested ();
}
}
public Nested Foo(){
Nested n = (???).CreateNested ();
// do something about n
return n;
}
}
Run Code Online (Sandbox Code Playgroud)
以便该类的用户Parent可以看到该类Nested,但无法创建它(但是他们可以从 获取它Parent)。我知道对于普通方法,您可以使用显式接口实现来完成,但它似乎不适用于构造函数。
例如,以下代码将不被接受
void foo(auto i){
cout<<(i+1);
}
Run Code Online (Sandbox Code Playgroud)
我认为它应该等同于以下公认的代码
template<typename T>
void foo(T i){
cout<<(i+1);
}
Run Code Online (Sandbox Code Playgroud)
因此编译器应该能够推导(或实例化)参数的类型.但为什么它不起作用?
多谢 :-)
我试图实现这个直接的Maybemonad.因此,基本上整个表达式的评估结果Nothing是中间步骤之一Nothing.
type Maybe<'a> =
| Just of 'a
| Nothing
type MaybeBuilder () =
member this.Combine ((first, second) : Maybe<'a> * Maybe<'b>) : Maybe<'b> =
printfn "Combine called"
match first with
| Nothing -> Nothing
| _ ->
match second with
| Nothing -> Nothing
| _ as a -> a
member this.Zero () = Just ()
member this.Bind((m, f) : Maybe<'a> * ('a -> Maybe<'b>)) =
printfn "Bind called"
match m with
| …Run Code Online (Sandbox Code Playgroud) 在下面的片段中(我已经抽象了所有其他琐碎的部分)
data T s = T (s -> s)
foo :: T s -> s -> s
foo (T f) x = bar x where
bar :: s -> s
bar a = f a
Run Code Online (Sandbox Code Playgroud)
我收到了以下错误
Couldn't match expected type `s1' with actual type `s'
`s1' is a rigid type variable bound by
the type signature for bar :: s1 -> s1 at /tmp/test.hs:5:12
`s' is a rigid type variable bound by
the type signature for foo :: T s …Run Code Online (Sandbox Code Playgroud) 我正在Expression使用自定义代码生成器编写自定义 Spark 催化剂,但似乎 Spark (3.0.0) 不想使用生成的代码,并回退到解释模式。
我以非常标准的方式创建我的 SparkSession,除了我尝试强制代码生成:
val spark = SparkSession.builder()
.appName("test-spark")
.master("local[5]")
.config("spark.sql.codegen.factoryMode", "CODEGEN_ONLY")
.config("spark.sql.codegen.fallback", "false")
.getOrCreate()
Run Code Online (Sandbox Code Playgroud)
然后我定义Expression了解释模式和代码生成器的这个自定义:
case class IsTrimmedExpr(child: Expression) extends UnaryExpression with ExpectsInputTypes {
override def inputTypes: Seq[DataType] = Seq(StringType)
override lazy val dataType: DataType = BooleanType
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
throw new RuntimeException("expected code gen")
nullSafeCodeGen(ctx, ev, input => s"($input.trim().equals($input))")
}
override protected def nullSafeEval(input: Any): Any = {
throw new RuntimeException("should not eval") …Run Code Online (Sandbox Code Playgroud) 在这条线上
type SafeReturn a = Exception e => Either e a
Run Code Online (Sandbox Code Playgroud)
我收到了这个警告
Variable ‘e’ is implicitly quantified due to a context
Use explicit forall syntax instead.
Run Code Online (Sandbox Code Playgroud)
这是什么意思?