我正在观看Control.Lens介绍视频.
这让我想知道为什么Setter类型需要在仿函数中包装它.
它(大致)定义如下:
type Control.Lens.Setter s t a b = (Functor f) => (a -> f a) -> s -> f t
Run Code Online (Sandbox Code Playgroud)
假设我有一个名为Pointthis的定义数据:
data Point = Point { _x :: Int, _y :: Int } deriving Show
Run Code Online (Sandbox Code Playgroud)
然后我可以xlens像这样写自己的:
type MySetter s t a b = (a -> b) -> s -> t
xlens :: MySetter Point Point Int Int
xlens f p = p { _x = f (_x p) …Run Code Online (Sandbox Code Playgroud) 在 C 中,我可以使用索引以嵌套方式可变地迭代数组。在 Rust 中,我几乎可以使用索引做同样的事情,但是如果我想使用迭代器而不是索引怎么办?
例如,以下代码段可以成功编译,因为两个借用都是不可变的:
let xs = [0, 1, 2];
for x in &xs {
for y in &xs {
println!("x={} y={}", *x, *y);
}
}
Run Code Online (Sandbox Code Playgroud)
但是如果我想使用可变迭代器怎么办?
let mut xs = [0, 1, 2];
for x in &mut xs {
*x += 1;
for y in &mut xs {
*y += 1;
println!("x={} y={}", *x, *y);
}
}
Run Code Online (Sandbox Code Playgroud)
这导致:
error[E0499]: cannot borrow `xs` as mutable more than once at a time
Run Code Online (Sandbox Code Playgroud)
我理解需要引导对数据的写入访问,但我也想知道经验丰富的 Rust 用户如何仅使用迭代器来实现这一目标——假设索引仅用于教育目的。
我使用Qt,我需要在主线程中执行一些代码。我意识到成功地使用了信号和槽。
我的问题是,即使它正在工作:什么定义了在哪个线程中执行槽作为信号发射的直接结果?
是在执行 connect() 函数的线程内部还是什么?
由于我是Rust的新手,我需要有关如何以惯用方式完成错误处理的指导.我发现错误处理样板真的很烦人.
我被多个人困住了Option<T>.None手动处理每个案例太冗长了.
在Haskell,例如,你可以链可选值(Maybe)与各种运营商的操作:fmap,<*>,>>=等:
f x = x * x
g x = x ++ x
main = print $ g <$> show <$> f <$> Just 2
Run Code Online (Sandbox Code Playgroud)
在Rust中看起来不可能.我正在尝试将两个字符的卡片串解析为一个结构Card:
const FACES: &'static str = "23456789TJQKA";
const SUITS: &'static str = "CDHS";
enum Face { /* ... */ }
enum Suit { C, D, H, S }
struct Card {
face: Face,
suit: Suit
}
impl FromStr for …Run Code Online (Sandbox Code Playgroud) 我需要一个变量的引用值。
例如,假设我们有变量qwe和asd:
(setq qwe '(1 2 3)) ;; qwe is set to (1 2 3)
(setq asd ''(1 2 3)) ;; asd is set to '(1 2 3)
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何asd使用qwe?
我是这样做的:
(setq asd `(quote ,qwe))
;; Now asd is '(1 2 3)
Run Code Online (Sandbox Code Playgroud)
但对我来说它看起来又丑又坏。如果没有更好的方法,我会感到惊讶。
在下面的示例中,该Default特征仅用于演示目的。
我的问题是:
f()和的声明有什么区别g()?g()不能编译,因为它与 相同f()?impl trait声明中返回具体类型?struct Something {
}
impl Default for Something {
fn default() -> Self {
Something{}
}
}
// This compiles.
pub fn f() -> impl Default {
Something{}
}
// This doesn't.
pub fn g<T: Default>() -> T {
Something{}
}
Run Code Online (Sandbox Code Playgroud) 初始化OpenGL时,我设置了以下内容:
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glCullFace(GL_FRONT);
Run Code Online (Sandbox Code Playgroud)
然后,我在屏幕上画一个三角形.这就是我所做的一切,真的.但是OpenGL总是使用我指定使用的相反绕组类型glFrontFace.
三角形顶点定义如下:
static GLfloat TRIANGLE[] = {
0.0f, 0.5f, 0.0f, // Point 1 on image
-0.5f, -0.5f, 0.0f, // Point 2
0.5f, -0.5f, 0.0f, // Point 3
};
Run Code Online (Sandbox Code Playgroud)
您可以在以下图片中看到结果:

我期望的是GL_CCW绘制三角形而CL_CW没有.但结果恰恰相反.
我的观点和观点矩阵都是正确的.什么可能导致这个问题?
假设我得到一个JSON,其中包含一些我知道的字段以及一些我不知道的字段:
{
"a": { ... },
"b": { ... },
"c": { ... },
"something": "else",
"more": {"of": ["the", "same"]},
"and": ["even", "some", "more"]
}
Run Code Online (Sandbox Code Playgroud)
由于JSON结构有点复杂,我真的希望使用以下数据在Haskell中使用它:
data A = ...
data B = ...
data C = ...
data MyObject = MyObject
{
a :: A
, b :: B
, c :: C
, additionalFields :: Object
}
Run Code Online (Sandbox Code Playgroud)
是否可以将字段a,b和c转换为Haskell对象,并将所有其他字段打包并作为Aeson.Object传递?
另外,我没有任何JSON字段顺序的保证.