bash - 仅打印其参数的函数是纯函数吗?

标签 bash functional-programming

假设您有一个仅打印其参数格式的函数:

__print_spec_result() {
  printf '%s  %s%s (%.3f s)%s\n' "$1" "$2" "$cyan" "$3" "$reset"
}

这里,$1$2是函数的参数,$cyan$reset是常量。

这个函数在函数意义上是纯粹的吗?

最佳答案

不,因为 printf 是不纯的。

参见wiki :

printf() is impure because it causes output to an I/O device as a side effect

来自另一个SO问题和他的回答: Why is printf() an impure function?

printf is impure because its result has "side effects" -- specifically, it prints something on the screen (or in a file, etc). If it were pure, then you could call it a billion times and be sure nothing bad would happen. But if you actually call printf a million times, there certainly is a difference to the user -- it fills up his screen (or disk space, or whatever). So clearly it's not pure.

关于bash - 仅打印其参数的函数是纯函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31111833/

相关文章:

lambda - Java 8 中的 lambda 表达式如何支持函数式编程?

bash - bash/shell 数字上下文中的递归变量扩展从何而来?

macos - 如何从 shell 脚本获取文件大小(以字节为单位)?

c++ - C++ 中类似 QuickCheck 的模板函数基准测试

java - 避免在 Java 8 stream reduce 方法中使用全局变量

javascript - 如何在不改变原始数组的情况下过滤嵌套数组?

linux - Bash:如果第二列中的数字大于 3,如何保持行?

bash - 用 bash 替换 yml 中一行的值

linux - 从 shell 脚本解析文件中的键/值的代码

clojure - 访问 Clojure 的线程优先宏参数