Last active
February 24, 2017 21:53
-
-
Save dgendill/ec45d54b093a7adf471cf0bceceab971 to your computer and use it in GitHub Desktop.
An example of using Aff to run synchronous and asynchronous effects, and how to sequentially group effects using parSequence.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Run with `pulp run --main AffParallel` | |
module AffParallelExample where | |
import Prelude | |
import Control.Monad.Aff (Aff, launchAff, forkAll, later') | |
import Control.Monad.Console (Console, log) | |
import Control.Parallel (parSequence) | |
type Piece = String | |
type X = Int | |
type Y = Int | |
main = launchAff $ do | |
log "Running four half-second animations synchronously (2s total time)" | |
animatePieceToPosition' "s-King" 1 1 | |
animatePieceToPosition' "s-Queen" 1 2 | |
animatePieceToPosition' "s-Jack" 1 3 | |
animatePieceToPosition' "s-Joker" 1 4 | |
log "Running four half-second animations asynchronously (.5s total time)" | |
forkAll | |
[ animatePieceToPosition' "as-One" 2 1 | |
, animatePieceToPosition' "as-Two" 2 2 | |
, animatePieceToPosition' "as-Three" 2 3 | |
, animatePieceToPosition' "as-Four" 2 4 ] | |
log "Running 2 sets of four-asynchronous-half-second-animations synchronously (4s total time)" | |
void $ parSequence | |
[ animatePieceToPosition' "Rook" 3 1 | |
, animatePieceToPosition' "Pawn" 3 2 | |
, animatePieceToPosition' "Castle" 3 3 | |
, animatePieceToPosition' "Horse" 3 4 ] | |
void $ parSequence | |
[ animatePieceToHome' "Rook" | |
, animatePieceToHome' "Pawn" | |
, animatePieceToHome' "Castle" | |
, animatePieceToHome' "Horse" ] | |
log "Done" | |
-- | A function that simulates an animation, e.g. moving a game piece | |
-- | from it's current position to point x, y | |
animatePieceToPosition' :: forall e. Piece -> X -> Y -> Aff (console :: CONSOLE | e) Unit | |
animatePieceToPosition' piece x y = do | |
log $ "- Moving " <> piece <> " to " <> (showPosition x y) | |
later' 1000 $ log $ "- Done moving " <> piece <> " to " <> (showPosition x y) | |
where showPosition x' y' = "(" <> (show x') <> "," <> (show y') <> ")" | |
-- | A function that simulates an animation, e.g. moving a game piece | |
-- | from it's current position to it's "home" position | |
animatePieceToHome' :: forall e. Piece -> Aff (console :: CONSOLE | e) Unit | |
animatePieceToHome' piece = do | |
log $ "- Moving " <> piece <> " back to 'home' position." | |
later' 1000 $ log $ "- Done moving " <> piece <> " to home." | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Running four half-second animations synchronously (4s total time) | |
- Moving s-King to (1,1) | |
- Done moving s-King to (1,1) | |
- Moving s-Queen to (1,2) | |
- Done moving s-Queen to (1,2) | |
- Moving s-Jack to (1,3) | |
- Done moving s-Jack to (1,3) | |
- Moving s-Joker to (1,4) | |
- Done moving s-Joker to (1,4) | |
Running four one-second animations asynchronously (1s total time) | |
- Moving as-One to (2,1) | |
- Moving as-Two to (2,2) | |
- Moving as-Three to (2,3) | |
- Moving as-Four to (2,4) | |
Running 2 sets of four asynchronous one-second animations synchronously (2s total time) | |
- Moving Rook to (3,1) | |
- Moving Pawn to (3,2) | |
- Moving Castle to (3,3) | |
- Moving Horse to (3,4) | |
- Done moving as-One to (2,1) | |
- Done moving as-Two to (2,2) | |
- Done moving as-Three to (2,3) | |
- Done moving as-Four to (2,4) | |
- Done moving Rook to (3,1) | |
- Done moving Pawn to (3,2) | |
- Done moving Castle to (3,3) | |
- Done moving Horse to (3,4) | |
- Moving Rook back to 'home' position. | |
- Moving Pawn back to 'home' position. | |
- Moving Castle back to 'home' position. | |
- Moving Horse back to 'home' position. | |
- Done moving Rook to home. | |
- Done moving Pawn to home. | |
- Done moving Castle to home. | |
- Done moving Horse to home. | |
Done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment