sky*_*yde 6 monads f# haskell scala do-notation
F#Computation Expressions允许隐藏厚层语法糖背后的一元语法的复杂性.Scala中是否有类似的东西?
我认为这是为了理解......
例:
val f = for {
a <- Future(10 / 2) // 10 / 2 = 5
b <- Future(a + 1) // 5 + 1 = 6
c <- Future(a - 1) // 5 - 1 = 4
} yield b * c // 6 * 4 = 24
val result = f.get
Run Code Online (Sandbox Code Playgroud)
但它确实感觉不对.有更好的语法吗?
例如,你可以拥有哈斯克尔
main = do fromHandle <- getAndOpenFile "Copy from: " ReadMode
toHandle <- getAndOpenFile "Copy to: " WriteMode
contents <- hGetContents fromHandle
hPutStr toHandle contents
hClose toHandle
putStr "Done."
这与scala不同,看起来不像是一个foreach循环.Scala语法似乎与List comprehension有太强的耦合,这是一个独特的概念.这阻止我编写看起来并不奇怪的内部DSL(monad).