小编Leu*_*nko的帖子

VIM - 如何为键分配不同的映射,具体取决于行是否为空?

我想根据上下文分配不同的命令.

我正在尝试做这样的事情:

if line is empty:
    map x key to y command
else
    map x key to z command
Run Code Online (Sandbox Code Playgroud)

vim keymapping macvim

2
推荐指数
2
解决办法
130
查看次数

传递一个带有类型参数的函数作为参数

我想创建一个函数g,它将函数f作为参数,其中f有一个类型参数.我无法获得g该编辑的签名.一次尝试是这样的:

scala> def mock1[A](): A = null.asInstanceOf[A] // STUB
mock1: [A]()A

scala> def mock2[A](): A = null.asInstanceOf[A] // STUB
mock2: [A]()A

scala> def g0(f: [A]() => A): Int = f[Int]()
<console>:1: error: identifier expected but '[' found.
       def g0(f: [A]() => A): Int = f[Int]()
                 ^
Run Code Online (Sandbox Code Playgroud)

如果我将包含类型参数的函数包装在特征中,我可以使它工作,如下所示:

scala> trait FWrapper { def f[A](): A }
defined trait FWrapper

scala> class mock1wrapper extends FWrapper { def f[A]() = mock1[A]() }
defined class mock1wrapper …
Run Code Online (Sandbox Code Playgroud)

scala type-parameter

2
推荐指数
1
解决办法
2224
查看次数

Java Pass实例类型的Object为泛型类类型参数

是否可以将Objects实例类型作为泛型的类型参数传递?类似于以下内容:

Object obj = new Double(3.14); //Instance type Double

//Could I do the following?
Item<obj.getInstanceType()> item = new Item<obj.getInstanceType()>(obj);

public class Item<T> {
    private T item;
    public Item(T item) {
        this.item = item
    }
    public T getItem() {
        return this.item;
    }
}
Run Code Online (Sandbox Code Playgroud)

java generics object type-parameter instancetype

2
推荐指数
1
解决办法
3140
查看次数

了解Haskell懒惰评估

原谅我的愚蠢问题,我是Haskell的新手.

我在Haskell中尝试了以下内容:

sum [fib n| n <- [1..], (even (fib n) && fib n < 4000000)] 
Run Code Online (Sandbox Code Playgroud)

这需要无限的时间.如果我遗漏n <- [1..],解决方案立即出现.

我认为这无关紧要,因为Haskell正在评估懒惰.我是否误解了懒惰的评价?

haskell fibonacci lazy-evaluation

2
推荐指数
2
解决办法
129
查看次数

字符串操作(剪切字符串的头部)

为什么我不能得到"xxx"?返回值是一些非常奇怪的符号...我希望返回的值是xxx,但我不知道这个程序有什么问题.该功能运行良好,可以为我打印"xxx",但一旦它返回值到main函数,字符串结果就不能很好地显示"xxx".有人可以告诉我原因吗?

char* cut_command_head(char *my_command, char *character) {
    char command_arg[256];
    //command_arg = (char *)calloc(256, sizeof(char));
    char *special_position;
    int len;
    special_position = strstr(my_command, character);
    //printf("special_position: %s\n", special_position);
    for (int i=1; special_position[i] != '\0'; i++) {
        //printf("spcial_position[%d]: %c\n", i, special_position[i]);
        command_arg[i-1] = special_position[i];
       //printf("command_arg[%d]: %c\n", i-1, command_arg[i-1]);
    }
    len = (int)strlen(command_arg);
    //printf("command_arg len: %d\n", len);
    command_arg[len] = '\0';
    my_command = command_arg;
    printf("my_command: %s\n", my_command);
    return my_command;
}

int main(int argc, const char * argv[]) {
    char *test = "cd xxx";
    char *outcome;
    outcome …
Run Code Online (Sandbox Code Playgroud)

c

2
推荐指数
1
解决办法
108
查看次数