首先,我创建了一个新工作区:
stack new xxxx
stack init
stack build
Run Code Online (Sandbox Code Playgroud)
然后
cd xxx\app
stack ghci
import Data.Map
Run Code Online (Sandbox Code Playgroud)
我可以导入像Data.Char和的其他模块Data.List,但我无法导入Data.Map.GHCi告诉我:
Could not find module 'Data.Map'
It is a member of the hidden package 'containers-0.5.7.1@containers-0.5.7.1'.`
Run Code Online (Sandbox Code Playgroud) 我很好奇我是否应该使用Write::flush或者File::sync_all当我写完文件时。
https://doc.rust-lang.org/src/alloc/slice.rs.html#268-270
https://doc.rust-lang.org/src/core/cmp.rs.html#1034
pub fn sort(&mut self)
where
T: Ord,
{
merge_sort(self, |a, b| a.lt(b));
}
fn lt(&self, other: &Rhs) -> bool {
matches!(self.partial_cmp(other), Some(Less))
}
Run Code Online (Sandbox Code Playgroud)
它lt()在merge_sort(). 如果partial_cmp()返回None则lt()返回Less。
所以,似乎PartialOrd已经足够了sort()。
Prelude> :t fmap (\x -> 2x) []
fmap (\x -> 2x) [] :: Num (t -> b) => [b]
Prelude> :t fmap (\x -> 2 * x) []
fmap (\x -> 2 * x) [] :: Num b => [b]
Prelude> :t 1 1
1 1 :: (Num (t -> t1), Num t) => t1
Prelude> :t 1 * 1
1 * 1 :: Num a => a
Run Code Online (Sandbox Code Playgroud)
有什么区别2x和2*x?
是什么意思Num (t -> t1) => t1 …
class IntTuple(tuple):
def __new__(cls, iterable):
generator = (x for x in iterable if isinstance(x, int) and x > 0)
return super().__new__(cls, generator)
Run Code Online (Sandbox Code Playgroud)
Pycharm建议我删除generator这是在return。为什么?
它说: 该检查报告声明的参数和实际参数之间存在差异,以及不正确的参数(例如重复命名参数)和不正确的参数顺序。装饰器也被分析。
但它的输出是我所期望的。
例子:
t = IntTuple([1, -1, "abc", 2, [1, 2], 3])
print(t)
Run Code Online (Sandbox Code Playgroud)
output: (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
我是 Python 新手,所以我认为这可能缺乏标准化。
我应该如何改进它?
我从RFC 246中知道这一点:
- 常量声明常量值.它们代表一个值,而不是内存地址.这是人们可以达到的最常见的事情,并且
static几乎在所有情况下我们今天都会知道它.- 静态声明全局变量.这些代表一个内存地址.它们很少使用:主要用例是全局锁,全局原子计数器以及与传统C库的接口.
当我试图维护一张桌子时,我不知道两者之间究竟有什么不同.
我应该选择哪一个?
我想得到汉字的Unicode值.它可能看起来像let values: &[u16] = f("???");
当我使用时,"?".as_bytes()我得到了[227, 129, 174].
当我使用'?'.escape_unicode()我'\u306e'的0x306e正是我想要的.
我几天才学习Rust.我认为这两个代码示例是相同的,但编译器不同意.你能解释第二部分会发生什么吗?为什么我需要解除引用key两次,但value一次?
第一部分
use std::collections::HashMap;
let mut h = HashMap::new();
h.insert(1, 1);
h.insert(2, 2);
let mut keys: Vec<i32> = Vec::new();
let mut values: Vec<i32> = Vec::new();
for (k, v) in &h {
keys.push(**k);
values.push(*v);
}
Run Code Online (Sandbox Code Playgroud)
第二部分
fn main() {
let mut v = vec![2, 3, 5, 1, 2, 3, 8, 6, 3, 1, 4, 6, 7];
use std::collections::HashMap;
let mut h = HashMap::new();
for element in &v {
let count = h.entry(element).or_insert(0);
*count += 1;
}
let …Run Code Online (Sandbox Code Playgroud)