<commit_before>-- | A newtype over `Aff` that provides `Applicative` instances that run in
-- | parallel. This is useful, for example, if you want to run a whole bunch
-- | of AJAX requests at the same time, rather than sequentially.
module Control.Monad.Aff.Par
  ( Par(..)
  , runPar
  ) where

import Prelude

import Control.Alt (Alt)
import Control.Alternative (Alternative)
import Control.Monad.Aff (attempt, cancelWith, forkAff)
import Control.Monad.Aff.AVar (AffAVar(), makeVar, makeVar', takeVar, putVar, killVar)
import Control.Plus (Plus, empty)

import Data.Either (either)
import Data.Monoid (Monoid, mempty)

newtype Par e a = Par (AffAVar e a)

-- | Extracts the `Aff` from the `Par`.
runPar :: forall e a. Par e a -> AffAVar e a
runPar (Par aff) = aff

instance semigroupPar :: (Semigroup a) => Semigroup (Par e a) where
  append a b = append <$> a <*> b

instance monoidPar :: (Monoid a) => Monoid (Par e a) where
  mempty = pure mempty

instance functorPar :: Functor (Par e) where
  map f (Par fa) = Par (f <$> fa)

instance applyPar :: Apply (Par e) where
  apply (Par ff) (Par fa) = Par do
    vf <- makeVar
    va <- makeVar
    c1 <- forkAff (ff >>= putVar vf)
    c2 <- forkAff (fa >>= putVar va)
    (takeVar vf <*> takeVar va) `cancelWith` (c1 <> c2)

instance applicativePar :: Applicative (Par e) where
  pure v = Par (pure v)

-- | Returns the first value, or the first error if both error.
instance altPar :: Alt (Par e) where
  alt (Par a1) (Par a2) =
    let maybeKill va ve err =
      do e <- takeVar ve
         if e == 1 then killVar va err else return unit
         putVar ve (e + 1)
    in Par do
      va <- makeVar     -- the `a` value
      ve <- makeVar' 0  -- the error count (starts at 0)
      c1 <- forkAff $ attempt a1 >>= either (maybeKill va ve) (putVar va)
      c2 <- forkAff $ attempt a2 >>= either (maybeKill va ve) (putVar va)
      (takeVar va) `cancelWith` (c1 <> c2)

instance plusPar :: Plus (Par e) where
  empty = Par empty
<commit_msg>Fix indentation failing when compiling with HEAD<commit_after>-- | A newtype over `Aff` that provides `Applicative` instances that run in
-- | parallel. This is useful, for example, if you want to run a whole bunch
-- | of AJAX requests at the same time, rather than sequentially.
module Control.Monad.Aff.Par
  ( Par(..)
  , runPar
  ) where

import Prelude

import Control.Alt (Alt)
import Control.Alternative (Alternative)
import Control.Monad.Aff (attempt, cancelWith, forkAff)
import Control.Monad.Aff.AVar (AffAVar(), makeVar, makeVar', takeVar, putVar, killVar)
import Control.Plus (Plus, empty)

import Data.Either (either)
import Data.Monoid (Monoid, mempty)

newtype Par e a = Par (AffAVar e a)

-- | Extracts the `Aff` from the `Par`.
runPar :: forall e a. Par e a -> AffAVar e a
runPar (Par aff) = aff

instance semigroupPar :: (Semigroup a) => Semigroup (Par e a) where
  append a b = append <$> a <*> b

instance monoidPar :: (Monoid a) => Monoid (Par e a) where
  mempty = pure mempty

instance functorPar :: Functor (Par e) where
  map f (Par fa) = Par (f <$> fa)

instance applyPar :: Apply (Par e) where
  apply (Par ff) (Par fa) = Par do
    vf <- makeVar
    va <- makeVar
    c1 <- forkAff (ff >>= putVar vf)
    c2 <- forkAff (fa >>= putVar va)
    (takeVar vf <*> takeVar va) `cancelWith` (c1 <> c2)

instance applicativePar :: Applicative (Par e) where
  pure v = Par (pure v)

-- | Returns the first value, or the first error if both error.
instance altPar :: Alt (Par e) where
  alt (Par a1) (Par a2) =
    let maybeKill va ve err = do
         e <- takeVar ve
         if e == 1 then killVar va err else return unit
         putVar ve (e + 1)
    in Par do
      va <- makeVar     -- the `a` value
      ve <- makeVar' 0  -- the error count (starts at 0)
      c1 <- forkAff $ attempt a1 >>= either (maybeKill va ve) (putVar va)
      c2 <- forkAff $ attempt a2 >>= either (maybeKill va ve) (putVar va)
      (takeVar va) `cancelWith` (c1 <> c2)

instance plusPar :: Plus (Par e) where
  empty = Par empty

{'fraction_non_alphanumeric': 0.1166832174776564, 'fraction_numerical': 0.007944389275074478, 'mean_word_length': 2.965551181102362, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3720869', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1353, 'n_words': 570}