module ClassyPrelude
(
module CorePrelude
, undefined
, (++)
, Semigroup (..)
, WrappedMonoid
, module Data.Functor
, module Control.Applicative
, (<&&>)
, (<||>)
, module Control.Monad
, module Control.Monad.Trans.Unlift
, whenM
, unlessM
, module Control.Concurrent.MVar.Lifted
, module Control.Concurrent.Chan.Lifted
, module Control.Concurrent.STM
, atomically
, alwaysSTM
, alwaysSucceedsSTM
, retrySTM
, orElseSTM
, checkSTM
, module Data.IORef.Lifted
, module Data.Mutable
, primToPrim
, primToIO
, primToST
, module Data.Primitive.MutVar
, trace
, traceShow
, traceId
, traceM
, traceShowId
, traceShowM
, assert
, module Data.Time
, defaultTimeLocale
, Generic
, Identity (..)
, MonadReader
, ask
, asks
, ReaderT (..)
, Reader
, module Data.Foldable
, module Data.Traversable
, module Data.Bifunctor
, module Data.MonoTraversable
, module Data.MonoTraversable.Unprefixed
, module Data.Sequences
, module Data.Containers
, module Data.Builder
, module Data.NonNull
, toByteVector
, fromByteVector
, Handle
, stdin
, stdout
, stderr
, module Control.Concurrent.Lifted
, yieldThread
, module Control.Concurrent.Async
, module Control.Concurrent.Async.Lifted.Safe
, waitAsync
, pollAsync
, waitCatchAsync
, cancel
, cancelWith
, linkAsync
, link2Async
, map
, readMay
, zip, zip3, zip4, zip5, zip6, zip7
, unzip, unzip3, unzip4, unzip5, unzip6, unzip7
, zipWith, zipWith3, zipWith4, zipWith5, zipWith6, zipWith7
, hashNub
, ordNub
, ordNubBy
, sortWith
, Prelude.repeat
, (\\)
, intersect
, Show (..)
, tshow
, tlshow
, charToLower
, charToUpper
, IOData (..)
, print
, hClose
, DList
, asDList
, applyDList
, module Control.Exception.Safe
, module Control.DeepSeq
, asByteString
, asLByteString
, asHashMap
, asHashSet
, asText
, asLText
, asList
, asMap
, asIntMap
, asMaybe
, asSet
, asIntSet
, asVector
, asUVector
, asSVector
, asString
) where
import qualified Prelude
import Control.Applicative ((<**>),liftA,liftA2,liftA3,Alternative (..), optional)
import Data.Functor
import Control.Exception (assert)
import Control.Exception.Safe
import Control.DeepSeq (deepseq, ($!!), force, NFData (..))
import Control.Monad (when, unless, void, liftM, ap, forever, join, replicateM_, guard, MonadPlus (..), (=<<), (>=>), (<=<), liftM2, liftM3, liftM4, liftM5)
import Control.Concurrent.Lifted hiding (yield, throwTo)
import qualified Control.Concurrent.Lifted as Conc (yield)
import Control.Concurrent.MVar.Lifted
import Control.Concurrent.Chan.Lifted
import Control.Concurrent.STM hiding (atomically, always, alwaysSucceeds, retry, orElse, check)
import qualified Control.Concurrent.STM as STM
import Data.IORef.Lifted
import Data.Mutable
import Data.Traversable (Traversable (..), for, forM)
import Data.Foldable (Foldable)
import Data.IOData (IOData (..))
import Control.Monad.Base
import Control.Monad.Trans.Unlift
import qualified Control.Concurrent.Async as Async
import Control.Concurrent.Async
( Async
, waitSTM, pollSTM, waitCatchSTM
)
import Control.Concurrent.Async.Lifted.Safe
( Pure, Forall
, async, asyncBound, asyncOn, asyncWithUnmask, asyncOnWithUnmask
, withAsync, withAsyncBound, withAsyncOn, withAsyncWithUnmask, withAsyncOnWithUnmask
, asyncThreadId
, race, race_, concurrently, mapConcurrently, Concurrently (..)
)
import Data.Vector.Instances ()
import CorePrelude hiding
( print, undefined, (<>), catMaybes, first, second
, try, throwIO, onException, mask, mask_
, handle, finally, catch, bracket, bracket_, bracketOnError
, catchIOError
, uninterruptibleMask, uninterruptibleMask_
#if MIN_VERSION_safe_exceptions(0,1,4)
, catchJust, handleJust, tryJust
#endif
)
import Data.ChunkedZip
import qualified Data.Char as Char
import Data.Sequences
import Data.MonoTraversable
import Data.MonoTraversable.Unprefixed
import Data.MonoTraversable.Instances ()
import Data.Containers
import Data.Builder
import Data.NonNull
import Data.ByteString.Internal (ByteString (PS))
import Data.Vector.Storable (unsafeToForeignPtr, unsafeFromForeignPtr)
import System.IO (Handle, stdin, stdout, stderr, hClose)
import Debug.Trace (trace, traceShow)
import Data.Semigroup (Semigroup (..), WrappedMonoid (..))
import Prelude (Show (..))
import Data.Time
( UTCTime (..)
, Day (..)
, toGregorian
, fromGregorian
, formatTime
, parseTime
, getCurrentTime
)
import Data.Time.Locale.Compat (defaultTimeLocale)
import qualified Data.Set as Set
import qualified Data.Map as Map
import qualified Data.HashSet as HashSet
import GHC.Generics (Generic)
import Control.Monad.Primitive (primToPrim, primToIO, primToST)
import Data.Primitive.MutVar
import Data.Functor.Identity (Identity (..))
import Control.Monad.Reader (MonadReader, ask, asks, ReaderT (..), Reader)
import Data.Bifunctor
import Data.DList (DList)
import qualified Data.DList as DList
tshow :: Show a => a -> Text
tshow = fromList . Prelude.show
tlshow :: Show a => a -> LText
tlshow = fromList . Prelude.show
charToLower :: Char -> Char
charToLower = Char.toLower
charToUpper :: Char -> Char
charToUpper = Char.toUpper
readMay :: (Element c ~ Char, MonoFoldable c, Read a) => c -> Maybe a
readMay a =
case [x | (x, t) <- Prelude.reads (otoList a :: String), onull t] of
[x] -> Just x
_ -> Nothing
map :: Functor f => (a -> b) -> f a -> f b
map = fmap
infixr 5 ++
(++) :: Monoid m => m -> m -> m
(++) = mappend
infixl 9 \\
(\\) :: SetContainer a => a -> a -> a
(\\) = difference
intersect :: SetContainer a => a -> a -> a
intersect = intersection
asByteString :: ByteString -> ByteString
asByteString = id
asLByteString :: LByteString -> LByteString
asLByteString = id
asHashMap :: HashMap k v -> HashMap k v
asHashMap = id
asHashSet :: HashSet a -> HashSet a
asHashSet = id
asText :: Text -> Text
asText = id
asLText :: LText -> LText
asLText = id
asList :: [a] -> [a]
asList = id
asMap :: Map k v -> Map k v
asMap = id
asIntMap :: IntMap v -> IntMap v
asIntMap = id
asMaybe :: Maybe a -> Maybe a
asMaybe = id
asSet :: Set a -> Set a
asSet = id
asIntSet :: IntSet -> IntSet
asIntSet = id
asVector :: Vector a -> Vector a
asVector = id
asUVector :: UVector a -> UVector a
asUVector = id
asSVector :: SVector a -> SVector a
asSVector = id
asString :: [Char] -> [Char]
asString = id
print :: (Show a, MonadIO m) => a -> m ()
print = liftIO . Prelude.print
sortWith :: (Ord a, IsSequence c) => (Element c -> a) -> c -> c
sortWith f = sortBy $ comparing f
undefined :: a
undefined = error "ClassyPrelude.undefined"
traceId :: String -> String
traceId a = trace a a
traceM :: (Monad m) => String -> m ()
traceM string = trace string $ return ()
traceShowId :: (Show a) => a -> a
traceShowId a = trace (show a) a
traceShowM :: (Show a, Monad m) => a -> m ()
traceShowM = traceM . show
yieldThread :: MonadBase IO m => m ()
yieldThread = Conc.yield
hashNub :: (Hashable a, Eq a) => [a] -> [a]
hashNub = go HashSet.empty
where
go _ [] = []
go s (x:xs) | x `HashSet.member` s = go s xs
| otherwise = x : go (HashSet.insert x s) xs
ordNub :: (Ord a) => [a] -> [a]
ordNub = go Set.empty
where
go _ [] = []
go s (x:xs) | x `Set.member` s = go s xs
| otherwise = x : go (Set.insert x s) xs
ordNubBy :: (Ord b) => (a -> b) -> (a -> a -> Bool) -> [a] -> [a]
ordNubBy p f = go Map.empty
where
go _ [] = []
go m (x:xs) = let b = p x in case b `Map.lookup` m of
Nothing -> x : go (Map.insert b [x] m) xs
Just bucket
| elem_by f x bucket -> go m xs
| otherwise -> x : go (Map.insert b (x:bucket) m) xs
elem_by :: (a -> a -> Bool) -> a -> [a] -> Bool
elem_by _ _ [] = False
elem_by eq y (x:xs) = y `eq` x || elem_by eq y xs
atomically :: MonadIO m => STM a -> m a
atomically = liftIO . STM.atomically
retrySTM :: STM a
retrySTM = STM.retry
alwaysSTM :: STM Bool -> STM ()
alwaysSTM = STM.always
alwaysSucceedsSTM :: STM a -> STM ()
alwaysSucceedsSTM = STM.alwaysSucceeds
orElseSTM :: STM a -> STM a -> STM a
orElseSTM = STM.orElse
checkSTM :: Bool -> STM ()
checkSTM = STM.check
whenM :: Monad m => m Bool -> m () -> m ()
whenM mbool action = mbool >>= flip when action
unlessM :: Monad m => m Bool -> m () -> m ()
unlessM mbool action = mbool >>= flip unless action
asDList :: DList a -> DList a
asDList = id
applyDList :: DList a -> [a] -> [a]
applyDList = DList.apply
infixr 3 <&&>
(<&&>) :: Applicative a => a Bool -> a Bool -> a Bool
(<&&>) = liftA2 (&&)
infixr 2 <||>
(<||>) :: Applicative a => a Bool -> a Bool -> a Bool
(<||>) = liftA2 (||)
toByteVector :: ByteString -> SVector Word8
toByteVector (PS fptr offset idx) = unsafeFromForeignPtr fptr offset idx
fromByteVector :: SVector Word8 -> ByteString
fromByteVector v =
PS fptr offset idx
where
(fptr, offset, idx) = unsafeToForeignPtr v
waitAsync :: MonadIO m => Async a -> m a
waitAsync = atomically . waitSTM
pollAsync :: MonadIO m => Async a -> m (Maybe (Either SomeException a))
pollAsync = atomically . pollSTM
waitCatchAsync :: MonadIO m => Async a -> m (Either SomeException a)
waitCatchAsync = atomically . waitCatchSTM
cancel :: MonadIO m => Async a -> m ()
cancel = liftIO . Async.cancel
cancelWith :: (MonadIO m, Exception e) => Async a -> e -> m ()
cancelWith a e = liftIO (Async.cancelWith a (toAsyncException e))
linkAsync :: MonadIO m => Async a -> m ()
linkAsync = liftIO . Async.link
link2Async :: MonadIO m => Async a -> Async b -> m ()
link2Async a = liftIO . Async.link2 a