小编Dul*_*nas的帖子

管道`|`运算符在elm-lang的case表达式中做什么?

我的 Elm 代码中有以下代码片段:

type alias Model =
  { content : String
  }


update : Msg -> Model -> Model
update msg model =
  case msg of
    Change newContent ->
      { model | content = newContent }
Run Code Online (Sandbox Code Playgroud)

有什么作用{ model | content = newContent }?它是否分配(绑定)newContenttomodel和的值content?这就是| 操作员被放置在那里的原因吗?

elm

8
推荐指数
2
解决办法
459
查看次数

如何在 Python 中对需要活动 Click 上下文的函数进行单元测试

我正在使用命令行 shell,并且正在尝试测试一些解析命令参数的函数。

解析器.py:

import shlex
import click


def process_cmd(args, called_self=False):
    result = args

    # Replace aliases
    if not called_self:
        aliases = click.get_current_context().obj.config['ALIASES']
        if result[0] in aliases:
            substitution = process_cmd(split_args(aliases[result[0]]), True)
            if len(result) == 1:
                result = substitution
            else:
                result = substitution + result[1:]

    return result


def split_pipeline(args):
    cmd = []
    for arg in args:
        if arg == '|':
            yield process_cmd(cmd)
            cmd = []
        else:
            cmd.append(arg)
    # yield the last part of the pipeline
    yield process_cmd(cmd)
Run Code Online (Sandbox Code Playgroud)

这是我编写的单元测试split_pipeline

import parser …
Run Code Online (Sandbox Code Playgroud)

python unit-testing pytest python-click

7
推荐指数
1
解决办法
1786
查看次数

将 Zig 中的 [*]u8 转换为 []u8

当我有一个[*]u8指针和一个usize长度时,如何将指针转换为[]u8指定长度的切片?

pointers zig

6
推荐指数
1
解决办法
1046
查看次数

如何 List.sum 一个列表(可能是浮点数)?

我正在尝试将字符串转换为浮点数并找到它们的总和:

> String.split "," "0.2,0.3,3.1" 
> List.map String.toFloat ["0.2","0.3","3.1"]
> List.sum [Just 0.2,Just 0.3,Just 3.1]
Run Code Online (Sandbox Code Playgroud)

但我收到这些编译器消息:

This argument is a list of type:

    List (Maybe Float)

But `sum` needs the 1st argument to be:

    List number

Hint: Use Maybe.withDefault to handle possible errors. Longer term, it is
usually better to write out the full `case` though!
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到这些值的总和?

list elm

2
推荐指数
1
解决办法
108
查看次数

用重复值初始化 Nim 数组

我有这种带有几个整数字段的类型:

type
    Thing = object
        x: int32
        y: int32
Run Code Online (Sandbox Code Playgroud)

我需要用这种类型制作一个固定大小的大数组:

array[0..255, Thing]
Run Code Online (Sandbox Code Playgroud)

如何将所有项的 x 和 y 设置为 0 来初始化此数组?

arrays nim-lang

1
推荐指数
1
解决办法
162
查看次数

标签 统计

elm ×2

arrays ×1

list ×1

nim-lang ×1

pointers ×1

pytest ×1

python ×1

python-click ×1

unit-testing ×1

zig ×1