快乐冲浪与生活

多体验、多体会、多体悟

0%

失业

月初的时候,出于某些原因,总经理跟我说:“XX,招你进来是看在 JY 的面子上,现在 JY 要走了,公司也没有留你的必要了。”

我说:“好,没问题,正常给赔偿就行。”

原文地址: https://go.dev/blog/deconstructing-type-parameters

译者评论

本文通过 slices.Clone 泛型函数介绍了 Go 是如何使用类型推断完成参数类型的解构。简单来说,如果第一个类型参数是一个复合类型,则可以通过第二、第三或更多的类型参数约束复杂类型中的类型参数,而类型推断则可以通过第一个参数推断出后续类型参数的实际类型。另外本文还说明为消除歧义而引入 ~ 符号,即用于指定类型的底层类型。

这是一篇关于 React Hooks 的技术文章翻译,原文地址: https://dev.to/michael_osas/understanding-react-hooks-how-to-use-useref-usememo-and-usecallback-for-more-efficient-code-3ceh ,翻译不当之处请指正。

译者评价

文章主要介绍了 useRef、useMemo 和 useCallback 3 个 React Hook,读者可以通过此文了解 3 种 Hook 的使用方式、场景,但文章也存在一些缺点:

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: a2htray
# create date: 2023/3/26

"""
PAT 乙级 1046
"""

if __name__ == '__main__':
    n = int(input())
    a = 0
    b = 0

    for i in range(n):
        tokens = list(map(int, input().split(' ')))
        if tokens[1] == tokens[3]:
            continue
        total = tokens[0] + tokens[2]
        if tokens[1] == total:
            b += 1
            continue
        if tokens[3] == total:
            a += 1
            continue

    print(a, b)

history 命令可用于浏览历史执行过的命令,该命令在 Bourne Shell 中不可用,但 Bash 和 Korn 则支持该特性。在 Bash 和 Korn 中,每一次命令的执行都被视为一次事件,并且配有相应的事件号(Event Number)。在需要的情况下,可以通过事件号将执行过的命令再次执行。

不带选项的 history 会在终端输出所有执行过的历史命令。

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: a2htray
# create date: 2023/3/25

"""
PAT 乙级 1044
"""

mars_digits = [
    'tret',
    'jan', 'feb', 'mar', 'apr',
    'may', 'jun', 'jly', 'aug',
    'sep', 'oct', 'nov', 'dec',
]

mars_carries = [
    'tam', 'hel', 'maa', 'huh',
    'tou', 'kes', 'hei', 'elo',
    'syy', 'lok', 'mer', 'jou',
]


def is_earth(s: str):
    return s.isdigit()


def is_mars(s: str):
    return not is_earth(s)


def to_earth(s: str):
    tokens = s.split(' ')
    if len(tokens) == 1:
        if tokens[0] in mars_digits:
            return mars_digits.index(tokens[0])
        if tokens[0] in mars_carries:
            return (mars_carries.index(tokens[0]) + 1) * 13
    else:
        return (mars_carries.index(tokens[0]) + 1) * 13 + mars_digits.index(tokens[1])


def to_mars(s: str):
    num = int(s)
    if num < 13:
        return mars_digits[num]

    if num % 13 == 0:
        return f'{mars_carries[num // 13 - 1]}'
    else:
        return f'{mars_carries[num // 13 - 1]} {mars_digits[num % 13]}'


if __name__ == '__main__':
    n = int(input())
    lines = []
    for _ in range(n):
        line = input()
        lines.append(line)

    for line in lines:
        if is_earth(line):
            print(to_mars(line))
        else:
            print(to_earth(line))