Skip to content

mapConcurrently / forConcurrently + lenses #76

Open

Description

@Icelandjack

async has some functions that can be parameterized by optics from lens, is this something that is worth adding.

mapConcurrently :: Traversable t => (a -> IO b) -> t a -> IO (t b)

forConcurrently :: Traversable t => t a -> (a -> IO b)-> IO (t b)

mapConcurrently_ :: Foldable f => (a -> IO b) -> f a -> IO ()

forConcurrently_ :: Foldable f => f a -> (a -> IO b) -> IO ()

can be parameterized by optics from lens

mapConcurrentlyOf :: LensLike Concurrently s t a b -> (a -> IO b) -> (s -> IO t)
mapConcurrentlyOf lens = flip (forConcurrentlyOf lens)

forConcurrentlyOf :: forall s t a b. LensLike Concurrently s t a b -> s -> (a -> IO b) -> IO t
forConcurrentlyOf = coerce (forOf @IO @s @t @a @b)

mapConcurrentlyOf_ :: forall s a r. Getting (Traversed r Concurrently) s a -> (a -> IO r) -> (s -> IO ())
mapConcurrentlyOf_ lens = flip (forConcurrentlyOf_ lens)

forConcurrentlyOf_ :: forall s a r. Getting (Traversed r Concurrently) s a -> s -> (a -> IO r) -> IO ()
forConcurrentlyOf_ = coerce (forOf_ @IO @r @s @a)

that run them concurrently

>> forConcurrentlyOf_ (replicated 10) 'a' print
'a''a''
'''aa'''a'
a''aaa'a'

'''
'

Activity

  1. Icelandjack commented on Jan 4, 2018

    @Icelandjack
    ContributorAuthor

    I'm thinking that a much better solution is to create an optic modifier, something that takes lens-like over IO-like and produces LensLike Concurrently.

    That way you can write

    forConcurrentlyOf  lens = forOf  (asConcurrent lens)
    forConcurrentlyOf_ lens = forOf_ (asConcurrent lens)
  2. simonmar commented on Jan 8, 2018

    @simonmar
    Owner

    I'm not a heavy lens user so I have no strong feelings on this, but I'd rather not take a dependency on lens. Can this be added in a separate async-lens package?

  3. treeowl commented on Mar 15, 2023

    @treeowl
    Contributor

    @simonmar, lens defines

    type LensLike f s t a b = (a -> f b) -> s -> f t

    so mapConcurrentlyOf (which I'd call traverseConcurrentlyOf) and forConcurrentlyOf should be no problem. Traversed is a newtype defined in lens, and is frankly a bit of a mess, so I don't think the other two can be supported so easily.

  4. simonmar commented on Jun 8, 2023

    @simonmar
    Owner

    I'd rather not make async depend on lens. Could these be put into a separate package async-lens, say in a module Control.Concurrent.Async.Lens?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions