我是函数式编程的新手,想写一个函数,接收长度不固定的值列表,然后生成一个字符串,其中包含每个值经过格式化后的表示。
用伪代码举个例子。
render_entries([2, 3, 4])
// -> "entry: 2, entry: 3, entry: 4"
(我具体用的是 Gleam (https://gleam.run/),不过我想同样的思路也适用于其他函数式语言,只是语法不同)
应该怎样实现呢?
我是函数式编程的新手,想写一个函数,接收长度不固定的值列表,然后生成一个字符串,其中包含每个值经过格式化后的表示。
用伪代码举个例子。
render_entries([2, 3, 4])
// -> "entry: 2, entry: 3, entry: 4"
(我具体用的是 Gleam (https://gleam.run/),不过我想同样的思路也适用于其他函数式语言,只是语法不同)
应该怎样实现呢?
I'm a functional programming beginner, trying to create a function which accepts values in a list (whose length isn't fixed) and produces a string containing a formatted representation of every value.
As an example (using pseudocode):
render_entries([2, 3, 4])
// -> "entry: 2, entry: 3, entry: 4"
(My particular language is Gleam (https://gleam.run/), although I'd expect the idea to apply to other functional languages with only syntactic differences)
How would I accomplish that?
我想出来了。我的 Gleam 例子具体是这样写的。
list.fold(entries, "", fn(buffer, entry) -> String {
buffer + "entry: " + int.to_string(entry) + " "
})
I've worked it out. For my particular Gleam example:
list.fold(entries, "", fn(buffer, entry) -> String {
buffer + "entry: " + int.to_string(entry) + " "
})
一个接收列表,并为每个值生成一个 (???) 的函数
函数式语言通常把这种操作叫作 "map"。Gleam 也有名为 map 的列表函数 (https://hexdocs.pm/gleam_stdlib/gleam/list.html#map),看起来正好能满足你的需求。比如写出这样的表达式
list.map([2, 3, 4], fn(entry) { "entry: " <> int.to_string(entry) })
得到的就是包含转换后元素的列表。然后可以用 list.intersperse (https://hexdocs.pm/gleam_stdlib/gleam/list.html#intersperse) 来连接列表,在相邻元素之间插入额外的元素(这种辅助函数在函数式语言中远没有那么普遍)。
Gleam 似乎提供了类似 Elixir 的管道 (https://tour.gleam.run/functions/pipelines/),所以我可能会这样写
[2, 3, 4]
|> list.map(fn(entry) { "entry: " <> int.to_string(entry) })
|> list.intersperse(", ")
我认为函数式编程中还有两种基本操作,一种是把列表归约为单个结果的 reduce 或 fold,另一种是接收列表和谓词,并返回只包含满足谓词的元素的新列表的 filter。其中一些操作可以通过另一些操作来表达(特别是可以用 fold 实现 map 和 filter,但效率可能不高),不过根据我的经验,选择合适的操作通常更容易理解,而且往往更快。
a function receiving a list and producing a (???) for every value
Functional languages normally call this operation "map". There is a list function called map in Gleam (https://hexdocs.pm/gleam_stdlib/gleam/list.html#map), which seems to match what you need. For example, with an expression such as
list.map([2, 3, 4], fn(entry) { "entry: " <> int.to_string(entry) })
the result would be a list containing the transformed entries. Afterwards, list.intersperse (https://hexdocs.pm/gleam_stdlib/gleam/list.html#intersperse) lets you join that list with an extra entry placed between successive elements (a helper that isn't nearly as universal in functional languages).
Gleam appears to offer pipelines similar to Elixir's (https://tour.gleam.run/functions/pipelines/), so I'd perhaps express it as
[2, 3, 4]
|> list.map(fn(entry) { "entry: " <> int.to_string(entry) })
|> list.intersperse(", ")
Two more operations that I regard as fundamental across functional programming include reduce or fold, taking a list down to one result, and filter, accepting a list plus a predicate and producing a list containing the entries passing that predicate. Some can be expressed through others (in particular, fold can implement map as well as filter, albeit perhaps inefficiently), but in my experience selecting the appropriate operation is easier to follow and frequently quicker.
我在 VSCode 编辑器中输入了下面的 Lean4 代码: 然后得到 "unknown identifier 'begin'"。在下面三行处加入 end,会得到 "invalid 'end', insufficient scopes"。我哪里做错了…
我上次查看 JavaScript 文档还是在 1996 年。我想找一门涵盖“必需的 JavaScript 和网页概念”的课程,让我能有把握地编写 Elm 和使用端口。 不理解 DOM 确实阻碍了我的 Elm 工作,而且我也不知道自己还缺哪些知识。:f…
很多 go 示例都这样开头, 我使用的 GOPATH, main 和 fmt 模块存在哪里?