假设您有一个仅打印其参数格式的函数:
__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/