在我正在编写的程序中,我需要将列表中的每个元素与所有其他元素相乘,如下所示:
List = [i1, i2, i3]
Result = [i1*i2, i1*i3, i2*i3]
Run Code Online (Sandbox Code Playgroud)
我一直在搞乱循环一段时间,但我似乎无法让它工作.这是我到目前为止(不起作用,我知道为什么它不起作用,只是征求意见):
def function():
for j in range(len(list)):
n = j+1
for i in range(len(list)):
if n+i > len(list):
n -= 1
x = factor[j] * factor[j+i]
result.append(x)
return
Run Code Online (Sandbox Code Playgroud) use std::ops::Deref;
use std::sync::{Arc, Mutex, MutexGuard};
struct Var {}
fn multithreading() -> Var {
let shared_var = Arc::new(Mutex::new(Var {}));
/*
multithreading job
*/
return *(shared_var.lock().unwrap().deref());
}
Run Code Online (Sandbox Code Playgroud)
我正在定义一个要操作的多线程函数Var,但该函数无法编译并抱怨:
error[E0507]: cannot move out of a shared reference
Run Code Online (Sandbox Code Playgroud)
有没有办法停止共享shared_var并返回其中的变量?
实现 Trait CopyforVar也可以解决该错误,但在我的实际用例中Var太大而无法复制,我更喜欢任何其他解决方案。
在 C++ 中,我们可以重载operator bool()将结构转换为bool:
struct Example {
explicit operator bool() const {
return false;
}
};
int main() {
Example a;
if (a) { /* some work */ }
}
Run Code Online (Sandbox Code Playgroud)
我们可以用 Rust 做一些简单(而且优雅)的事情吗:
pub struct Example {}
fn main() {
let k = Example {};
if k {
// some work
}
}
Run Code Online (Sandbox Code Playgroud) 我希望将所有 for 循环与在其主体中声明的向量相匹配(稍后可能会扩展到 while 循环和 do-while 循环):
\n#include <vector>\n\nint main() {\n for (int i = 0; i < 10; i++) {\n std::vector<int> foo;\n }\n\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n我从这个程序中得到的 AST 是
\n`-FunctionDecl 0x55c1d33c8bc8 <main.cpp:3:1, line:9:1> line:3:5 main \'int ()\'\n `-CompoundStmt 0x55c1d3402a48 <col:12, line:9:1>\n |-ForStmt 0x55c1d34029e0 <line:4:3, line:6:3>\n | |-DeclStmt 0x55c1d33c8d40 <line:4:8, col:17>\n | | `-VarDecl 0x55c1d33c8cb8 <col:8, col:16> col:12 used i \'int\' cinit\n | | `-IntegerLiteral 0x55c1d33c8d20 <col:16> \'int\' 0\n | |-<<<NULL>>>\n | |-BinaryOperator 0x55c1d33c8db0 <col:19, col:23> \'bool\' \'<\'\n …Run Code Online (Sandbox Code Playgroud) 我正在为表达式构建一个解析器.
这是我的语法规则:
expr ::= term (+ expr | - expr | null)
term ::= expo (* term | / term | null)
expo ::= factor (^ expo | null)
factor ::= (expr) | int
Run Code Online (Sandbox Code Playgroud)
和相应的代码:
expr :: Parser Int
expr = do t <- term
do _ <- symbol "+"
e <- expr
return (t + e)
<|> do _ <- symbol "-"
e <- expr
return (t - e)
<|> return t
term :: Parser Int
term = do …Run Code Online (Sandbox Code Playgroud) Just (+) <*> Just 3 <*> Just 5
Run Code Online (Sandbox Code Playgroud)
只是8
pure (+) <*> Just 3 <*> Just 5
Run Code Online (Sandbox Code Playgroud)
只是8
pure (*3) <*> [0..10]
Run Code Online (Sandbox Code Playgroud)
[0,3,6,9,12,15,18,21,24,27,30]
Just (*3) <*> [0..10]
Run Code Online (Sandbox Code Playgroud)
无法将类型'[]'与'Maybe'匹配
预期类型:也许b
实际类型:[b]
在'(<*>)'的第二个参数中,即'[0 ... 10]'
在表达式中:Just(*3)<*> [0 .. 10]
在'it'的等式中:it = Just(*3)<*> [0 ... 10]
如果是pure和Just互换,它们有什么不同的时候?
我正在根据字体大小调整图像大小,以根据文本大小更改图像大小,但是结果与macOS和Android(两者均与Chrome相比)截然不同。
在桌面版Chrome上:
在Android的Chrome上:
您可以看到,与文本相比,Android上的图像要小得多。如何解决,使图片比Android上的文字大2.5?
看起来像GitHub页面有问题吗?
https://s999inf.github.io/imagezoom/
https://github.com/s999inf/imagezoom/blob/master/index.html
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-rendering: optimizeLegibility;
-webkit-font-smoothing: subpixel-antialiased;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.logos > img {
display: inline-block;
height: 100vh;
max-height: 5em;
}
.test-logos {
display: inline-block;
}
.test-logos__img-2em {
height: 2em;
}
.test-logos__img-3em {
height: 3em;
}
.test-logos__img-5em {
height: 5em;
}
</style>
</head>
<body>
copy text copy text copy text copy text copy text copy text copy text copy text copy text copy text copy …Run Code Online (Sandbox Code Playgroud) 我发现在C++中允许将基本对象的私有函数覆盖为公共函数,因为Visual Studio会产生0警告.这样做有潜在的危险吗?
如果没有,在基础对象中声明私有,受保护和公共虚函数之间有什么区别?
*Main> do 0
Run Code Online (Sandbox Code Playgroud)
0
*Main> do return 0
Run Code Online (Sandbox Code Playgroud)
0
*Main> do (Just 0)
Run Code Online (Sandbox Code Playgroud)
只是0
*Main> do return (Just 0)
Run Code Online (Sandbox Code Playgroud)
只是0
*Main> do Nothing
Run Code Online (Sandbox Code Playgroud)
没有
*Main> do return Nothing
Run Code Online (Sandbox Code Playgroud)
没有
bareDo :: a -> a
bareDo x = do x
doReturn :: Monad m => a -> m a
doReturn x = do return x
Run Code Online (Sandbox Code Playgroud)
为什么会do x和do return x同样地进行评价,而当将他们不这样做?
我试图理解Monad,这已经足够的教程和讨论,但我无法弄清楚.
也就是说即使是后写
不要阅读monad教程.
所以,我觉得弄脏手可能会有所帮助.
我真的很感谢谁帮我离开这里(我的意思是do和return,不Monad,我知道这将需要一些天).
haskell ×3
c++ ×2
python ×2
rust ×2
applicative ×1
clang ×1
clang-tidy ×1
css ×1
functor ×1
html ×1
javascript ×1
monads ×1
parsing ×1
python-2.7 ×1