Comments (5)
Thanks for the suggestion. How often do you think these patterns occur in practice? Would be good to get a sense of how useful they would be.
from extra.
Thanks for the reply! For me it's when I'm traversing a list and zip it up to return a map:
- traverse a list that returns
[Maybe b]
- zip the previous list with
[a]
- convert
[(a, Maybe B)]
to[Maybe (a, b)]
- keep only the available values:
[(a, b)]
For example:
newtype UserId = UserId Int deriving (Eq, Ord)
data User
getUserById :: UserId -> m (Maybe User)
getUserById = undefined
getUsersById :: Monad m => [UserId] -> m (Map UserId User)
getUsersById userIds =
fromList . mapMaybe maybeSnd . zip userIds <$> traverse getUserById userIds
from extra.
Would be interesting to get a sense of how often this occurs. One way would be to add an hlint rule and run it over a reasonable volume of Haskell code to show how often this shows up (or maybe just grep for it). Another would be to ask on Twitter/Reddit or similar if people have this issue.
from extra.
strong :: Functor f => (a, f b) -> f (a, b)
strong (a, fb) = fmap (a,) fb
is common enough pattern it has a name (strong functors), and all haskell Functor
s are strong in respect to products. But you would write it as uncurried in Haskell
strong :: Functor f => a -> f b -> f (a, b)
strong a fb = fmap (a,) fb
and then
getUsersById :: Monad m => [UserId] -> m (Map UserId User)
getUsersById userIds =
fromList . mapMaybe maybeSnd . zip userIds <$> traverse getUserById userIds
I'd write that as
getUsersById :: Monad m => [UserId] -> m (Map UserId User)
getUsersById usedIds = fromList . catMaybes <$> forM userIds $ \usedId ->
user <- getUsersById
return (strong userId user)
but here it's easy to inline fmap (userId,) user
as well.
The fmap (something,)
is not uncommon on Hackage: https://hackage-search.serokell.io/?q=fmap+%5C%28%5Ba-zA-Z%5D%2B%2C%5C%29, but I'm not sure it actually would benefit from a name
\xs -> catMaybes <$> traverse f xs
is generalized to witherM
from witherable
. (I'd appreaciate a good name for flipped variant, like we have mapM
and forM
or traverse
and for
) (FWIW: similar request #87 for foldMap
)
from extra.
Related Issues (20)
- suggestion: mwhen :: Monoid a => Bool -> a -> a HOT 3
- fourth / fifth tuples? HOT 1
- GHC9.8: ambiguous reference to unzip HOT 1
- `Data.Foldable.Extra` not showing up on Hoogle HOT 2
- Add flip const HOT 9
- Add withEnvironment and withModifiedEnvironment HOT 1
- Use GHC's definition of spanEnd? HOT 3
- Documentation of `zipFrom` is misleading HOT 1
- `concatUnzip` is just a special case of folding monoids HOT 5
- Consider adding strict Var primitives HOT 2
- proposal to add filesWithExtension HOT 3
- Add (!?) function for lists HOT 3
- Make `notNull` for any `Foldable` HOT 3
- Add `flip foldMap` HOT 5
- Build failures with GHC >= 9 in v1.5 to v1.7.6 HOT 2
- `groupOnKey :: Eq b => (a -> k) -> [a] -> [(k, [a])]` HOT 1
- NonEmpty input in Data.List.Extra.repeatedly HOT 2
- third3 isn't as lazy as second from Data.Bifunctor HOT 1
- Make replace not be partial HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from extra.