4 ms·
In most cases (like the example), it simply allows you to call "effectful" code (often monadic, IO etc) to populate arguments of a function. My favorite exampl
by spopejoy 11y ago
In most cases (like the example), it simply allows you to call "effectful" code (often monadic, IO etc) to populate arguments of a function.
My favorite example is "I want to grab three web pages concurrently and return them in a single triple." To the haters who think this is just golf, just TRY to write this as beautifully in any other language.
(page1, page2, page3)
<- runConcurrently $ (,,)
<$> Concurrently (getURL "url1")
<*> Concurrently (getURL "url2")
<*> Concurrently (getURL "url3")
(taken from the excellent haddocks for Control.Current.Async https://hackage.haskell.org/package/async-2.0.1.4/docs/Control-Concurrent-Async.html#t:Concurrently https://hackage.haskell.org/package/async-2.0.1.4/docs/Contr...)
- doublec 11y agoAlice ML: val (page1, page2, page3) = (spawn getURL "url1", spawn getURL "url2", spawn getURL "url3") The requests are concurrent. Spawn returns a future and 'page1', etc are futures immediately. When the value of 'page1', etc is requested later the requesting process either blocks until the getURL for that value is complete, or transforms into the value implicitly if it's already done. I think it's just as nice as the Haskell example. I do agree that Haskell is a great language though.
- purescript 11y agoI don't know Alice ML but one advantage of using abstractions like Applicative is that you can write code which is polymorphic in the particular applicative you choose. So, you can mock your concurrent requests using the Identity or ZipList applicative, and use the type class laws to prove things which are true about both. I'm guessing, but it looks like the spawn syntax is built-in here. Not that it's not as elegant as the Concurrent version, but likely not as powerful from an abstraction point of view.
- doublec 11y agoCorrect, spawn is built in in Alice ML. Haskell wins on all the ways it can be used beyond built in functionality compared to it.
- deleted 11y ago[deleted]