我正在玩Haskell和Project Euler的第23个问题.用列表解决后,我去了这里看了一些数组工作.这个解决方案比我的快得多.所以这就是问题所在.我什么时候应该在Haskell中使用数组?他们的表现比名单更好吗?在哪些情况下?
我试图获得像Real World Haskell建议的文件大小:
getFileSize :: FilePath -> IO (Maybe Integer)
getFileSize path = handle (\_ -> return Nothing)
$ bracket (openFile path ReadMode) (hClose) (\h -> do size <- hFileSize h
return $ Just size)
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
Ambiguous type variable `e0' in the constraint:
(GHC.Exception.Exception e0) arising from a use of `handle'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: handle (\ _ -> return Nothing)
In the expression:
handle (\ _ -> return Nothing) …Run Code Online (Sandbox Code Playgroud) 我是C#开发人员,我现在正在学习Haskell.我想在Haskell中重写我的一个项目只是为了好玩.在这个项目中,我有一些与MySQL有关的工作,所以我想问一下:在Haskell中使用MySQL的最佳工具是什么.另外看一些代码如何使用它会很棒(我有Windows 7).
我正在使用Rust的并发性并尝试绕过Send/ Sync/ Arc/ Mutex.我在共享对特征实例的引用时遇到问题,该实例存在于HashMap:
use std::{collections::HashMap, sync::Arc, thread, time::Duration};
#[derive(Debug)]
struct A {
foo: u8,
}
trait Foo {
fn get_foo(&self) -> u8;
}
impl Foo for A {
fn get_foo(&self) -> u8 {
self.foo
}
}
fn main() {
let a = Arc::new(A { foo: 8 });
let mut map: HashMap<u8, Arc<Foo>> = HashMap::new();
map.insert(8u8, a);
for _ in 0..2 {
let a = map.get(&8u8).expect("boom");
let a = a.clone();
thread::spawn(move || { …Run Code Online (Sandbox Code Playgroud) 试图在我的新scala项目中添加"import"语句(我现在正在使用IntelliJ 10.5.2)语法高亮显示器告诉我他找不到java包:
import java.util.zip //Cannot resolve symbol java
object Main extends App {
override def main(args: Array[String]) {
}
}
Run Code Online (Sandbox Code Playgroud)
怎么了.我该怎么做才能导入这个包?
可能重复:
Java泛型约束需要像C#这样的默认构造函数
我想设置一个约束类型T,它必须有一个没有参数的构造函数.在C#中它将是:
public interface Interface<T> where T : new() { }
这个功能是否在Java中可用?
我正在玩Rust和tiny-http.我创建了一个函数,其中我正在弄乱请求的标题然后发送响应:
fn handle_request(req: Request) {
let headers = req.headers();
// working with headers
let res = Response::from_string("hi");
req.respond(res);
}
Run Code Online (Sandbox Code Playgroud)
它失败并出现错误:
main.rs:41:5: 41:8 error: cannot move out of `req` because it is borrowed
main.rs:41 req.respond(res);
^~~
main.rs:27:19: 27:22 note: borrow of `req` occurs here
main.rs:27 let headers = req.headers();
^~~
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
所以我有点理解,req.headers()接受&self哪个进行借用req和req.respond()"移动",req因为它接受self.我不确定我应该在这做什么,有人可以帮我理解吗?
我正在尝试使用HashMap<String, &Trait>但我有一个我不明白的错误信息.这是代码(围栏):
use std::collections::HashMap;
trait Trait {}
struct Struct;
impl Trait for Struct {}
fn main() {
let mut map: HashMap<String, &Trait> = HashMap::new();
let s = Struct;
map.insert("key".to_string(), &s);
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
error[E0597]: `s` does not live long enough
--> src/main.rs:12:36
|
12 | map.insert("key".to_string(), &s);
| ^ borrowed value does not live long enough
13 | }
| - `s` dropped here while still borrowed
|
= note: values in a scope are dropped in …Run Code Online (Sandbox Code Playgroud) 为什么我不能这样做:
do_once = Proc.new { yield }
do_once.call { puts 1 }
irb抛出 LocalJumpError: no block given (yield)
我正在尝试解析具有类似于此格式的特定字符串:
prefix,body1:body2
Run Code Online (Sandbox Code Playgroud)
我想使用像这样的.chars方法和其他方法.take_while:
let chars = str.chars();
let prefix: String = chars.take_while(|&c| c != ',').collect();
let body1: String = chars.take_while(|&c| c != ':').collect();
let body2: String = chars.take_while(|_| true).collect();
Run Code Online (Sandbox Code Playgroud)
(游乐场)
但是编译器抱怨:
error: use of moved value: `chars` [E0382]
let body1: String = chars.take_while(|&c| c != ':').collect();
^~~~~
help: see the detailed explanation for E0382
note: `chars` moved here because it has type `core::str::Chars<'_>`, which is non-copyable
let prefix: String = chars.take_while(|&c| c != ',').collect(); …Run Code Online (Sandbox Code Playgroud)