Skip to content

Instantly share code, notes, and snippets.

@rlucha
Last active January 19, 2022 13:40
Show Gist options
  • Select an option

  • Save rlucha/696ca604c9744ad11aff7d46b1706de7 to your computer and use it in GitHub Desktop.

Select an option

Save rlucha/696ca604c9744ad11aff7d46b1706de7 to your computer and use it in GitHub Desktop.

Revisions

  1. rlucha renamed this gist Jan 29, 2019. 1 changed file with 0 additions and 0 deletions.
  2. rlucha revised this gist Jan 22, 2019. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions Monad Reader Example Purescript
    Original file line number Diff line number Diff line change
    @@ -94,17 +94,15 @@ derive newtype instance applicativeAppC ∷ Applicative AppC
    derive newtype instance monadAskAppC :: MonadAsk Config AppC
    derive newtype instance monadEffectAppC :: MonadEffect AppC

    getConfigAppC :: forall m. MonadEffect m => MonadAsk Config m => m String
    getConfigAppC :: forall m. MonadEffect m => MonadAsk Config m => m Unit
    getConfigAppC = do
    config <- ask
    liftEffect (log "side effect!")
    pure (getConfig config)
    liftEffect (log (getConfig config))

    runAppC :: forall a. AppC a -> Config -> Effect a
    runAppC (AppC readerT) c = runReaderT readerT c

    main05 :: Effect Unit
    main05 = do
    r <- runAppC getConfigAppC config
    log r
    where config = (Config " ***** fooR ***** ")
    runAppC getConfigAppC config
    where config = (Config " Show me ")
  3. rlucha revised this gist Jan 22, 2019. 1 changed file with 40 additions and 3 deletions.
    43 changes: 40 additions & 3 deletions Monad Reader Example Purescript
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,9 @@ import Effect (Effect)
    import Effect.Class (class MonadEffect, liftEffect)
    import Effect.Console (log)


    ---------------------------------------------------------
    -- 01 no Reader
    ---------------------------------------------------------

    data Config = Config String

    @@ -20,10 +21,12 @@ main01 = do
    log $ getConfig config
    where config = (Config " ***** foo ***** ")


    ---------------------------------------------------------
    -- 02 with Reader
    -- getConfigR doesn't need to be passed config directly
    -- as it is within the context of the monad
    ---------------------------------------------------------

    getConfigR :: Reader Config String
    getConfigR = do
    config <- ask
    @@ -34,8 +37,10 @@ main02 = do
    log $ runReader getConfigR config
    where config = (Config " ***** fooR ***** ")


    ---------------------------------------------------------
    -- 03 with ReaderT
    -- Now we can combine a side effect while also asking for the config
    ---------------------------------------------------------

    getConfigRT :: ReaderT Config Effect String
    getConfigRT = do
    @@ -49,7 +54,10 @@ main03 = do
    log r
    where config = (Config " ***** fooR ***** ")

    ---------------------------------------------------------
    -- 04 with App as a newType
    -- We start creating now a App Monad with the behaviours we want
    ---------------------------------------------------------

    newtype App a = App (ReaderT Config Effect a)
    derive newtype instance bindApp ∷ Bind App
    @@ -71,3 +79,32 @@ main04 = do
    r <- runApp getConfigApp config
    log r
    where config = (Config " ***** fooR ***** ")


    ---------------------------------------------------------
    -- 05 with constrains
    -- Now we uncouple the functions from the app by
    -- declaring what are the characteristics of the monad
    -- where this function is going to execute, instead of the actual monad
    -- so that we can add new things without changing the App monad types everywhere
    ---------------------------------------------------------
    newtype AppC a = AppC (ReaderT Config Effect a)
    derive newtype instance bindAppC ∷ Bind AppC
    derive newtype instance applicativeAppC ∷ Applicative AppC
    derive newtype instance monadAskAppC :: MonadAsk Config AppC
    derive newtype instance monadEffectAppC :: MonadEffect AppC

    getConfigAppC :: forall m. MonadEffect m => MonadAsk Config m => m String
    getConfigAppC = do
    config <- ask
    liftEffect (log "side effect!")
    pure (getConfig config)

    runAppC :: forall a. AppC a -> Config -> Effect a
    runAppC (AppC readerT) c = runReaderT readerT c

    main05 :: Effect Unit
    main05 = do
    r <- runAppC getConfigAppC config
    log r
    where config = (Config " ***** fooR ***** ")
  4. rlucha revised this gist Jan 22, 2019. 1 changed file with 0 additions and 44 deletions.
    44 changes: 0 additions & 44 deletions Monad Reader Example Purescript
    Original file line number Diff line number Diff line change
    @@ -7,50 +7,6 @@ import Effect (Effect)
    import Effect.Class (class MonadEffect, liftEffect)
    import Effect.Console (log)

    -- data Config = Config String

    -- -- ReaderT (r -> m a)
    -- newtype App a = ReaderT Config (Effect a)

    -- -- I understand this
    -- mylog :: forall m. MonadAsk m => MonadEffect Config m => m Unit
    -- mylog = do
    -- Config c <- ask
    -- liftEffect (log c)


    -- main :: Effect Unit
    -- main = runReaderT mylog (Config "rob")



    -- runReaderT fn env
    -- fn gets ask capabilities


    -- type maybeTuple a = Maybe (String, a)

    -- myapp :: maybeTuple Int

    -- ###########################################################

    -- just Reader

    -- data Config = Config String

    -- iconfig :: Reader Config String
    -- iconfig = do
    -- r <- ask
    -- pure (Config r)

    -- getConfig c = runReader c iconfig

    -- main :: Effect Unit
    -- main = do
    -- c <- getConfig (Config "foo")
    -- log c

    -- ###########################################################

    -- 01 no Reader

  5. rlucha revised this gist Jan 22, 2019. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Monad Reader Example Purescript
    Original file line number Diff line number Diff line change
    @@ -94,8 +94,6 @@ main03 = do
    where config = (Config " ***** fooR ***** ")

    -- 04 with App as a newType
    -- we'll get No type class instance was found for
    -- Control.Bind.Bind App because our newtype is not a Monad?

    newtype App a = App (ReaderT Config Effect a)
    derive newtype instance bindApp ∷ Bind App
  6. rlucha revised this gist Jan 22, 2019. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions Monad Reader Example Purescript
    Original file line number Diff line number Diff line change
    @@ -98,6 +98,10 @@ main03 = do
    -- Control.Bind.Bind App because our newtype is not a Monad?

    newtype App a = App (ReaderT Config Effect a)
    derive newtype instance bindApp ∷ Bind App
    derive newtype instance applicativeApp ∷ Applicative App
    derive newtype instance monadAskApp :: MonadAsk Config App
    derive newtype instance monadEffectApp :: MonadEffect App

    getConfigApp :: App String
    getConfigApp = do
    @@ -108,8 +112,8 @@ getConfigApp = do
    runApp :: forall a. App a -> Config -> Effect a
    runApp (App readerT) c = runReaderT readerT c

    main03 :: Effect Unit
    main03 = do
    main04 :: Effect Unit
    main04 = do
    r <- runApp getConfigApp config
    log r
    where config = (Config " ***** fooR ***** ")
  7. rlucha revised this gist Jan 22, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Monad Reader Example Purescript
    Original file line number Diff line number Diff line change
    @@ -95,7 +95,7 @@ main03 = do

    -- 04 with App as a newType
    -- we'll get No type class instance was found for
    -- Control.Bind.Bind App as our newtype os not a Monad
    -- Control.Bind.Bind App because our newtype is not a Monad?

    newtype App a = App (ReaderT Config Effect a)

  8. rlucha renamed this gist Jan 22, 2019. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions Reader Example Purescript → Monad Reader Example Purescript
    Original file line number Diff line number Diff line change
    @@ -92,3 +92,24 @@ main03 = do
    r <- runReaderT getConfigRT config
    log r
    where config = (Config " ***** fooR ***** ")

    -- 04 with App as a newType
    -- we'll get No type class instance was found for
    -- Control.Bind.Bind App as our newtype os not a Monad

    newtype App a = App (ReaderT Config Effect a)

    getConfigApp :: App String
    getConfigApp = do
    config <- ask
    liftEffect (log "side effect!")
    pure (getConfig config)

    runApp :: forall a. App a -> Config -> Effect a
    runApp (App readerT) c = runReaderT readerT c

    main03 :: Effect Unit
    main03 = do
    r <- runApp getConfigApp config
    log r
    where config = (Config " ***** fooR ***** ")
  9. rlucha created this gist Jan 22, 2019.
    94 changes: 94 additions & 0 deletions Reader Example Purescript
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    module Main where

    import Prelude

    import Control.Monad.Reader (Reader, runReader, ReaderT(..), runReaderT, ask, class MonadAsk)
    import Effect (Effect)
    import Effect.Class (class MonadEffect, liftEffect)
    import Effect.Console (log)

    -- data Config = Config String

    -- -- ReaderT (r -> m a)
    -- newtype App a = ReaderT Config (Effect a)

    -- -- I understand this
    -- mylog :: forall m. MonadAsk m => MonadEffect Config m => m Unit
    -- mylog = do
    -- Config c <- ask
    -- liftEffect (log c)


    -- main :: Effect Unit
    -- main = runReaderT mylog (Config "rob")



    -- runReaderT fn env
    -- fn gets ask capabilities


    -- type maybeTuple a = Maybe (String, a)

    -- myapp :: maybeTuple Int

    -- ###########################################################

    -- just Reader

    -- data Config = Config String

    -- iconfig :: Reader Config String
    -- iconfig = do
    -- r <- ask
    -- pure (Config r)

    -- getConfig c = runReader c iconfig

    -- main :: Effect Unit
    -- main = do
    -- c <- getConfig (Config "foo")
    -- log c

    -- ###########################################################

    -- 01 no Reader

    data Config = Config String

    getConfig :: Config -> String
    getConfig (Config c) = c

    main01 :: Effect Unit
    main01 = do
    log $ getConfig config
    where config = (Config " ***** foo ***** ")


    -- 02 with Reader
    -- getConfigR doesn't need to be passed config directly
    -- as it is within the context of the monad
    getConfigR :: Reader Config String
    getConfigR = do
    config <- ask
    pure (getConfig config)

    main02 :: Effect Unit
    main02 = do
    log $ runReader getConfigR config
    where config = (Config " ***** fooR ***** ")


    -- 03 with ReaderT

    getConfigRT :: ReaderT Config Effect String
    getConfigRT = do
    config <- ask
    liftEffect (log "side effect!")
    pure (getConfig config)

    main03 :: Effect Unit
    main03 = do
    r <- runReaderT getConfigRT config
    log r
    where config = (Config " ***** fooR ***** ")