我当时正在读《Scala with Cats》一书,其中有这样一句话,我将在这里引用:
\n\n\n请注意,Scala\xe2\x80\x99s Futures 是纯函数式编程的一个很好的例子,因为它们是引用透明的。
\n
另外,还提供了一个示例,如下所示:
\nval future1 = {\n // Initialize Random with a fixed seed:\n val r = new Random(0L)\n // nextInt has the side-effect of moving to\n // the next random number in the sequence:\n val x = Future(r.nextInt)\n for {\n a <- x\n b <- x\n } yield (a, b)\n}\nval future2 = {\n val r = new Random(0L)\n for {\n a <- Future(r.nextInt)\n b <- Future(r.nextInt)\n } yield (a, b)\n}\nval …Run Code Online (Sandbox Code Playgroud) 我有 Scala 背景,在 Scala 中,您可以将函数定义为单个值或实际函数,例如:
val inc1: Int => Int = _ + 1 // single FUNCTION value
def inc2(x: Int): Int = x + 1 // normal function definition
// in this case "inc1 eq inc1" is true, since this is a single instance
// but "inc2 eq inc2" is false
Run Code Online (Sandbox Code Playgroud)
这两个有一些差异(即大小分配,第一个是单个实例,而另一个每次调用时都会返回一个实例,...),因此根据用例,我们可以推断出一个使用。现在我是 golang 的新手,想知道以下 2 个函数定义(如果我的短语有误请纠正我)在 Golang 中是否有所不同,如果有的话,有什么不同?
var inc1 = func(x int) int { return x + 1 }
func inc2(x int) int { return x + 1 …Run Code Online (Sandbox Code Playgroud)