Skip to content

Commit

Permalink
Merge pull request #1 from conradlo/update-exercise
Browse files Browse the repository at this point in the history
Update exercises
  • Loading branch information
conradlo authored Oct 24, 2018
2 parents cf0b510 + 754ad5d commit 5a04be3
Show file tree
Hide file tree
Showing 73 changed files with 432 additions and 706 deletions.
6 changes: 6 additions & 0 deletions exercises/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.psci
.psci_modules
.psc-package
.psc-ide-port
.pulp-cache
output
32 changes: 0 additions & 32 deletions exercises/chapter10/bower.json

This file was deleted.

16 changes: 16 additions & 0 deletions exercises/chapter10/psc-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "chapter10",
"set": "psc-0.12.0-20180625",
"source": "https://github.com/purescript/package-sets.git",
"depends": [
"prelude",
"console",
"lists",
"strings",
"foreign",
"foreign-generic",
"web-html",
"react",
"react-dom"
]
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Control.Monad.Eff.Alert where
module Effect.Alert where

import Prelude

import Control.Monad.Eff (kind Effect, Eff)
import Effect (kind Effect, Effect)

foreign import data ALERT :: Effect

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Control.Monad.Eff.Storage where
module Effect.Storage where

import Prelude

import Control.Monad.Eff (kind Effect, Eff)
import Data.Foreign (Foreign)
import Effect (kind Effect, Effect)
import Foreign

foreign import data STORAGE :: Effect

Expand Down
16 changes: 8 additions & 8 deletions exercises/chapter10/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module Main where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Alert (ALERT, alert)
import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.Eff.Storage (STORAGE, setItem, getItem)
import Effect (Effect)
import Effect.Alert (ALERT, alert)
import Effect.Console (log)
import Effect.Storage (STORAGE, setItem, getItem)
import Control.Monad.Except (runExcept)
import DOM (DOM)
import DOM.HTML (window)
Expand All @@ -18,10 +18,10 @@ import Data.AddressBook.Validation (Errors, validatePerson')
import Data.Array ((..), length, modifyAt, zipWith)
import Data.Either (Either(..))
import Data.Foldable (foldMap, for_)
import Data.Foreign (ForeignError, readNullOrUndefined, readString, renderForeignError, toForeign)
import Data.Foreign.Class (class Decode, class Encode)
import Data.Foreign.Generic (decodeJSON, defaultOptions, encodeJSON, genericDecode, genericEncode)
import Data.Foreign.Index (index)
import Foreign (ForeignError, readNullOrUndefined, readString, renderForeignError, unsafeToForeign)
import Foreign.Class (class Decode, class Encode)
import Foreign.Generic (decodeJSON, defaultOptions, encodeJSON, genericDecode, genericEncode)
import Foreign.Index (index)
import Data.Generic.Rep (class Generic)
import Data.List.NonEmpty (NonEmptyList)
import Data.Maybe (Maybe(..), fromJust, fromMaybe)
Expand Down
30 changes: 0 additions & 30 deletions exercises/chapter11/bower.json

This file was deleted.

13 changes: 13 additions & 0 deletions exercises/chapter11/psc-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "chapter11",
"set": "psc-0.12.0-20180625-2",
"source": "https://github.com/purescript/package-sets.git",
"depends": [
"prelude",
"node-readline",
"ordered-collections",
"strings",
"transformers",
"yargs"
]
}
4 changes: 2 additions & 2 deletions exercises/chapter11/src/Game.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Data.Map as M
import Data.Set as S
import Control.Monad.RWS (RWS)
import Control.Monad.Reader.Class (ask)
import Control.Monad.State.Class (get, modify, put)
import Control.Monad.State.Class (get, modify_, put)
import Control.Monad.Writer.Class (tell)
import Data.Coords (Coords(..), prettyPrintCoords, coords)
import Data.Foldable (for_)
Expand Down Expand Up @@ -42,7 +42,7 @@ pickUp item = do
_ -> tell (L.singleton "I don't see that item here.")

move :: Int -> Int -> Game Unit
move dx dy = modify (\(GameState state) -> GameState (state { player = updateCoords state.player }))
move dx dy = modify_ (\(GameState state) -> GameState (state { player = updateCoords state.player }))
where
updateCoords :: Coords -> Coords
updateCoords (Coords p) = coords (p.x + dx) (p.y + dy)
Expand Down
25 changes: 6 additions & 19 deletions exercises/chapter11/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ module Main where

import Prelude
import Node.ReadLine as RL
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.Eff.Exception (EXCEPTION)
import Effect (Effect)
import Effect.Console (log)
import Control.Monad.RWS (RWSResult(..), runRWS)
import Data.Either (Either(..))
import Data.Foldable (for_)
Expand All @@ -18,13 +17,8 @@ import Node.Yargs.Applicative (Y, runY, flag, yarg)
import Node.Yargs.Setup (usage)

runGame
:: forall eff
. GameEnvironment
-> Eff ( exception :: EXCEPTION
, readline :: RL.READLINE
, console :: CONSOLE
| eff
) Unit
:: GameEnvironment
-> Effect Unit
runGame env = do
interface <- RL.createConsoleInterface RL.noCompletion
RL.setPrompt "> " 2 interface
Expand All @@ -33,11 +27,7 @@ runGame env = do
lineHandler
:: GameState
-> String
-> Eff ( exception :: EXCEPTION
, console :: CONSOLE
, readline :: RL.READLINE
| eff
) Unit
-> Effect Unit
lineHandler currentState input = do
case runRWS (game (split (wrap " ") input)) env currentState of
RWSResult state _ written -> do
Expand All @@ -51,10 +41,7 @@ runGame env = do

pure unit

main :: Eff ( exception :: EXCEPTION
, console :: CONSOLE
, readline :: RL.READLINE
) Unit
main :: Effect Unit
main = runY (usage "$0 -p <player name>") $ map runGame env
where
env :: Y GameEnvironment
Expand Down
31 changes: 0 additions & 31 deletions exercises/chapter12/bower.json

This file was deleted.

16 changes: 16 additions & 0 deletions exercises/chapter12/psc-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "chapter12",
"set": "psc-0.12.0",
"source": "https://github.com/purescript/package-sets.git",
"depends": [
"prelude",
"console",
"functions",
"lists",
"math",
"parallel",
"refs",
"strings",
"transformers"
]
}
36 changes: 17 additions & 19 deletions exercises/chapter12/src/Files.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,48 @@ module Files where
import Prelude

import Control.Monad.Cont.Trans (ContT(..))
import Control.Monad.Eff (kind Effect, Eff)
import Effect (Effect)
import Control.Monad.Except.Trans (ExceptT(..))
import Data.Either (Either(..))
import Data.Function.Uncurried (Fn4, Fn3, runFn4, runFn3)
import Types (Async)

foreign import data FS :: Effect

type ErrorCode = String

type FilePath = String

foreign import readFileImpl ::
forall eff. Fn3 FilePath
(String -> Eff (fs :: FS | eff) Unit)
(ErrorCode -> Eff (fs :: FS | eff) Unit)
(Eff (fs :: FS | eff) Unit)
Fn3 FilePath
(String -> Effect Unit)
(ErrorCode -> Effect Unit)
(Effect Unit)

foreign import writeFileImpl ::
forall eff. Fn4 FilePath
Fn4 FilePath
String
(Eff (fs :: FS | eff) Unit)
(ErrorCode -> Eff (fs :: FS | eff) Unit)
(Eff (fs :: FS | eff) Unit)
(Effect Unit)
(ErrorCode -> Effect Unit)
(Effect Unit)

readFile :: forall eff. FilePath -> (Either ErrorCode String -> Eff (fs :: FS | eff) Unit) -> Eff (fs :: FS | eff) Unit
readFile :: FilePath -> (Either ErrorCode String -> Effect Unit) -> Effect Unit
readFile path k = runFn3 readFileImpl path (k <<< Right) (k <<< Left)

writeFile :: forall eff. FilePath -> String -> (Either ErrorCode Unit -> Eff (fs :: FS | eff) Unit) -> Eff (fs :: FS | eff) Unit
writeFile :: FilePath -> String -> (Either ErrorCode Unit -> Effect Unit) -> Effect Unit
writeFile path text k = runFn4 writeFileImpl path text (k $ Right unit) (k <<< Left)

readFileCont :: forall eff. FilePath -> Async (fs :: FS | eff) (Either ErrorCode String)
readFileCont :: FilePath -> Async (Either ErrorCode String)
readFileCont path = ContT $ readFile path

writeFileCont :: forall eff. FilePath -> String -> Async (fs :: FS | eff) (Either ErrorCode Unit)
writeFileCont :: FilePath -> String -> Async (Either ErrorCode Unit)
writeFileCont path text = ContT $ writeFile path text

readFileContEx :: forall eff. FilePath -> ExceptT ErrorCode (Async (fs :: FS | eff)) String
readFileContEx :: FilePath -> ExceptT ErrorCode Async String
readFileContEx path = ExceptT $ readFileCont path

writeFileContEx :: forall eff. FilePath -> String -> ExceptT ErrorCode (Async (fs :: FS | eff)) Unit
writeFileContEx :: FilePath -> String -> ExceptT ErrorCode Async Unit
writeFileContEx path text = ExceptT $ writeFileCont path text

copyFileContEx :: forall eff. FilePath -> FilePath -> ExceptT ErrorCode (Async (fs :: FS | eff)) Unit
copyFileContEx :: FilePath -> FilePath -> ExceptT ErrorCode Async Unit
copyFileContEx src dest = do
content <- readFileContEx src
writeFileContEx dest content
writeFileContEx dest content
14 changes: 6 additions & 8 deletions exercises/chapter12/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ module Main where
import Prelude

import Control.Monad.Cont.Trans (runContT)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log, error)
import Effect (Effect)
import Effect.Console (log, error)
import Control.Monad.Trans.Class (lift)
import Data.Either (either)
import Network.HTTP.Client (HTTP, get)
import Network.HTTP.Client (get)
import Types (Async)

main :: Eff ( http :: HTTP
, console :: CONSOLE
) Unit
main :: Effect Unit
main = async do
response <- get "http://purescript.org"
lift (either error log response)
where
async :: forall eff. Async eff Unit -> Eff eff Unit
async = flip runContT pure
async :: Async Unit -> Effect Unit
async = flip runContT pure
Loading

0 comments on commit 5a04be3

Please sign in to comment.