Equivalent to an arbitrary Haskell notation or F # evaluation expression in Scala?

F # Calculations Expressions hide the complexity of monadic syntax behind a thick layer of syntactic sugar. Is there something similar in Scala?

I think this is for understanding ...

Example:

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 

But this is really not the case. Is there any better syntax?

for example in haskell you will have

  main = do fromHandle <- getAndOpenFile "Copy from:" ReadMode
           toHandle <- getAndOpenFile "Copy to:" WriteMode 
           contents <- hGetContents fromHandle
           hPutStr toHandle contents
           hClose toHandle
           putStr "Done."

this, unlike scala, is not like foreach loops. The Scala syntax seems to be too strong when combined with an understanding of List, which is an understandable concept. This prevents me from writing internal DSLs (monads) that don't look weird.

+6
source share
2 answers

It seems that this syntax does not exist in scala, and we would need to implement it ourselves using the compiler plugin architecture.

-2
source

The missing fragment is probably used for = scala for-understanding:

 val f = for { a <- Future(10 / 2) // 10 / 2 = 5 b <- Future(a + 1) // 5 + 1 = 6 c <- Future(a - 1) // 5 - 1 = 4 d = b * c // 6 * 4 = 24 } yield d val result = f.get 

When reasonably mixing both <- and = you must have all the necessary flexibility.

+4
source

Source: https://habr.com/ru/post/897654/


All Articles