Is there a Monad which collects results and `mappend`s them?

Chris Wohlert

I have the following pattern of a Reader with a Semigroup Element:

runFunction :: Reader Env Element
runFunction = do
  a <- getA
  b <- getB
  c <- getC
  return $ a <> b <> c

Where getA :: Reader Env Element.

Is there a way to:

runFunction = do
  getA
  getB
  getC

I feel like I see this pattern alot, where I imperatively chain monadic calls, and they get turned into a single element at the end.

Note: I don't want to do getA >>= getB >>= getC since getB isn't :: Element -> Reader Env Element

It feels like a State Monad, that automatically modifies state with <>, but I don't know.

Working with monadic code is still quite fresh to me.

4castle

The WriterT monad transformer can be used to build up a monoidal value with each action.

You can use lift to wrap your Reader actions into the WriterT monad transformer, then move the result of each action into the monad's environment by using tell, sequence the actions, and then unwrap the result back to a single Reader action using execWriterT.

Here is what I mean:

import Control.Monad.Writer

wrap :: (Monad m, Monoid w) => m w -> WriterT w m ()
wrap m = lift m >>= tell

unwrap :: (Monad m) => WriterT w m a -> m w
unwrap = execWriterT

runFunction :: Reader Env Element
runFunction = unwrap $ do
    wrap getA
    wrap getB
    wrap getC

As you can see, this generalizes to any monad, not just Reader.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why does the "which" command give duplicate results?

分類Dev

Columns of My Unpivot Query Results not Where I Want Them to Be

分類Dev

pgAdmin III writes files with junk in them, which causes PSQL to fail

分類Dev

Check the details of columns which have attributes applied to them

分類Dev

A monad of tuples (monad, state)?

分類Dev

Unable to understand how State Monad get it's state in this code

分類Dev

How to make Koen Claessen's concurrency monad with pure variables?

分類Dev

Keep which(..., arr.ind = TRUE) results that connect

分類Dev

In MongoDB, how to fetch recent 30 results from each id and group them by id

分類Dev

Can I create a generic @Query that detects which parameters from URL are not null and make a search only by them with Spring?

分類Dev

How to check which button was pressed when using the same callback or event listener for all of them

分類Dev

Get ComboBox selected value from a list which has two types of objects and the combobox is only showing one of them

分類Dev

How to restructure columns which some of them should have the same values in R

分類Dev

how to open multiple file from single file which is having list of files in python and how to do processing on them?

分類Dev

Which linux distro's package repositories are secure and which are not?

分類Dev

Which seeds have to be set where to realize 100% reproducibility of training results in tensorflow?

分類Dev

Why do we have instructions such as RDRAND instead of an I/O which would gives us similar results?

分類Dev

Retrieve files from SQL Server, display them with extension's icon and read them

分類Dev

What is Julia's equivalent of R's which?

分類Dev

What is indexed monad?

分類Dev

Scala Cats State Monad

分類Dev

What is monad analog in Java?

分類Dev

Scala Cats State Monad

分類Dev

Stricter Strict State Monad

分類Dev

Visualizing the Free Monad

分類Dev

Scala and State Monad

分類Dev

Logging using the free monad

分類Dev

Understanding forall in Monad '>>=' function?

分類Dev

How to zoom a monad transformer?

Related 関連記事

  1. 1

    Why does the "which" command give duplicate results?

  2. 2

    Columns of My Unpivot Query Results not Where I Want Them to Be

  3. 3

    pgAdmin III writes files with junk in them, which causes PSQL to fail

  4. 4

    Check the details of columns which have attributes applied to them

  5. 5

    A monad of tuples (monad, state)?

  6. 6

    Unable to understand how State Monad get it's state in this code

  7. 7

    How to make Koen Claessen's concurrency monad with pure variables?

  8. 8

    Keep which(..., arr.ind = TRUE) results that connect

  9. 9

    In MongoDB, how to fetch recent 30 results from each id and group them by id

  10. 10

    Can I create a generic @Query that detects which parameters from URL are not null and make a search only by them with Spring?

  11. 11

    How to check which button was pressed when using the same callback or event listener for all of them

  12. 12

    Get ComboBox selected value from a list which has two types of objects and the combobox is only showing one of them

  13. 13

    How to restructure columns which some of them should have the same values in R

  14. 14

    how to open multiple file from single file which is having list of files in python and how to do processing on them?

  15. 15

    Which linux distro's package repositories are secure and which are not?

  16. 16

    Which seeds have to be set where to realize 100% reproducibility of training results in tensorflow?

  17. 17

    Why do we have instructions such as RDRAND instead of an I/O which would gives us similar results?

  18. 18

    Retrieve files from SQL Server, display them with extension's icon and read them

  19. 19

    What is Julia's equivalent of R's which?

  20. 20

    What is indexed monad?

  21. 21

    Scala Cats State Monad

  22. 22

    What is monad analog in Java?

  23. 23

    Scala Cats State Monad

  24. 24

    Stricter Strict State Monad

  25. 25

    Visualizing the Free Monad

  26. 26

    Scala and State Monad

  27. 27

    Logging using the free monad

  28. 28

    Understanding forall in Monad '>>=' function?

  29. 29

    How to zoom a monad transformer?

ホットタグ

アーカイブ