Skip to content

fix runListT #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ListT examples
  • Loading branch information
matthewleon committed Mar 29, 2017
commit 3ba6750b674369c022636bdc066fc1844acf59e8
38 changes: 38 additions & 0 deletions test/Example/List.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Example.List where

import Prelude
import Data.Array as A
import Data.List.Lazy as L
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.List.Trans (ListT, runListT)
import Control.MonadZero (guard)

-- based on http://hackage.haskell.org/package/list-transformer
logList :: forall eff.
ListT (Eff (console :: CONSOLE | eff)) String
-> Eff (console :: CONSOLE | eff) Unit
logList l = runListT do
liftEff $ log "logging listT"
str <- l
liftEff $ log str

-- based on https://wiki.haskell.org/ListT_done_right#Sum_of_squares
sumSqrs :: forall eff.
Int
-> ListT (Eff (console :: CONSOLE | eff)) Unit
sumSqrs n = do
let
nats = L.iterate (add one) zero -- lazy infinite list
squares = L.toUnfoldable <<< L.takeWhile (_ <= n) $ map (\x -> x * x) nats
x <- squares
y <- squares
liftEff $ log ("<" <> show x <> "," <> show y <> ">")
guard $ x + y == n
liftEff $ log "Sum of squares."

main :: forall eff. Eff (console :: CONSOLE | eff) Unit
main = do
logList $ A.toUnfoldable ["one", "two", "three"]
runListT $ sumSqrs 10
2 changes: 2 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Example.State as State
import Example.StateEff as StateEff
import Example.Writer as Writer
import Example.RWS as RWS
import Example.List as List

main :: Eff (console :: CONSOLE) Unit
main = do
Expand All @@ -20,3 +21,4 @@ main = do
StateEff.main
Writer.main
RWS.main
List.main