0%

柯里化是什么意思
ChatGPT
柯里化(Currying)是一种将多参数函数转换为一系列单参数函数的技术。在柯里化中,你可以将一个带有多个参数的函数转换为一系列只接受一个参数的函数,每个函数都返回一个新的函数,直到接受足够数量的参数后才执行原始函数。

下面是一个简单的 JavaScript 示例来说明柯里化的概念:

javascript
Copy code
// 原始的多参数函数
function add(x, y) {
return x + y;
}

// 柯里化函数
function curriedAdd(x) {
return function(y) {
return x + y;
};
}

// 使用柯里化函数
const addTwo = curriedAdd(2);
console.log(addTwo(3)); // 输出 5
在这个例子中,我们有一个原始的多参数函数 add(x, y),它接受两个参数并返回它们的和。然后,我们定义了一个柯里化函数 curriedAdd(x),它接受一个参数 x 并返回一个新的函数,这个新函数接受另一个参数 y,并返回 x + y 的结果。最后,我们使用 curriedAdd(2) 来部分应用函数,得到一个新的函数 addTwo,它等价于 function(y) { return 2 + y; },然后我们可以使用这个新函数来计算 2 + 3 的结果。

柯里化的好处在于它能够将原始的多参数函数转换为一系列单参数函数,这样可以更加灵活地进行函数组合和重用,并且能够方便地进行函数的部分应用。

柯里化 是什么意思,举个例子
ChatGPT
柯里化(Currying)是一种将多参数函数转换为一系列单参数函数的技术。在柯里化中,你可以将一个带有多个参数的函数转换为一系列只接受一个参数的函数,每个函数都返回一个新的函数,直到接受足够数量的参数后才执行原始函数。

下面是一个简单的 JavaScript 示例来说明柯里化的概念:

javascript
Copy code
// 原始的多参数函数
function add(x, y) {
return x + y;
}

// 柯里化函数
function curriedAdd(x) {
return function(y) {
return x + y;
};
}

// 使用柯里化函数
const addTwo = curriedAdd(2);
console.log(addTwo(3)); // 输出 5
在这个例子中,我们有一个原始的多参数函数 add(x, y),它接受两个参数并返回它们的和。然后,我们定义了一个柯里化函数 curriedAdd(x),它接受一个参数 x 并返回一个新的函数,这个新函数接受另一个参数 y,并返回 x + y 的结果。最后,我们使用 curriedAdd(2) 来部分应用函数,得到一个新的函数 addTwo,它等价于 function(y) { return 2 + y; },然后我们可以使用这个新函数来计算 2 + 3 的结果。

柯里化的好处在于它能够将原始的多参数函数转换为一系列单参数函数,这样可以更加灵活地进行函数组合和重用,并且能够方便地进行函数的部分应用。

brew install 如何忽略证书错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
brew install timothyye/tap/glance                       7s xhs@xhsdeMac-mini 17:51:35
Running `brew update --auto-update`...
==> Fetching timothyye/tap/glance
Error: glance: Failed to download resource "glance"
Failure while executing; `/usr/bin/env /opt/homebrew/Library/Homebrew/shims/shared/curl --disable --cookie /dev/null --globoff --show-error --user-agent Homebrew/4.1.3\ \(Macintosh\;\ arm64\ Mac\ OS\ X\ 13.6.3\)\ curl/8.1.2 --header Accept-Language:\ en --retry 3 --fail --location --silent --head https://download.xiaozhou.xyz/glance-mac64-0.1.tar.gz` exited with 60. Here's the output:
curl: (60) SSL certificate problem: certificate has expired
More details here: https://curl.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
HTTP/1.1 200 Connection established


ChatGPT
对于 Homebrew,通常情况下它会使用系统中的 curl 或者其他下载工具来下载软件包。因此,要让 Homebrew 忽略证书错误,你可以尝试在运行 brew install 命令之前设置相关的环境变量,让底层的下载工具忽略 SSL 证书验证。

你可以通过设置 HOMEBREW_CURLRC 环境变量来传递 curl 参数。在终端中执行以下命令:

bash
Copy code
export HOMEBREW_CURLRC=1
echo “-k” >> ~/.curlrc
这样做会将 -k 参数添加到 curl 的默认配置文件 ~/.curlrc 中,从而告诉 curl 忽略 SSL 证书验证。然后你再运行 brew install 命令时,Homebrew 应该会使用忽略证书错误的 curl 参数。

请注意,忽略 SSL 证书错误会降低安全性,只应该在调试或者特定情况下使用。

go 切片性能最佳实践
如果在for-loop里对某个slice 使用 append()请先把 slice的容量很扩充到位,这样可以避免内存重新分享以及系统自动按2的N次方幂进行扩展但又用不到,从而浪费内存

1
2
3
4
5
var keys []time.Time
for k := range dateCount {
keys = append(keys, k)
}

改成

1
2
3
4
keys := make([]time.Time, 0, len(dateCount))
for k := range dateCount {
keys = append(keys, k)
}

go 拉取代码报错

1
2
3
4
5
6
7
8
9
10
11
12
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0660 for '/root/.ssh/id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/root/.ssh/id_rsa": bad permissions
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists. (exit status 1)
1
chmod 600 /root/.ssh/id_rsa

nginx alias 和 root 的差异
aliasroot 在 nginx 配置中的作用类似,但并不完全相同。

  1. alias: 使用 alias 指令可以将请求路径映射到指定的文件或目录,例如:

    1
    2
    3
    location /images/ {
    alias /data/images/;
    }

    在上面的配置中,请求路径为 /images/ 的请求会映射到 /data/images/ 目录下。

  2. root: 使用 root 指令可以指定当前请求的根目录,例如:

    1
    2
    3
    server {
    root /var/www/html;
    }

    在上面的配置中,如果请求的路径为 /index.html,则 nginx 会在 /var/www/html 目录下寻找该文件。

区别在于:

  • alias 指令可以指定一个与 URI 不同的目录来处理请求,即将请求路径映射到指定目录下的文件或子目录。
  • root 指令用于指定请求的根目录,nginx 在处理请求时会基于该根目录进行文件查找。

举例来说,假设有以下配置:

1
2
3
4
5
6
7
8
9
location /images/ {
alias /data/images/;
# other configurations for images location
}

location /static/ {
root /var/www/;
# other configurations for static files location
}
  • 对于 /images/ 路径的请求,使用了 alias,nginx 会将请求路径 /images/abc.jpg 映射到 /data/images/abc.jpg 文件。
  • 对于 /static/ 路径的请求,使用了 root,nginx 会将请求路径 /static/def.jpg 映射到 /var/www/static/def.jpg 文件。

因此,虽然 aliasroot 在某种程度上都可以用于指定文件或目录路径,但它们的用法和影响范围略有不同。

安装protoc-gen-go-grpc

1
2
3
4
5
6
protoc --go_out=. --go-grpc_out=. proto/ppm/*
protoc-gen-go-grpc: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.
❯ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

搭文件服务器

1
2
3
4
5
6
7
8
9
10
11
12
# docker-compose.yaml
version: '2'
services:
nginx:
image: nginx:1.13
ports:
- "8080:80"
volumes:
- "./nginx.conf:/etc/nginx/nginx.conf"
- "/root/debug_file/nginx_file_home:/usr/share/nginx/files"
command:
sh -c "ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone && nginx -g 'daemon off;'"

同目录下新增 nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name your.domain.com;
root /usr/share/nginx/files;
charset utf-8;
location / {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
}

启动容器

1
docker-compose up -d --force-recreate

为什么GOPROXY 影响 go mod tidy

1
2
3
GOPROXY="https://goproxy.cn,direct" go mod tidy 才可以
之前的 GOPROXY="https://proxy.golang.org,direct" 不行

也学到了可以把 GO 的环境变量放在同一句命令上让它生效

1
CGO_ENABLED=0 GOOS=linux GO111MODULE=auto go build