relude 0.7.0.0 → 1.0.0.0
raw patch · 64 files changed
+1054/−965 lines, 64 filesdep +criteriondep −gaugedep ~basedep ~bytestringdep ~ghc-primPVP ok
version bump matches the API change (PVP)
Dependencies added: criterion
Dependencies removed: gauge
Dependency ranges changed: base, bytestring, ghc-prim
API changes (from Hackage documentation)
- Relude.Base: boundedEnumFrom :: (Enum a, Bounded a) => a -> [a]
- Relude.Base: boundedEnumFromThen :: (Enum a, Bounded a) => a -> a -> [a]
- Relude.Base: class Bounded a
- Relude.Base: class Enum a
- Relude.Base: data Handle
- Relude.Base: enumFrom :: Enum a => a -> [a]
- Relude.Base: enumFromThen :: Enum a => a -> a -> [a]
- Relude.Base: enumFromThenTo :: Enum a => a -> a -> a -> [a]
- Relude.Base: enumFromTo :: Enum a => a -> a -> [a]
- Relude.Base: fromEnum :: Enum a => a -> Int
- Relude.Base: maxBound :: Bounded a => a
- Relude.Base: minBound :: Bounded a => a
- Relude.Base: pred :: Enum a => a -> a
- Relude.Base: stderr :: Handle
- Relude.Base: stdin :: Handle
- Relude.Base: stdout :: Handle
- Relude.Base: succ :: Enum a => a -> a
- Relude.Base: toEnum :: Enum a => Int -> a
- Relude.Base: withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
- Relude.Container.One: instance Data.Hashable.Class.Hashable a => Relude.Container.One.One (Data.HashSet.Base.HashSet a)
- Relude.Container.One: instance Data.Hashable.Class.Hashable k => Relude.Container.One.One (Data.HashMap.Base.HashMap k v)
- Relude.Extra.Enum: inverseMap :: forall a k. (Bounded a, Enum a, Ord k) => (a -> k) -> k -> Maybe a
- Relude.Extra.Enum: universe :: (Bounded a, Enum a) => [a]
- Relude.Extra.Enum: universeNonEmpty :: forall a. (Bounded a, Enum a, Eq a) => NonEmpty a
- Relude.Extra.Map: instance (GHC.Classes.Eq a, Data.Hashable.Class.Hashable a) => Relude.Extra.Map.StaticMap (Data.HashSet.Base.HashSet a)
- Relude.Extra.Map: instance (GHC.Classes.Eq k, Data.Hashable.Class.Hashable k) => Relude.Extra.Map.DynamicMap (Data.HashMap.Base.HashMap k v)
- Relude.Extra.Map: instance (GHC.Classes.Eq k, Data.Hashable.Class.Hashable k) => Relude.Extra.Map.StaticMap (Data.HashMap.Base.HashMap k v)
- Relude.Extra.Validation: Failure :: e -> Validation e a
- Relude.Extra.Validation: Success :: a -> Validation e a
- Relude.Extra.Validation: data Validation e a
- Relude.Extra.Validation: eitherToValidation :: Either e a -> Validation e a
- Relude.Extra.Validation: instance (GHC.Base.Semigroup e, GHC.Base.Monoid e) => GHC.Base.Alternative (Relude.Extra.Validation.Validation e)
- Relude.Extra.Validation: instance (GHC.Base.Semigroup e, GHC.Base.Semigroup a) => GHC.Base.Semigroup (Relude.Extra.Validation.Validation e a)
- Relude.Extra.Validation: instance (GHC.Base.Semigroup e, GHC.Base.Semigroup a, GHC.Base.Monoid a) => GHC.Base.Monoid (Relude.Extra.Validation.Validation e a)
- Relude.Extra.Validation: instance (GHC.Classes.Eq e, GHC.Classes.Eq a) => GHC.Classes.Eq (Relude.Extra.Validation.Validation e a)
- Relude.Extra.Validation: instance (GHC.Classes.Ord e, GHC.Classes.Ord a) => GHC.Classes.Ord (Relude.Extra.Validation.Validation e a)
- Relude.Extra.Validation: instance (GHC.Show.Show e, GHC.Show.Show a) => GHC.Show.Show (Relude.Extra.Validation.Validation e a)
- Relude.Extra.Validation: instance (Relude.Extra.Validation.NoValidationMonadError, GHC.Base.Semigroup e) => GHC.Base.Monad (Relude.Extra.Validation.Validation e)
- Relude.Extra.Validation: instance Data.Bifoldable.Bifoldable Relude.Extra.Validation.Validation
- Relude.Extra.Validation: instance Data.Bifunctor.Bifunctor Relude.Extra.Validation.Validation
- Relude.Extra.Validation: instance Data.Bitraversable.Bitraversable Relude.Extra.Validation.Validation
- Relude.Extra.Validation: instance Data.Foldable.Foldable (Relude.Extra.Validation.Validation e)
- Relude.Extra.Validation: instance Data.Traversable.Traversable (Relude.Extra.Validation.Validation e)
- Relude.Extra.Validation: instance GHC.Base.Functor (Relude.Extra.Validation.Validation e)
- Relude.Extra.Validation: instance GHC.Base.Semigroup e => GHC.Base.Applicative (Relude.Extra.Validation.Validation e)
- Relude.Extra.Validation: validationToEither :: Validation e a -> Either e a
+ Relude.Base: [getDown] :: Down a -> a
+ Relude.Container.One: instance Data.Hashable.Class.Hashable a => Relude.Container.One.One (Data.HashSet.Internal.HashSet a)
+ Relude.Container.One: instance Data.Hashable.Class.Hashable k => Relude.Container.One.One (Data.HashMap.Internal.HashMap k v)
+ Relude.Debug: traceShowWith :: Show b => (a -> b) -> a -> a
+ Relude.Enum: boundedEnumFrom :: (Enum a, Bounded a) => a -> [a]
+ Relude.Enum: boundedEnumFromThen :: (Enum a, Bounded a) => a -> a -> [a]
+ Relude.Enum: class Bounded a
+ Relude.Enum: class Enum a
+ Relude.Enum: enumFrom :: Enum a => a -> [a]
+ Relude.Enum: enumFromThen :: Enum a => a -> a -> [a]
+ Relude.Enum: enumFromThenTo :: Enum a => a -> a -> a -> [a]
+ Relude.Enum: enumFromTo :: Enum a => a -> a -> [a]
+ Relude.Enum: fromEnum :: Enum a => a -> Int
+ Relude.Enum: inverseMap :: forall a k. (Bounded a, Enum a, Ord k) => (a -> k) -> k -> Maybe a
+ Relude.Enum: maxBound :: Bounded a => a
+ Relude.Enum: minBound :: Bounded a => a
+ Relude.Enum: pred :: Enum a => a -> a
+ Relude.Enum: succ :: Enum a => a -> a
+ Relude.Enum: toEnum :: Enum a => Int -> a
+ Relude.Enum: universe :: (Bounded a, Enum a) => [a]
+ Relude.Enum: universeNonEmpty :: forall a. (Bounded a, Enum a) => NonEmpty a
+ Relude.Extra.Foldable: average :: forall a f. (Foldable f, Fractional a) => f a -> Maybe a
+ Relude.Extra.Foldable1: average1 :: forall a f. (Foldable1 f, Fractional a) => f a -> a
+ Relude.Extra.Foldable1: foldr1 :: Foldable1 f => (a -> b -> b) -> b -> f a -> b
+ Relude.Extra.Foldable1: maximumOn1 :: (Foldable1 f, Ord b) => (a -> b) -> f a -> a
+ Relude.Extra.Foldable1: minimumOn1 :: (Foldable1 f, Ord b) => (a -> b) -> f a -> a
+ Relude.Extra.Map: instance (GHC.Classes.Eq a, Data.Hashable.Class.Hashable a) => Relude.Extra.Map.StaticMap (Data.HashSet.Internal.HashSet a)
+ Relude.Extra.Map: instance (GHC.Classes.Eq k, Data.Hashable.Class.Hashable k) => Relude.Extra.Map.DynamicMap (Data.HashMap.Internal.HashMap k v)
+ Relude.Extra.Map: instance (GHC.Classes.Eq k, Data.Hashable.Class.Hashable k) => Relude.Extra.Map.StaticMap (Data.HashMap.Internal.HashMap k v)
+ Relude.Lifted.Env: getArgs :: MonadIO m => m [String]
+ Relude.Lifted.Env: lookupEnv :: MonadIO m => String -> m (Maybe String)
+ Relude.Lifted.File: readFile' :: MonadIO m => FilePath -> m String
+ Relude.Lifted.Handle: BlockBuffering :: Maybe Int -> BufferMode
+ Relude.Lifted.Handle: LineBuffering :: BufferMode
+ Relude.Lifted.Handle: NoBuffering :: BufferMode
+ Relude.Lifted.Handle: data BufferMode
+ Relude.Lifted.Handle: data Handle
+ Relude.Lifted.Handle: hFlush :: MonadIO m => Handle -> m ()
+ Relude.Lifted.Handle: hGetBuffering :: MonadIO m => Handle -> m BufferMode
+ Relude.Lifted.Handle: hIsEOF :: MonadIO m => Handle -> m Bool
+ Relude.Lifted.Handle: hSetBuffering :: MonadIO m => Handle -> BufferMode -> m ()
+ Relude.Lifted.Handle: stderr :: Handle
+ Relude.Lifted.Handle: stdin :: Handle
+ Relude.Lifted.Handle: stdout :: Handle
+ Relude.Lifted.Handle: withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
+ Relude.List: maybeAt :: Int -> [a] -> Maybe a
+ Relude.List: partitionWith :: (a -> Either b c) -> [a] -> ([b], [c])
+ Relude.List.Reexport: span :: (a -> Bool) -> [a] -> ([a], [a])
+ Relude.Monad: infinitely :: Applicative f => f a -> f Void
+ Relude.Nub: intNub :: [Int] -> [Int]
+ Relude.Nub: intNubOn :: (a -> Int) -> [a] -> [a]
+ Relude.Nub: ordNubOn :: forall b a. Ord b => (a -> b) -> [a] -> [a]
- Relude.Base: coerce :: Coercible a b => a -> b
+ Relude.Base: coerce :: forall {k :: RuntimeRep} (a :: TYPE k) (b :: TYPE k). Coercible a b => a -> b
- Relude.Base: infix 4 <
+ Relude.Base: infix 4 >=
- Relude.Base: seq :: a -> b -> b
+ Relude.Base: seq :: forall {r :: RuntimeRep} a (b :: TYPE r). a -> b -> b
- Relude.Container.One: -- | Type of single element of the structure.
+ Relude.Container.One: -- | Type of a single element of the structure.
- Relude.Function: (<<<) :: forall k cat (b :: k) (c :: k) (a :: k). Category cat => cat b c -> cat a b -> cat a c
+ Relude.Function: (<<<) :: forall {k} cat (b :: k) (c :: k) (a :: k). Category cat => cat b c -> cat a b -> cat a c
- Relude.Function: (>>>) :: forall k cat (a :: k) (b :: k) (c :: k). Category cat => cat a b -> cat b c -> cat a c
+ Relude.Function: (>>>) :: forall {k} cat (a :: k) (b :: k) (c :: k). Category cat => cat a b -> cat b c -> cat a c
- Relude.Functor.Reexport: contramap :: Contravariant f => (a -> b) -> f b -> f a
+ Relude.Functor.Reexport: contramap :: Contravariant f => (a' -> a) -> f a -> f a'
- Relude.Numeric: infixl 6 -
+ Relude.Numeric: infixl 6 +
- Relude.Numeric: infixl 7 `mod`
+ Relude.Numeric: infixl 7 `quot`
Files
- CHANGELOG.md +98/−0
- README.md +55/−18
- benchmark/Main.hs +37/−2
- relude.cabal +65/−42
- src/Relude.hs +10/−8
- src/Relude/Applicative.hs +17/−11
- src/Relude/Base.hs +2/−14
- src/Relude/Bool.hs +1/−1
- src/Relude/Bool/Guard.hs +27/−6
- src/Relude/Bool/Reexport.hs +1/−1
- src/Relude/Container.hs +1/−1
- src/Relude/Container/One.hs +3/−2
- src/Relude/Container/Reexport.hs +1/−1
- src/Relude/Debug.hs +87/−17
- src/Relude/DeepSeq.hs +4/−4
- src/Relude/Enum.hs +146/−0
- src/Relude/Exception.hs +4/−3
- src/Relude/Extra.hs +1/−12
- src/Relude/Extra/Bifunctor.hs +1/−1
- src/Relude/Extra/CallStack.hs +1/−1
- src/Relude/Extra/Enum.hs +4/−118
- src/Relude/Extra/Foldable.hs +28/−1
- src/Relude/Extra/Foldable1.hs +97/−2
- src/Relude/Extra/Group.hs +1/−1
- src/Relude/Extra/Lens.hs +1/−1
- src/Relude/Extra/Map.hs +1/−1
- src/Relude/Extra/Newtype.hs +1/−1
- src/Relude/Extra/Tuple.hs +7/−2
- src/Relude/Extra/Type.hs +1/−9
- src/Relude/Extra/Validation.hs +0/−432
- src/Relude/File.hs +1/−1
- src/Relude/Foldable.hs +1/−1
- src/Relude/Foldable/Fold.hs +10/−6
- src/Relude/Foldable/Reexport.hs +1/−5
- src/Relude/Function.hs +1/−1
- src/Relude/Functor.hs +1/−1
- src/Relude/Functor/Fmap.hs +7/−2
- src/Relude/Functor/Reexport.hs +1/−1
- src/Relude/Lifted.hs +13/−1
- src/Relude/Lifted/Concurrent.hs +1/−1
- src/Relude/Lifted/Env.hs +41/−0
- src/Relude/Lifted/Exit.hs +1/−1
- src/Relude/Lifted/File.hs +16/−1
- src/Relude/Lifted/Handle.hs +67/−0
- src/Relude/Lifted/IORef.hs +1/−1
- src/Relude/Lifted/Terminal.hs +1/−1
- src/Relude/List.hs +50/−3
- src/Relude/List/NonEmpty.hs +1/−1
- src/Relude/List/Reexport.hs +2/−2
- src/Relude/Monad.hs +29/−1
- src/Relude/Monad/Either.hs +2/−28
- src/Relude/Monad/Maybe.hs +1/−1
- src/Relude/Monad/Reexport.hs +1/−1
- src/Relude/Monad/Trans.hs +1/−9
- src/Relude/Monoid.hs +3/−2
- src/Relude/Nub.hs +75/−3
- src/Relude/Numeric.hs +16/−2
- src/Relude/Print.hs +1/−1
- src/Relude/String.hs +1/−1
- src/Relude/String/Conversion.hs +1/−1
- src/Relude/String/Reexport.hs +1/−1
- src/Relude/Unsafe.hs +1/−1
- test/Test/Relude/Extra/Validation/Property.hs +0/−168
- test/Test/Relude/Property.hs +1/−2
CHANGELOG.md view
@@ -3,6 +3,104 @@ `relude` uses [PVP Versioning][1]. The changelog is available [on GitHub][2]. +## 1.0.0.0 — Mar 12, 2021++* [#353](https://github.com/kowainik/relude/issues/353):+ Reexport most common modules from the following libraries:++ + `containers`+ + `unordered-containers`+ + `text`+ + `bytestring`++ Now, when using `relude`, you don't need to add these libraries to+ your `.cabal` file to enjoy their main API. Try removing them from+ your `.cabal` file after upgrading to this version of `relude` to+ see if you still need them.++ To utilise this feature, update the `mixin` part of your package+ configuration (if you're using the mixins approach) to the following:++ ```cabal+ mixins: base hiding (Prelude)+ , relude (Relude as Prelude)+ , relude+ ```++* [#345](https://github.com/kowainik/relude/issues/345):+ Support GHC-9.0.+* Upgrade minor GHC versions to GHC-8.10.4 and GHC-8.8.4.+* [#268](https://github.com/kowainik/relude/issues/268):+ Drop support of GHC-8.0.2.+* [#270](https://github.com/kowainik/relude/issues/270):+ Standardise `universe`, `universeNonEmpty` and `inverseMap` functions that+ previously were introduced in the `Relude.Extra.Enum` module. `Relude.Enum`+ module created that is exported in the main `Relude` module by default.++ __Migration guide:__ If you were using any of these functions you can now+ remove `Relude.Extra.Enum` from your imports and explicit `mixins` section+ as they are available for you with the `Relude` module.+* Remove the `Eq` constraint on `universeNonEmpty`+* [#269](https://github.com/kowainik/relude/issues/269):+ Remove the `Relude.Extra.Validation` module.++ __Migration guide:__+ If you use `Relude.Extra.Validation` in you project you need to:++ 1. Add `validation-selective` into the `build-depends` section of your+ `.cabal` file.+ 2. Change imports of `Relude.Extra.Validation` to `Validation`:++ ```haskell+ -- Was:+ import Relude.Extra.Validation (Validation (..), ..)+ -- Became:+ import Validation (Validation (..), ..)+ ```++* [#346](https://github.com/kowainik/relude/issues/346),+ [#347](https://github.com/kowainik/relude/issues/347):+ Reimplement `ordNub` through `nubOrd` from `containers`.+ Add `ordNubOn`, `intNub` and `intNubOn` functions.+* [#327](https://github.com/kowainik/relude/issues/327):+ Add `infinitely` as more strictly typed `forever`.+* [#311](https://github.com/kowainik/relude/issues/311):+ Add `maybeAt` function — the non-operator version of `!!?` with its+ arguments flipped.+* [#314](https://github.com/kowainik/relude/issues/314):+ Add lifted versions of functions to work with `Handle`:++ + `hFlush`+ + `hIsEOF`+ + `hSetBuffering`+ + `hGetBuffering`+* [#305](https://github.com/kowainik/relude/issues/305):+ Add lifted versions of functions to work with environment:++ + `getArgs`+ + `lookupEnv`+* Add lifted version of the `readFile'` function.+* Reexport the `BufferMode` type from `base`.+* [#309](https://github.com/kowainik/relude/issues/309):+ Reexport `span` from `Data.List`.+* [#319](https://github.com/kowainik/relude/issues/319):+ Implement `partitionWith`.+* [#307](https://github.com/kowainik/relude/issues/307):+ Add `foldr1` to `Foldable1`.+* [#316](https://github.com/kowainik/relude/issues/316):+ Add `average` and `average1` — efficient functions for finding+ average on foldable structures.+* [#306](https://github.com/kowainik/relude/issues/306):+ Add `maximumOn1` and `minimumOn1` to `Foldable1`.+* [#301](https://github.com/kowainik/relude/issues/301):+ Add `traceShowWith` to `Relude.Debug`.+* [#304](https://github.com/kowainik/relude/issues/304),+ [#317](https://github.com/kowainik/relude/issues/317):+ Various documentation improvements.+* Updates to `relude`-specific `.hlint` rules.++Thanks @googleson78, @sushi-shi, @rektrex, @aleator, @mjgpy3, @dalpd,+@Bodigrim for helping with this release! ## 0.7.0.0 — May 14, 2020
README.md view
@@ -3,8 +3,6 @@  [](https://github.com/kowainik/relude/actions)-[](http://travis-ci.org/kowainik/relude)-[](https://ci.appveyor.com/project/kowainik/relude) [](https://hackage.haskell.org/package/relude) [](http://stackage.org/lts/package/relude) [](http://stackage.org/nightly/package/relude)@@ -62,15 +60,19 @@ appropriate, and make efficient container types (e.g. `Map`, `HashMap`, `Set`) more accesible. -5. **Minimalism** (low number of dependencies). We don not force users of+5. **Minimalism** (low number of dependencies). We do not force users of `relude` to stick to any specific lens or text formatting or logging library. Where possible, `relude` depends only on boot libraries.- The [Dependency graph](https://raw.githubusercontent.com/kowainik/relude/master/relude-dependency-graph.png)+ The [Dependency graph](https://raw.githubusercontent.com/kowainik/relude/main/relude-dependency-graph.png) of `relude` can give you a clearer picture. 6. **Convenience**. Despite minimalism, we want to bring commonly used types and functions into scope, and make available functions easier to use. Some examples of conveniences:++ + No need to add `containers`, `unordered-containers`, `text` and+ `bytestring` to dependencies in your `.cabal` file to use the+ main API of these libraries + No need to import types like `NonEmpty`, `Text`, `Set`, `Reader[T]`, `MVar`, `STM` + Functions like `liftIO`, `fromMaybe`, `sortWith` are avaiable by default as well + `IO` actions are lifted to `MonadIO`@@ -82,7 +84,7 @@ [`doctest`](http://hackage.haskell.org/package/doctest) + Documentation on [internal module structure](http://hackage.haskell.org/package/relude/docs/Relude.html) + `relude`-specific [HLint](http://hackage.haskell.org/package/hlint) rules:- [`.hlint.yaml`](https://github.com/kowainik/relude/blob/master/.hlint.yaml)+ [`.hlint.yaml`](https://github.com/kowainik/relude/blob/main/.hlint.yaml) 8. **User-friendliness.** Anyone should be able to quickly migrate to `relude`. Only some basic familiarity with the common libraries like `text` and `containers`@@ -202,10 +204,18 @@ ```haskell mixins: base hiding (Prelude) , relude (Relude as Prelude)+ , relude ``` > **NOTE:** this requires Cabal version to be at least `2.2` +The above syntax does the following:++1. Makes all modules of `base` available except `Prelude`.+2. Renames the `Relude` module in `relude` to `Prelude`.+3. Additionally allows importing all other modules from `relude`+ (extra modules and reexports from other libraries).+ See the following complete example of how your `.cabal` file may look like after the set up: @@ -217,10 +227,11 @@ library exposed-modules: Example build-depends: base >= 4.10 && < 4.13- , relude ^>= 0.6.0.0+ , relude ^>= 1.0.0.0 mixins: base hiding (Prelude) , relude (Relude as Prelude)+ , relude default-language: Haskell2010 ```@@ -229,19 +240,21 @@ > generate a Haskell project, the tool automatically creates the `mixins` > field when you specify a custom prelude. -If you want to bring a non-default module of `relude`, e.g. `Relude.Extra.Enum`-or `Relude.Unsafe`, you need to list it under the `mixins` field as well,-like this:+If you want to restrict allowed modules in `relude` to a specific list+(e.g. use only `Relude.Extra.Enum` or `Relude.Unsafe` or `Data.Text`+from `text`), you can alternatively list them explicitly under the+first `mixins` entry field as well, like this: ```cabal mixins: base hiding (Prelude) , relude (Relude as Prelude , Relude.Extra.Enum- , ...+ , Relude.Unsafe+ , Data.Text ) ``` -If you want to bring all `Extra.*` modules into scope, you can add+If you want to bring only all `Extra.*` modules into scope, you can add a single `Relude.Extra` module to `mixins`, and after that you can import all extra functions and data types from `Relude.Extra`. This is the easiest way to bring all functions and types from `relude` to your project@@ -254,6 +267,13 @@ ) ``` +> **NOTE:** due to the existing [stack issue](https://github.com/commercialhaskell/stack/issues/5077),+> `mixins` technique doesn't work with the `stack repl` at the moment. Please,+> consider this before using this method of turning `relude` on in the project.+>+> If having `stack repl` crucial for your workflow, see the following options of+> how to use Relude in your project.+ ### base-noprelude [[Back to the Table of Contents] ↑](#structure-of-this-tutorial)@@ -362,7 +382,7 @@ If you want to clean up your imports after switching to `relude`, you can use the `relude`-specific-[`.hlint.yaml`](https://github.com/kowainik/relude/blob/master/.hlint.yaml)+[`.hlint.yaml`](https://github.com/kowainik/relude/blob/main/.hlint.yaml) configuration for this task. With this config, `HLint` will produce warnings and hints on how to have more benefits from `relude`. @@ -489,7 +509,8 @@ * `uncons` splits a list at the first element. * `ordNub` and `sortNub` are _O(n log n)_ versions of `nub` (which is quadratic),- also, `hashNub` and `unstableNub` are almost _O(n)_ versions of `nub`.+ also, `hashNub` and `unstableNub` are almost _O(n)_ versions of `nub`,+ and `intNub` for fast `Int`s nub. * `whenM`, `unlessM`, `ifM`, `guardM` — monadic guard combinators, that work with any `Monad`, e.g. `whenM (doesFileExist "foo")`. * General fold functions:@@ -660,7 +681,7 @@ two commands on your CI: ```yaml-curl https://raw.githubusercontent.com/kowainik/relude/v0.6.0.0/.hlint.yaml -o .hlint-relude.yaml+curl https://raw.githubusercontent.com/kowainik/relude/v1.0.0.0/.hlint.yaml -o .hlint-relude.yaml curl -sSL https://raw.github.com/ndmitchell/neil/master/misc/travis.sh | sh -s -- hlint -h .hlint-relude.yaml . ``` @@ -697,7 +718,7 @@ _O(n)_ time and you can accidentally use `elem` from `Foldable` but with `relude` you can't). 4. Protolude supports older GHC versions (from GHC 7.6.1) while `relude` only- supports from GHC 8.0.2. So if you aim ancient GHC versions, `protolude`+ supports from GHC 8.2.2. So if you aim ancient GHC versions, `protolude` might be a better choice. But because of that it contains a lot of CPP, code is scary in some places as a consequence and it is more difficult to add, remove or change things there.@@ -733,16 +754,16 @@ First time: ```shell-$ cabal v2-install dhall-json+$ cabal v2-install dhall-yaml ``` Dhall 16.0.0 is required, so make sure that the previous command installed-`dhall-json` >= 1.6.4.+`dhall-yaml` >= 1.2.5. To generate `hlint` file: ```shell-$ dhall-to-yaml --omitNull <<< './hlint/hlint.dhall' > .hlint.yaml+$ dhall-to-yaml-ng <<< './hlint/hlint.dhall' > .hlint.yaml ``` Check that you have generated valid `.hlint.yaml` file without parse errors:@@ -754,3 +775,19 @@ See our blog post where we describe the details of the implementation for this solution: * [Dhall To HLint](https://kowainik.github.io/posts/2018-09-09-dhall-to-hlint)++### Producing dependency graph++Install `cabal-plan` first:++```shell+$ cabal v2-install cabal-plan+$ cabal-plan --version+cabal-plan 0.6.2.0+```++Then draw the graph only for the library dependencies:++```shell+cabal-plan dot --root lib:relude | dot -Tpng -o relude-dependency-graph.png+```
benchmark/Main.hs view
@@ -1,10 +1,12 @@+{-# LANGUAGE CPP #-}+ module Main (main) where import Relude hiding (show) import Data.List (nub)-import Gauge (Benchmark, bench, bgroup, nf)-import Gauge.Main (defaultMain)+import Criterion (Benchmark, bench, bgroup, nf)+import Criterion.Main (defaultMain) import Prelude (show) import qualified Data.HashSet as HashSet (insert)@@ -17,6 +19,12 @@ , bgroupList listOfBig "big" , bgroupList (nStrings 'z') "small str" , bgroupList (nStrings 'c') "big str"++#if __GLASGOW_HASKELL__ > 804+ , bgroupIntList listOfSmall "small ints"+ , bgroupIntList listOfBig "big ints"+#endif+ , bgroupFold ] @@ -63,6 +71,33 @@ safeSort :: [a] -> [a] safeSort = map NonEmpty.head . NonEmpty.group . sort++#if __GLASGOW_HASKELL__ > 804+bgroupIntList+ :: (Int -> [Int])+ -> String+ -> Benchmark+bgroupIntList f name = bgroup name $ map ($ f)+ [ bgroupNub 100+ , bgroupNub 500+ , bgroupNub 1000+ , bgroupNub 5000+ , bgroupNub 500000+ , bgroupNub 1000000+ ]+ where+ bgroupNub :: Int -> (Int -> [Int]) -> Benchmark+ bgroupNub n listOf = bgroup (show n) nubBenchs+ where+ listN :: [Int]+ listN = listOf n++ nubBenchs :: [Benchmark]+ nubBenchs =+ [ bench "ordNub" $ nf ordNub listN+ , bench "intNub" $ nf intNub listN+ ]+#endif listOfSmall :: Int -> [Int] listOfSmall n = let part = n `div` 100 in concat $ replicate part [1..100]
relude.cabal view
@@ -1,24 +1,24 @@-cabal-version: 2.4+cabal-version: 3.0 name: relude-version: 0.7.0.0+version: 1.0.0.0 synopsis: Safe, performant, user-friendly and lightweight Haskell Standard Library description: @__relude__@ is an alternative prelude library. If you find the default @Prelude@ unsatisfying, despite its advantages, consider using @relude@ instead.- .+ == Relude goals and design principles * __Productivity.__ You can be more productive with a "non-standard" standard library, and @relude@ helps you with writing safer and more efficient code faster.- .+ * __Total programming__. Usage of [/partial functions/](https://www.reddit.com/r/haskell/comments/5n51u3/why_are_partial_functions_as_in_head_tail_bad/) can lead to unexpected bugs and runtime exceptions in pure code. The types of partial functions lie about their behaviour. And even if it is not always possible to rely only on total functions, @relude@ strives to encourage best-practices and reduce the chances of introducing a bug.- .+ +---------------------------------+--------------------------------------------+ | __Partial__ | __Total__ | +=================================+============================================+@@ -30,51 +30,49 @@ +---------------------------------+--------------------------------------------+ | @fromJust :: Maybe a -> a@ | @fromMaybe :: a -> Maybe a -> a@ | +---------------------------------+--------------------------------------------+- .+ * __Type-safety__. We use the /"make invalid states unrepresentable"/ motto as one of our guiding principles. If it is possible, we express this concept through the types.- /Example:/ @ whenNotNull :: Applicative f => [a] -> (NonEmpty a -> f ()) -> f () @- .++ /Example:/ @ whenNotNull :: Applicative f => [a] -> (NonEmpty a -> f ()) -> f () @+ * __Performance.__ We prefer @Text@ over @[String](https://www.reddit.com/r/haskell/comments/29jw0s/whats_wrong_with_string/)@, use space-leaks-free functions (e.g. our custom performant @sum@ and @product@), introduce @\{\-\# INLINE \#\-\}@ and @\{\-\# SPECIALIZE \#\-\}@ pragmas where appropriate, and make efficient container types (e.g. @Map@, @HashMap@, @Set@) more accesible.- .- * __Minimalism__ (low number of dependencies). We don not force users of++ * __Minimalism__ (low number of dependencies). We do not force users of @relude@ to stick to any specific lens or text formatting or logging library. Where possible, @relude@ depends only on boot libraries.- The [Dependency graph](https://raw.githubusercontent.com/kowainik/relude/master/relude-dependency-graph.png)+ The [Dependency graph](https://raw.githubusercontent.com/kowainik/relude/main/relude-dependency-graph.png) of @relude@ can give you a clearer picture.- .+ * __Convenience__. Despite minimalism, we want to bring commonly used types and functions into scope, and make available functions easier to use. Some examples of conveniences:- .- 1. No need to import types like @NonEmpty@, @Text@, @Set@, @Reader[T]@, @MVar@, @STM@- .- 2. Functions like @liftIO@, @fromMaybe@, @sortWith@ are avaiable by default as well- .- 3. @IO@ actions are lifted to @MonadIO@- .++ 1. No need to add @containers@, @unordered-containers@, @text@+ and @bytestring@ to dependencies in your @.cabal@ file to+ use the main API of these libraries+ 2. No need to import types like @NonEmpty@, @Text@, @Set@, @Reader[T]@, @MVar@, @STM@+ 3. Functions like @liftIO@, @fromMaybe@, @sortWith@ are avaiable by default as well+ 4. @IO@ actions are lifted to @MonadIO@+ * __Excellent documentation.__- .+ 1. Tutorial- . 2. Migration guide from @Prelude@- . 3. Haddock for every function with examples tested by [doctest](http://hackage.haskell.org/package/doctest).- . 4. Documentation regarding [internal module structure](http://hackage.haskell.org/package/relude/docs/Relude.html)- .- 5. @relude@-specific [HLint](http://hackage.haskell.org/package/hlint) rules: @[.hlint.yaml](https://github.com/kowainik/relude/blob/master/.hlint.yaml)@- .+ 5. @relude@-specific [HLint](http://hackage.haskell.org/package/hlint) rules: @[.hlint.yaml](https://github.com/kowainik/relude/blob/main/.hlint.yaml)@+ * __User-friendliness.__ Anyone should be able to quickly migrate to @relude@. Only some basic familiarity with the common libraries like @text@ and @containers@ should be enough (but not necessary).- .+ * __Exploration.__ We have space to experiment with new ideas and proposals without introducing breaking changes. @relude@ uses the approach with @Extra.*@ modules which are not exported by default. The chosen approach makes it quite@@ -87,18 +85,18 @@ license-file: LICENSE author: Dmitrii Kovanikov, Veronika Romashkina, Stephen Diehl, Serokell maintainer: Kowainik <xrom.xkov@gmail.com>-copyright: 2016 Stephen Diehl, 2016-2018 Serokell, 2018-2020 Kowainik+copyright: 2016 Stephen Diehl, 2016-2018 Serokell, 2018-2021 Kowainik category: Prelude stability: stable build-type: Simple extra-doc-files: CHANGELOG.md README.md-tested-with: GHC == 8.0.2- GHC == 8.2.2+tested-with: GHC == 8.2.2 GHC == 8.4.4 GHC == 8.6.5- GHC == 8.8.3- GHC == 8.10.1+ GHC == 8.8.4+ GHC == 8.10.4+ GHC == 9.0.1 source-repository head@@ -106,7 +104,7 @@ location: git@github.com:kowainik/relude.git common common-options- build-depends: base >= 4.9 && < 4.15+ build-depends: base >= 4.10 && < 4.16 ghc-options: -Wall -Wcompat@@ -114,10 +112,8 @@ -Wincomplete-uni-patterns -Wincomplete-record-updates -fwarn-implicit-prelude- if impl(ghc >= 8.0)- ghc-options: -Wredundant-constraints- if impl(ghc >= 8.2)- ghc-options: -fhide-source-paths+ -Wredundant-constraints+ -fhide-source-paths if impl(ghc >= 8.4) ghc-options: -Wmissing-export-lists -Wpartial-fields@@ -149,6 +145,7 @@ Relude.Container.Reexport Relude.Debug Relude.DeepSeq+ Relude.Enum Relude.Exception Relude.File Relude.Foldable@@ -164,6 +161,8 @@ Relude.Lifted.File Relude.Lifted.IORef Relude.Lifted.Terminal+ Relude.Lifted.Handle+ Relude.Lifted.Env Relude.List Relude.List.NonEmpty Relude.List.Reexport@@ -193,14 +192,39 @@ Relude.Extra.Newtype Relude.Extra.Tuple Relude.Extra.Type- Relude.Extra.Validation Relude.Unsafe + reexported-modules:+ -- containers+ , Data.IntMap.Lazy+ , Data.IntMap.Strict+ , Data.IntSet+ , Data.Map.Lazy+ , Data.Map.Strict+ , Data.Set+ , Data.Sequence+ , Data.Tree+ -- unordered-containers+ , Data.HashMap.Lazy+ , Data.HashMap.Strict+ , Data.HashSet+ -- text+ , Data.Text+ , Data.Text.IO+ , Data.Text.Lazy+ , Data.Text.Lazy.IO+ , Data.Text.Read+ -- bytestring+ , Data.ByteString+ , Data.ByteString.Builder+ , Data.ByteString.Lazy+ , Data.ByteString.Short - build-depends: bytestring ^>= 0.10++ build-depends: bytestring >= 0.10 && < 0.12 , containers >= 0.5.7 && < 0.7 , deepseq ^>= 1.4- , ghc-prim >= 0.4.0.0 && < 0.7+ , ghc-prim >= 0.4.0.0 && < 0.8 , hashable >= 1.2 && < 1.4 , mtl ^>= 2.2 , stm >= 2.4 && < 2.6@@ -218,7 +242,6 @@ other-modules: Test.Relude.Gen Test.Relude.Container.One Test.Relude.Property- Test.Relude.Extra.Validation.Property build-depends: relude , bytestring , containers@@ -246,7 +269,7 @@ main-is: Main.hs build-depends: relude- , gauge+ , criterion , unordered-containers ghc-options: -threaded
src/Relude.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -34,6 +34,7 @@ ... mixins: base hiding (Prelude) , relude (Relude as Prelude)+ , relude @ === @base-noprelude@@@ -93,6 +94,8 @@ -- $debug , module Relude.DeepSeq -- $deepseq+ , module Relude.Enum+ -- $enum , module Relude.Exception -- $exception , module Relude.File@@ -130,6 +133,7 @@ import Relude.Container import Relude.Debug import Relude.DeepSeq+import Relude.Enum import Relude.Exception import Relude.File import Relude.Foldable@@ -174,6 +178,11 @@ to evaluate expressions to weak-head normal form or normal form. -} +{- $enum+__"Relude.Enum"__ reexports 'Enum' related typeclasses and functions. Also+introduced a few useful helpers to work with Enums.+-}+ {- $exception __"Relude.Exception"__ contains reexports from "Control.Exception", introduces 'bug' function as better 'error' and 'Exc' pattern synonym for convenient@@ -269,13 +278,6 @@ | __"Relude.Extra.Tuple"__ | Functions for working with tuples. | +------------------------------+-----------------------------------------------------------+ | __"Relude.Extra.Type"__ | Functions for inspecting and working with types. |-+------------------------------+-----------------------------------------------------------+-| __"Relude.Extra.Validation"__| 'Relude.Extra.Validation.Validation' data type. |-| | |-| | __⚠️ Warning ⚠️__ |-| | "Relude.Extra.Validation" is deprecated in favour of |-| | @validation-selective@: |-| | <https://hackage.haskell.org/package/validation-selective>| +------------------------------+-----------------------------------------------------------+ | __"Relude.Unsafe"__ | Unsafe partial functions (produce 'error') for lists and | | | 'Maybe'. |
src/Relude/Applicative.hs view
@@ -1,10 +1,9 @@-{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -21,9 +20,6 @@ , Applicative (..) , Const (..) , ZipList (..)-#if __GLASGOW_HASKELL__ < 802- , liftA2-#endif , liftA3 , optional , (<**>)@@ -41,16 +37,26 @@ -- >>> import Relude -- >>> import Relude.Monad (Maybe) --- | Shorter alias for @pure ()@.------ >>> pass :: Maybe ()--- Just ()+{- | Shorter alias for @pure ()@.++>>> pass :: Maybe ()+Just ()++Useful shortcut when need an empty action:++@+printJust :: Maybe Int -> IO ()+printJust mInt = case mInt of+ Just i -> putStrLn $ "Number: " ++ show i+ Nothing -> pass+@+-} pass :: Applicative f => f () pass = pure () {-# INLINE pass #-} -{- | Named version of the '<**>' operator, which is '<*>' but flipped. For-chaining applicative operations in forward applications using+{- | Named version of the '<**>' operator, which is '<*>' but flipped. It is+helpful for chaining applicative operations in forward applications using 'Relude.Function.&'. >>> Just (+ 1) & appliedTo (Just 2)
src/Relude/Base.hs view
@@ -1,10 +1,9 @@-{-# LANGUAGE CPP #-} {-# LANGUAGE Trustworthy #-} {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -34,17 +33,11 @@ -- * Basic type classes , module GHC.Base- , module GHC.Enum , module GHC.Generics , module GHC.Show -- * GHC-specific functionality-#if MIN_VERSION_base(4,10,0) , module GHC.TypeNats-#else- , module GHC.TypeLits-#endif- , module GHC.OverloadedLabels , module GHC.ExecutionStack , module GHC.Stack@@ -54,7 +47,7 @@ import Data.Char (Char, chr) -- IO-import System.IO (FilePath, Handle, IO, IOMode (..), stderr, stdin, stdout, withFile)+import System.IO (FilePath, IO, IOMode (..)) -- Base typeclasses import Data.Eq (Eq (..))@@ -68,15 +61,10 @@ import Data.Void (Void, absurd, vacuous) import GHC.Base (asTypeOf, ord, seq, ($!))-import GHC.Enum (Bounded (..), Enum (..), boundedEnumFrom, boundedEnumFromThen) import GHC.Generics (Generic) import GHC.Show (Show) -#if MIN_VERSION_base(4,10,0) import GHC.TypeNats (CmpNat, KnownNat, Nat, SomeNat (..), natVal, someNatVal)-#else-import GHC.TypeLits (CmpNat, KnownNat, Nat, SomeNat (..), natVal, someNatVal)-#endif import GHC.ExecutionStack (getStackTrace, showStackTrace) import GHC.OverloadedLabels (IsLabel (..))
src/Relude/Bool.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Bool/Guard.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -38,6 +38,7 @@ -- >>> import Relude (Int, String, even, const) {- | Monadic version of 'when'.+Conditionally executes the provided action. >>> whenM (pure False) $ putTextLn "No text :(" >>> whenM (pure True) $ putTextLn "Yes text :)"@@ -53,7 +54,8 @@ whenM p m = p >>= flip when m {-# INLINE whenM #-} -{- | Monadic version of 'unless'.+{- | Monadic version of 'unless'. Reverse of 'whenM'.+Conditionally don't execute the provided action. >>> unlessM (pure False) $ putTextLn "No text :(" No text :(@@ -67,12 +69,23 @@ >>> ifM (pure True) (putTextLn "True text") (putTextLn "False text") True text+>>> ifM (pure False) (putTextLn "True text") (putTextLn "False text")+False text -} ifM :: Monad m => m Bool -> m a -> m a -> m a ifM p x y = p >>= \b -> if b then x else y {-# INLINE ifM #-} -{- | Monadic version of 'guard'. Occasionally useful.+{- | Monadic version of 'guard' that help to check that a condition ('Bool')+holds inside. Works with 'Monad's that are also 'Alternative'.++>>> guardM (Just True)+Just ()+>>> guardM (Just False)+Nothing+>>> guardM Nothing+Nothing+ Here some complex but real-life example: @@@ -90,7 +103,7 @@ {-# INLINE guardM #-} {- | Either lifts a value into an alternative context or gives a-minimal value depending on a predicate.+minimal value depending on a predicate. Works with 'Alternative's. >>> guarded even 3 :: [Int] []@@ -118,8 +131,12 @@ guarded p a = if p a then pure a else empty {-# INLINE guarded #-} -{- | Monadic version of 'Data.Bool.(&&)' operator.+{- | Monadic version of '(Data.Bool.&&)' operator. +It is lazy by the second argument (similar to '(Data.Bool.||)'), meaning that if+the first argument is 'False', the function will return 'False' without evaluating+the second argument.+ >>> Just False &&^ Just True Just False >>> Just True &&^ Just True@@ -137,7 +154,11 @@ (&&^) e1 e2 = ifM e1 e2 (pure False) {-# INLINE (&&^) #-} -{- | Monadic version of 'Data.Bool.(||)' operator.+{- | Monadic version of '(Data.Bool.||)' operator.++It is lazy by the second argument (similar to '(Data.Bool.||)'), meaning that if+the first argument is 'True', the function will return 'True' without evaluating+the second argument. >>> Just False ||^ Just True Just True
src/Relude/Bool/Reexport.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Container.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Container/One.hs view
@@ -6,7 +6,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -52,6 +52,7 @@ -- >>> import Relude {- | Typeclass for data types that can be created from one element.+E.g. lists, non-empty containers, maps. >>> one True :: [Bool] [True]@@ -68,7 +69,7 @@ this container) -} class One x where- -- | Type of single element of the structure.+ -- | Type of a single element of the structure. type OneItem x -- | Create a list, map, 'T.Text', etc from a single element.
src/Relude/Container/Reexport.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Debug.hs view
@@ -19,15 +19,21 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable Portability: Portable -Functions for debugging and prototyping. If you leave these functions in your-code then a warning is generated to remind you about left usages.+This module contains functions for debugging __pure__ functions. You+can't use functions like 'System.IO.putStrLn' for this purpose because+they require changes to the type signature, but functions in this module+avoid this problem by being pure on their own. +Additionally, these functions produce compile-time warnings, if you leave them+in your code. Warnings help you to cleanup all debugging usages before+releasing.+ @ __ghci>__ foo = trace "I forgot trace in code" @@ -36,18 +42,43 @@ "'trace' remains in code" @ -__⚠ NOTE:__ Use these functions only for debugging purposes. They break referential-transparency, they are only useful when you want to observe intermediate values-of your pure functions.+The following table briefly shows names and types of all functions in+this module.+++-----------------+----------------------------------------++| __Name__ | __Type__ |++=================+========================================++| 'trace' | @String -> a -> a@ |++-----------------+----------------------------------------++| 'traceShow' | @Show a => a -> b -> b@ |++-----------------+----------------------------------------++| 'traceShowId' | @Show a => a -> a@ |++-----------------+----------------------------------------++| 'traceShowWith' | @Show b => (a -> b) -> a -> a@ |++-----------------+----------------------------------------++| 'traceId' | @String -> String@ |++-----------------+----------------------------------------++| 'traceM' | @(Applicative f) => String -> f ()@ |++-----------------+----------------------------------------++| 'traceShowM' | @(Show a, Applicative f) => a -> f ()@ |++-----------------+----------------------------------------+++__⚠ NOTE:__ Use these functions only for local debugging+purposes. They break referential transparency, they are only useful+when you want to observe intermediate values of your pure functions+and to understand the behaviour locally. If you want to log debug+messages in your application, consider using a logging library+instead. -} module Relude.Debug ( -- * Tracing trace- , traceM- , traceId , traceShow , traceShowId+ , traceShowWith+ , traceId+ , traceM , traceShowM -- * Imprecise error@@ -61,8 +92,8 @@ import GHC.TypeLits (ErrorMessage (..), TypeError) import Relude.Applicative (Applicative)-import Relude.Base (Bounded, Char, Constraint, Enum, Eq, Generic, HasCallStack, Ord, Show, Type,- Typeable)+import Relude.Base (Char, Constraint, Eq, Generic, HasCallStack, Ord, Show, Type, Typeable)+import Relude.Enum (Bounded, Enum) import Relude.String (Read, String, Text, toString) import qualified Debug.Trace as Debug@@ -77,7 +108,8 @@ -- trace ---------------------------------------------------------------------------- -{- | Version of 'Debug.Trace.trace' that leaves warning.+{- | Prints the given 'String' message and returns the passed value of+type @a@. >>> increment l = map (+1) l >>> increment [2, 3, 4]@@ -88,12 +120,17 @@ incrementing each value of: [2,3,4] [3,4,5] +* If you want to print a 'Show'able value instead of 'String', use 'traceShow'.+* If you want to print the value itself, use 'traceShowId'.+* If you want to print by specifying a custom formatting function, use 'traceShowWith'.+ -} trace :: String -> a -> a trace = Debug.trace {-# WARNING trace "'trace' remains in code" #-} -{- | Version of 'Debug.Trace.traceShow' that leaves warning.+{- | Similar to 'trace' but prints a given value with the 'Show'+instance instead of a 'String'. >>> increment l = map (+1) l >>> increment [2, 3, 4]@@ -104,24 +141,55 @@ [2,3,4] [3,4,5] +* If you want to print a specific 'String' instead, use 'trace'+* If you want to print and return the same value, use 'traceShowId'+* If you want to specify a custom printing function, use 'traceShowWith' -} traceShow :: Show a => a -> b -> b traceShow = Debug.traceShow {-# WARNING traceShow "'traceShow' remains in code" #-} -{- | Version of 'Debug.Trace.traceShowId' that leaves warning.+{- | Similar to 'traceShow' but prints the given value itself instead+of a separate value. >>> traceShowId (1+2+3, "hello" ++ "world") (6,"helloworld") (6,"helloworld") +* If you to specify a different value to print, use 'trace' or 'traceShow'+* If you want to have more control over printing, use 'traceShowWith' -} traceShowId :: Show a => a -> a traceShowId = Debug.traceShowId {-# WARNING traceShowId "'traceShowId' remains in code" #-} -{- | Version of 'Debug.Trace.traceM' that leaves warning.+{- | Similar 'traceShowId', but uses a provided function to convert the+argument to a value with the 'Show' constraint. +>>> traceShowWith fst (1, "ABC")+1+(1,"ABC")++In other words, @'traceShowId' ≡ 'traceShowWith' id@.++This function is useful for debugging values that do not have 'Show'+instance:++>>> fst $ traceShowWith fst (1, id)+1+1++* If you don't need such flexibility, use simpler 'trace', 'traceShow' or 'traceShowId'++@since 1.0.0.0+-}+traceShowWith :: Show b => (a -> b) -> a -> a+traceShowWith f v = Debug.traceShow (f v) v+{-# WARNING traceShowWith "'traceShowWith remains in code" #-}++{- | Trace function to print values while working a pure monad+(e.g. 'Maybe', 'State', etc.)+ >>> :{ let action :: Maybe Int action = do@@ -135,6 +203,8 @@ x: 3 y: 12 Just 18++* If you want to print a value with the 'Show' instance instead, use 'traceShowM' -} traceM :: (Applicative f) => String -> f () traceM = Debug.traceM@@ -162,7 +232,7 @@ traceShowM = Debug.traceShowM {-# WARNING traceShowM "'traceShowM' remains in code" #-} -{- | Version of 'Debug.Trace.traceId' that leaves warning.+{- | Similar to 'traceShowId' but specialised for 'String'. >>> traceId "hello" "hello@@ -184,13 +254,13 @@ >>> error "oops" *** Exception: oops CallStack (from HasCallStack):- error, called at src\\Relude\\Debug.hs:218:11 in ...+ error, called at src\\Relude\\Debug.hs:288:11 in ... ... #else >>> error "oops" *** Exception: oops CallStack (from HasCallStack):- error, called at src/Relude/Debug.hs:218:11 in ...+ error, called at src/Relude/Debug.hs:288:11 in ... ... #endif
src/Relude/DeepSeq.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -40,7 +40,7 @@ -- $setup -- >>> import Relude -{- | Lifted alias for 'Control.Exception.Base.evaluate' with clearer name.+{- | Lifted alias for 'Control.Exception.Base.evaluate' with a clearer name. >>> let list = [1..5] :: [Int] >>> :sprint list@@ -68,7 +68,7 @@ {-# INLINE evaluateWHNF_ #-} {-# SPECIALIZE evaluateWHNF_ :: a -> IO () #-} -{- | Alias for @evaluateWHNF . force@ with clearer name.+{- | Alias for @evaluateWHNF . force@ with a clearer name. >>> let list = [1..5] :: [Int] >>> :sprint list@@ -83,7 +83,7 @@ {-# SPECIALIZE evaluateNF :: NFData a => a -> IO a #-} {- | Alias for @evaluateWHNF . rnf@. Similar to 'evaluateNF'--- but discards resulting value.+but discards the resulting value. >>> let list = [1..5] :: [Int] >>> :sprint list
+ src/Relude/Enum.hs view
@@ -0,0 +1,146 @@+{- |+Copyright: (c) 2021 Kowainik+SPDX-License-Identifier: MIT+Maintainer: Kowainik <xrom.xkov@gmail.com>+Stability: Stable+Portability: Portable++Reexports 'Enum' related typeclasses and functions. Also introduces a few useful+helpers to work with Enums.++**Note:** 'universe', 'universeNonEmpty' and 'inverseMap' were previously in the+extra modules, but due to their benefit in different use cases. If you imported+@Relude.Extra.Enum@ module, you can remove it now, as these functions are+reexported in the main "Relude" module.++@since 1.0.0.0+-}++module Relude.Enum+ ( -- * Useful combinators for Enums+ universe+ , universeNonEmpty+ , inverseMap+ -- * Base reexports+ , module GHC.Enum+ ) where++import GHC.Enum (Bounded (..), Enum (..), boundedEnumFrom, boundedEnumFromThen)+import Data.List.NonEmpty (NonEmpty (..))+import Data.List (drop)+import Data.Maybe (Maybe (..))++import Relude.Base (Ord (..))+import Relude.Extra.Tuple (fmapToFst)++import qualified Data.Map.Strict as M+++-- $setup+-- >>> import Relude++{- | Returns all values of some 'Bounded' 'Enum' in ascending order.++>>> universe :: [Bool]+[False,True]++>>> universe @Ordering+[LT,EQ,GT]++>>> data TrafficLight = Red | Blue | Green deriving (Show, Enum, Bounded)+>>> universe :: [TrafficLight]+[Red,Blue,Green]++>>> data Singleton = Singleton deriving (Show, Enum, Bounded)+>>> universe @Singleton+[Singleton]++@since 0.1.0+-}+universe :: (Bounded a, Enum a) => [a]+universe = [minBound .. maxBound]+{-# INLINE universe #-}++{- | Like 'universe', but returns 'NonEmpty' list of some enumeration++>>> universeNonEmpty :: NonEmpty Bool+False :| [True]++>>> universeNonEmpty @Ordering+LT :| [EQ,GT]++>>> data TrafficLight = Red | Blue | Green deriving (Show, Eq, Enum, Bounded)+>>> universeNonEmpty :: NonEmpty TrafficLight+Red :| [Blue,Green]++>>> data Singleton = Singleton deriving (Show, Eq, Enum, Bounded)+>>> universeNonEmpty @Singleton+Singleton :| []++@since 0.7.0.0+-}+universeNonEmpty :: forall a . (Bounded a, Enum a) => NonEmpty a+universeNonEmpty = minBound :| drop 1 universe+{-# INLINE universeNonEmpty #-}++{- | @inverseMap f@ creates a function that is the inverse of a given function+@f@. It does so by constructing 'M.Map' internally for each value @f a@. The+implementation makes sure that the 'M.Map' is constructed only once and then+shared for every call.++__Memory usage note:__ don't inverse functions that have types like 'Int'+as their result. In this case the created 'M.Map' will have huge size.++The complexity of reversed mapping is \(\mathcal{O}(\log n)\).++__Performance note:__ make sure to specialize monomorphic type of your functions+that use 'inverseMap' to avoid 'M.Map' reconstruction.++One of the common 'inverseMap' use-case is inverting the 'show' or a 'show'-like+function.++>>> data Color = Red | Green | Blue deriving (Show, Enum, Bounded)+>>> parse = inverseMap show :: String -> Maybe Color+>>> parse "Red"+Just Red+>>> parse "Black"+Nothing++__Correctness note:__ 'inverseMap' expects /injective function/ as its argument,+i.e. the function must map distinct arguments to distinct values.++Typical usage of this function looks like this:++@+__data__ GhcVer+ = Ghc802+ | Ghc822+ | Ghc844+ | Ghc865+ | Ghc881+ __deriving__ ('Eq', 'Ord', 'Show', 'Enum', 'Bounded')++showGhcVer :: GhcVer -> 'Text'+showGhcVer = \\__case__+ Ghc802 -> "8.0.2"+ Ghc822 -> "8.2.2"+ Ghc844 -> "8.4.4"+ Ghc865 -> "8.6.5"+ Ghc881 -> "8.8.1"++parseGhcVer :: 'Text' -> 'Maybe' GhcVer+parseGhcVer = 'inverseMap' showGhcVer+@++@since 0.1.1+-}+inverseMap+ :: forall a k .+ (Bounded a, Enum a, Ord k)+ => (a -> k)+ -> (k -> Maybe a)+inverseMap f = \k -> M.lookup k dict+ where+ dict :: M.Map k a+ dict = M.fromList (fmapToFst f (universe @a))+{-# INLINE inverseMap #-}
src/Relude/Exception.hs view
@@ -11,7 +11,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -54,8 +54,9 @@ #endif instance Exception Bug where- displayException (Bug e cStack) = E.displayException e ++ "\n"- ++ prettyCallStack cStack+ displayException (Bug e cStack) =+ E.displayException e ++ "\n"+ ++ prettyCallStack cStack -- | Generate a pure value which, when forced, will throw the given exception impureThrow :: Exception e => e -> a
src/Relude/Extra.hs view
@@ -1,5 +1,5 @@ {- |-Copyright: (c) 2019-2020 Kowainik+Copyright: (c) 2019-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental@@ -33,8 +33,6 @@ -- $Tuple , module Relude.Extra.Type -- $Type- , module Relude.Extra.Validation- -- $Validation ) where import Relude.Extra.Bifunctor@@ -48,7 +46,6 @@ import Relude.Extra.Newtype import Relude.Extra.Tuple import Relude.Extra.Type-import Relude.Extra.Validation {- $Bifunctor Additional combinators for 'Relude.Bifunctor'.@@ -96,12 +93,4 @@ {- $Type Functions for inspecting and working with types.--}--{- $Validation-'Validation' data type.--__⚠️ Warning ⚠️__-"Relude.Extra.Validation" is deprecated in favour-of [validation-selective](https://hackage.haskell.org/package/validation-selective). -}
src/Relude/Extra/Bifunctor.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE Safe #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental
src/Relude/Extra/CallStack.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE Safe #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental
src/Relude/Extra/Enum.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE Safe #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental@@ -13,128 +13,14 @@ -} module Relude.Extra.Enum- ( universe- , universeNonEmpty- , inverseMap- , next+ ( next , prev , safeToEnum ) where import Relude-import Relude.Extra.Tuple (fmapToFst) -import qualified Data.Map.Strict as M --{- | Returns all values of some 'Bounded' 'Enum' in ascending order.-->>> universe :: [Bool]-[False,True]-->>> universe @Ordering-[LT,EQ,GT]-->>> data TrafficLight = Red | Blue | Green deriving (Show, Enum, Bounded)->>> universe :: [TrafficLight]-[Red,Blue,Green]-->>> data Singleton = Singleton deriving (Show, Enum, Bounded)->>> universe @Singleton-[Singleton]--@since 0.1.0--}-universe :: (Bounded a, Enum a) => [a]-universe = [minBound .. maxBound]-{-# INLINE universe #-}--{- | Like 'universe', but returns 'NonEmpty' list of some enumeration-->>> universeNonEmpty :: NonEmpty Bool-False :| [True]-->>> universeNonEmpty @Ordering-LT :| [EQ,GT]-->>> data TrafficLight = Red | Blue | Green deriving (Show, Eq, Enum, Bounded)->>> universeNonEmpty :: NonEmpty TrafficLight-Red :| [Blue,Green]-->>> data Singleton = Singleton deriving (Show, Eq, Enum, Bounded)->>> universeNonEmpty @Singleton-Singleton :| []--@since 0.7.0.0--}-universeNonEmpty :: forall a . (Bounded a, Enum a, Eq a) => NonEmpty a-universeNonEmpty- | minBound @a == maxBound = minBound :| []- | otherwise = minBound :| [succ minBound .. maxBound]-{-# INLINE universeNonEmpty #-}--{- | @inverseMap f@ creates a function that is the inverse of a given function-@f@. It does so by constructing 'M.Map' internally for each value @f a@. The-implementation makes sure that the 'M.Map' is constructed only once and then-shared for every call.--__Memory usage note:__ don't inverse functions that have types like 'Int'-as their result. In this case the created 'M.Map' will have huge size.--The complexity of reversed mapping is \(\mathcal{O}(\log n)\).--__Performance note:__ make sure to specialize monomorphic type of your functions-that use 'inverseMap' to avoid 'M.Map' reconstruction.--One of the common 'inverseMap' use-case is inverting the 'show' or a 'show'-like-function.-->>> data Color = Red | Green | Blue deriving (Show, Enum, Bounded)->>> parse = inverseMap show :: String -> Maybe Color->>> parse "Red"-Just Red->>> parse "Black"-Nothing--__Correctness note:__ 'inverseMap' expects /injective function/ as its argument,-i.e. the function must map distinct arguments to distinct values.--Typical usage of this function looks like this:--@-__data__ GhcVer- = Ghc802- | Ghc822- | Ghc844- | Ghc865- | Ghc881- __deriving__ ('Eq', 'Ord', 'Show', 'Enum', 'Bounded')--showGhcVer :: GhcVer -> 'Text'-showGhcVer = \\__case__- Ghc802 -> "8.0.2"- Ghc822 -> "8.2.2"- Ghc844 -> "8.4.4"- Ghc865 -> "8.6.5"- Ghc881 -> "8.8.1"--parseGhcVer :: 'Text' -> 'Maybe' GhcVer-parseGhcVer = 'inverseMap' showGhcVer-@--@since 0.1.1--}-inverseMap- :: forall a k .- (Bounded a, Enum a, Ord k)- => (a -> k)- -> (k -> Maybe a)-inverseMap f = \k -> M.lookup k dict- where- dict :: M.Map k a- dict = M.fromList $ fmapToFst f (universe @a)-{-# INLINE inverseMap #-}- {- | Like 'succ', but doesn't fail on 'maxBound'. Instead it returns 'minBound'. >>> next False@@ -154,10 +40,10 @@ {- | Like 'pred', but doesn't fail on 'minBound'. Instead it returns 'maxBound'. ->>> prev False-True >>> prev True False+>>> prev False+True >>> pred False *** Exception: Prelude.Enum.Bool.pred: bad argument
src/Relude/Extra/Foldable.hs view
@@ -1,7 +1,8 @@ {-# LANGUAGE Safe #-}+{-# LANGUAGE BangPatterns #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental@@ -14,11 +15,14 @@ module Relude.Extra.Foldable ( foldlSC+ , average ) where import Relude +-- $setup+ {- | A left-associative fold that's tail-recursive but can still short-circuit. Returning a 'Left' short-circuits and immediately returns the value inside. Returning a 'Right' continues the fold as usual with the value inside.@@ -38,3 +42,26 @@ Left l -> l Right r -> k r {-# INLINE foldlSC #-}++{- | Given a 'Foldable' of 'Fractional' elements, computes the average if+possible and returns 'Maybe' element.++>>> average [42]+Just 42.0+>>> average @Double [1, 2, 3, 4]+Just 2.5+>>> average @Float [1.5, 2.5, 3 ,4]+Just 2.75+>>> average []+Nothing++@since 1.0.0.0+-}+average :: forall a f . (Foldable f, Fractional a) => f a -> Maybe a+average xs+ | null xs = Nothing+ | otherwise = Just+ . uncurry (/)+ . foldl' (\(!total, !count) x -> (total + x, count + 1)) (0,0)+ $ xs+{-# INLINE average #-}
src/Relude/Extra/Foldable1.hs view
@@ -1,5 +1,6 @@ {-# OPTIONS_GHC -Wno-redundant-constraints #-} +{-# LANGUAGE BangPatterns #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE Safe #-} {-# LANGUAGE TypeFamilies #-}@@ -8,7 +9,7 @@ {- | Copyright: (c) 2011-2015 Edward Kmett- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental@@ -26,10 +27,11 @@ module Relude.Extra.Foldable1 ( Foldable1 (..) , foldl1'+ , average1 ) where import Relude hiding (Product (..), Sum (..))-import Relude.Extra.Newtype (( #. ))+import Relude.Extra.Newtype ((#.)) import Data.Functor.Product (Product (..)) import Data.Functor.Sum (Sum (..))@@ -67,6 +69,19 @@ fold1 :: Semigroup m => f m -> m fold1 = foldMap1 id + {- | Combines the elements of a non-empty structure using a binary function @f@.++ >>> foldr1 (+) 0 (1 :| [2, 3])+ 6+ >>> foldr1 (+) 1 $ Identity 3+ 4++ @since 1.0.0.0+ -}+ foldr1 :: (a -> b -> b) -> b -> f a -> b+ foldr1 f accum as = appEndo (foldMap1 (Endo #. f) as) accum+ {-# INLINE foldr1 #-}+ {- | Convert a non-empty data structure to a NonEmpty list. >>> toNonEmpty (Identity 2)@@ -107,6 +122,30 @@ minimum1 :: Ord a => f a -> a minimum1 = SG.getMin #. foldMap1 SG.Min + {- | The largest element of a non-empty data structure+ with respect to the given comparison function.++ >>> maximumOn1 abs (0 :| [2, 1, -3, -2])+ -3++ @since 1.0.0.0+ -}+ maximumOn1 :: Ord b => (a -> b) -> f a -> a+ maximumOn1 f = maximumOn1 f . toNonEmpty+ {-# INLINE maximumOn1 #-}++ {- | The smallest element of a non-empty data structure+ with respect to the given comparison function.++ >>> minimumOn1 abs (0 :| [2, 1, -3, -2])+ 0++ @since 1.0.0.0+ -}+ minimumOn1 :: Ord b => (a -> b) -> f a -> a+ minimumOn1 f = minimumOn1 f . toNonEmpty+ {-# INLINE minimumOn1 #-}+ {- | @since 0.3.0@@ -139,6 +178,24 @@ {-# INLINE maximum1 #-} {-# INLINE minimum1 #-} + maximumOn1 :: forall a b. Ord b => (a -> b) -> NonEmpty a -> a+ maximumOn1 func = foldl1' $ cmpOn+ where+ cmpOn :: a -> a -> a+ cmpOn a b = case func a `compare` func b of+ GT -> a+ _ -> b+ {-# INLINE maximumOn1 #-}++ minimumOn1 :: forall a b. Ord b => (a -> b) -> NonEmpty a -> a+ minimumOn1 func = foldl1' $ cmpOn+ where+ cmpOn :: a -> a -> a+ cmpOn a b = case func a `compare` func b of+ LT -> a+ _ -> b+ {-# INLINE minimumOn1 #-}+ {- | @since 0.3.0@@ -172,6 +229,14 @@ minimum1 = coerce {-# INLINE minimum1 #-} + maximumOn1 :: Ord b => (a -> b) -> Identity a -> a+ maximumOn1 = const coerce+ {-# INLINE maximumOn1 #-}++ minimumOn1 :: Ord b => (a -> b) -> Identity a -> a+ minimumOn1 = const coerce+ {-# INLINE minimumOn1 #-}+ {- | @since 0.3.0@@ -201,6 +266,12 @@ {-# INLINE maximum1 #-} {-# INLINE minimum1 #-} + maximumOn1, minimumOn1 :: Ord b => (a -> b) -> (c, a) -> a+ maximumOn1 = const snd+ minimumOn1 = const snd+ {-# INLINE maximumOn1 #-}+ {-# INLINE minimumOn1 #-}+ {- | @since 0.3.0@@ -279,6 +350,9 @@ foldMap1 :: Semigroup m => (a -> m) -> [a] -> m foldMap1 _ _ = error "Unreachable list instance of Foldable1" + foldr1 :: (Foldable1 f) => (a -> b -> b) -> b -> f a -> b+ foldr1 _ _ = error "Unreachable list instance of Foldable1"+ fold1 :: Semigroup m => [m] -> m fold1 _ = error "Unreachable list instance of Foldable1" @@ -297,6 +371,12 @@ minimum1 :: Ord a => [a] -> a minimum1 _ = error "Unreachable list instance of Foldable1" + maximumOn1 :: (Ord b, Foldable1 f) => (a -> b) -> f a -> a+ maximumOn1 _ _ = error "Unreachable list instance of Foldable1"++ minimumOn1 :: (Ord b, Foldable1 f) => (a -> b) -> f a -> a+ minimumOn1 _ _ = error "Unreachable list instance of Foldable1"+ {- | Strictly folds non-empty structure with given function @f@: @@@ -312,3 +392,18 @@ foldl1' _ (x :| []) = x foldl1' f (x :| (y:ys)) = foldl' f (f x y) ys {-# INLINE foldl1' #-}++{- | Given a 'Foldable1' of 'Fractional' elements, computes the average if+possible and returns the resulting element.+++>>> average1 (42 :| [])+42.0+>>> average1 (1 :| [2,3,4])+2.5++@since 1.0.0.0+-}+average1 :: forall a f . (Foldable1 f, Fractional a) => f a -> a+average1 = uncurry (/) . foldl' (\(!total, !count) x -> (total + x, count + 1)) (0,0)+{-# INLINE average1 #-}
src/Relude/Extra/Group.hs view
@@ -2,7 +2,7 @@ {-# LANGUAGE TypeFamilies #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental
src/Relude/Extra/Lens.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2013-2016 Edward Kmett- (c) 2019-2020 Kowainik+ (c) 2019-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental
src/Relude/Extra/Map.hs view
@@ -2,7 +2,7 @@ {-# LANGUAGE TypeFamilies #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental
src/Relude/Extra/Newtype.hs view
@@ -2,7 +2,7 @@ {-# LANGUAGE Safe #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental
src/Relude/Extra/Tuple.hs view
@@ -2,7 +2,7 @@ {-# LANGUAGE TupleSections #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental@@ -26,8 +26,13 @@ , traverseBoth ) where -import Relude+import Relude.Function ((.))+import Relude.Functor (Functor (..), (<$>))+import Relude.Applicative (Applicative (..)) ++-- $setup+-- >>> import Relude {- | Creates a tuple by pairing something with itself.
src/Relude/Extra/Type.hs view
@@ -9,7 +9,7 @@ {-# LANGUAGE UndecidableInstances #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Experimental@@ -31,11 +31,7 @@ import Relude -#if ( __GLASGOW_HASKELL__ >= 802 ) import Type.Reflection (typeRep)-#else-import Data.Typeable (typeRep)-#endif -- $setup@@ -57,11 +53,7 @@ @since 0.4.0 -} typeName :: forall a. Typeable a => Text-#if ( __GLASGOW_HASKELL__ >= 802 ) typeName = show (typeRep @a)-#else-typeName = show (typeRep (Proxy @a))-#endif {-# INLINE typeName #-} {- | Concatenates type-level lists.
− src/Relude/Extra/Validation.hs
@@ -1,432 +0,0 @@-{-# OPTIONS_GHC -Wno-redundant-constraints #-}--{-# LANGUAGE CPP #-}-{-# LANGUAGE ConstraintKinds #-}-{-# LANGUAGE DataKinds #-}-{-# LANGUAGE LambdaCase #-}-{-# LANGUAGE Safe #-}-{-# LANGUAGE TypeFamilies #-}-{-# LANGUAGE TypeOperators #-}-{-# LANGUAGE UndecidableInstances #-}--#if __GLASGOW_HASKELL__ > 802-{-# LANGUAGE DerivingStrategies #-}-#endif--{- |-Copyright: (c) 2014 Chris Allen, Edward Kmett- (c) 2018-2020 Kowainik-SPDX-License-Identifier: MIT-Maintainer: Kowainik <xrom.xkov@gmail.com>-Stability: Experimental-Portability: Portable--@since 0.4.0--= Attention--__⚠️ Warning ⚠️__--This module is now deprecated @since 0.7.0.0.-The whole module will be removed in the upcoming release.--== Migration rules--The module is deprecated in favour of-[validation-selective](https://hackage.haskell.org/package/validation-selective).-The package has the identical functionality, so can be easily migrated to.--If you use "Relude.Extra.Validation" in you project you need to:--1. Add @validation-selective@ into the @build-depends@ section of your @.cabal@- file.-2. Change imports of "Relude.Extra.Validation" to "Validation":-- @- -- Was:- import Relude.Extra.Validation (Validation (..), ..)- -- Become:- import Validation (Validation (..), ..)- @--= Description--'Validation' is a monoidal sibling to 'Either' but 'Validation' doesn't have a-'Monad' instance. 'Validation' allows to accumulate all errors instead of-short-circuiting on the first error so you can display all possible errors at-once. Common use-cases include:--1. Validating each input of a form with multiple inputs.-2. Performing multiple validations of a single value.--Instances of different standard typeclasses provide various semantics:--1. 'Functor': change the type inside 'Success'.-2. 'Bifunctor': change both 'Failure' and 'Success'.-3. 'Applicative': apply function to values inside 'Success' and accumulate- errors inside 'Failure'.-4. 'Semigroup': accumulate both 'Failure' and 'Success' with '<>'.-5. 'Monoid': 'Success' that shores 'mempty'.-6. 'Alternative': return first 'Success' or accumulate all errors inside 'Failure'.--}--module Relude.Extra.Validation- ( -- * How to use- -- $use- Validation(..)- , validationToEither- , eitherToValidation- ) where--import GHC.TypeLits (ErrorMessage (..), TypeError)--import Relude---- >>> $setup--- >>> import Relude--{- $use--Take for example a type @Computer@ that needs to be validated:-->>> :{-data Computer = Computer- { computerRam :: !Int -- ^ Ram in Gigabytes- , computerCpus :: !Int- } deriving (Eq, Show)-:}--You can validate that the computer has a minimum of 16GB of RAM:-->>> :{-validateRam :: Int -> Validation [Text] Int-validateRam ram- | ram >= 16 = Success ram- | otherwise = Failure ["Not enough RAM"]-:}--and that the processor has at least two CPUs:-->>> :{-validateCpus :: Int -> Validation [Text] Int-validateCpus cpus- | cpus >= 2 = Success cpus- | otherwise = Failure ["Not enough CPUs"]-:}--You can use these functions with the 'Applicative' instance of the 'Validation'-type to construct a validated @Computer@. You will get either (pun intended) a-valid @Computer@ or the errors that prevent it from being considered valid.--Like so:-->>> :{-mkComputer :: Int -> Int -> Validation [Text] Computer-mkComputer ram cpus = Computer- <$> validateRam ram- <*> validateCpus cpus-:}--Using @mkComputer@ we get a @Success Computer@ or a list with all possible errors:-->>> mkComputer 16 2-Success (Computer {computerRam = 16, computerCpus = 2})-->>> mkComputer 16 1-Failure ["Not enough CPUs"]-->>> mkComputer 15 2-Failure ["Not enough RAM"]-->>> mkComputer 15 1-Failure ["Not enough RAM","Not enough CPUs"]--}---- | 'Validation' is 'Either' with a 'Left' that is a 'Semigroup'.-data Validation e a- = Failure e- | Success a-#if __GLASGOW_HASKELL__ > 802- deriving stock (Eq, Ord, Show)-#else- deriving (Eq, Ord, Show)-#endif-{-# DEPRECATED Validation "Use 'Validation' from 'validation-selective' instead"#-}--instance Functor (Validation e) where- fmap :: (a -> b) -> Validation e a -> Validation e b- fmap _ (Failure e) = Failure e- fmap f (Success a) = Success (f a)- {-# INLINE fmap #-}-- (<$) :: a -> Validation e b -> Validation e a- x <$ Success _ = Success x- _ <$ Failure e = Failure e- {-# INLINE (<$) #-}--{- | This instances covers the following cases:--1. Both 'Success': combine values inside 'Success' with '<>'.-2. Both 'Failure': combine values inside 'Failure' with '<>'.-3. One 'Success', one 'Failure': return 'Failure'.--__Examples__-->>> success1 = Success [42] :: Validation [Text] [Int]->>> success2 = Success [69] :: Validation [Text] [Int]->>> failure1 = Failure ["WRONG"] :: Validation [Text] [Int]->>> failure2 = Failure ["FAIL"] :: Validation [Text] [Int]-->>> success1 <> success2-Success [42,69]-->>> failure1 <> failure2-Failure ["WRONG","FAIL"]-->>> success1 <> failure1-Failure ["WRONG"]--@since 0.6.0.0--}-instance (Semigroup e, Semigroup a) => Semigroup (Validation e a) where- (<>) :: Validation e a -> Validation e a -> Validation e a- (<>) = liftA2 (<>)- {-# INLINE (<>) #-}--{- | 'mempty' is @'Success' 'mempty'@.--@since 0.6.0.0--}-instance (Semigroup e, Semigroup a, Monoid a) => Monoid (Validation e a) where- mempty :: Validation e a- mempty = Success mempty- {-# INLINE mempty #-}-- mappend :: Validation e a -> Validation e a -> Validation e a- mappend = (<>)- {-# INLINE mappend #-}--{- | This instance is the most important instance for the 'Validation' data-type. It's responsible for the many implementations. And it allows to accumulate-errors while performing validation or combining the results in the applicative-style.--__Examples__-->>> success1 = Success 42 :: Validation [Text] Int->>> success2 = Success 69 :: Validation [Text] Int->>> successF = Success (* 2) :: Validation [Text] (Int -> Int)->>> failure1 = Failure ["WRONG"] :: Validation [Text] Int->>> failure2 = Failure ["FAIL"] :: Validation [Text] Int-->>> successF <*> success1-Success 84-->>> successF <*> failure1-Failure ["WRONG"]-->>> (+) <$> success1 <*> success2-Success 111-->>> (+) <$> failure1 <*> failure2-Failure ["WRONG","FAIL"]-->>> liftA2 (+) success1 failure1-Failure ["WRONG"]-->>> liftA3 (,,) failure1 success1 failure2-Failure ["WRONG","FAIL"]--Implementations of all functions are lazy and they correctly work if some-arguments are not fully evaluated.-->>> :{-isFailure :: Validation e a -> Bool-isFailure (Failure _) = True-isFailure (Success _) = False-:}-->>> failure1 *> failure2-Failure ["WRONG","FAIL"]->>> isFailure $ failure1 *> failure2-True->>> epicFail = error "Impossible validation" :: Validation [Text] Int->>> isFailure $ failure1 *> epicFail-True--}-instance Semigroup e => Applicative (Validation e) where- pure :: a -> Validation e a- pure = Success- {-# INLINE pure #-}-- (<*>) :: Validation e (a -> b) -> Validation e a -> Validation e b- Failure e <*> b = Failure $ case b of- Failure e' -> e <> e'- Success _ -> e- Success _ <*> Failure e = Failure e- Success f <*> Success a = Success (f a)- {-# INLINE (<*>) #-}-- (*>) :: Validation e a -> Validation e b -> Validation e b- Failure e *> b = Failure $ case b of- Failure e' -> e <> e'- Success _ -> e- Success _ *> Failure e = Failure e- Success _ *> Success b = Success b- {-# INLINE (*>) #-}-- (<*) :: Validation e a -> Validation e b -> Validation e a- Failure e <* b = Failure $ case b of- Failure e' -> e <> e'- Success _ -> e- Success _ <* Failure e = Failure e- Success a <* Success _ = Success a- {-# INLINE (<*) #-}---{- | This instance implements the following behavior for the binary operator:--1. Both 'Failure': combine values inside 'Failure' using '<>'.-2. At least is 'Success': return the left 'Success' (the earliest 'Success').-3. 'empty' is @'Failure' 'mempty'@.--__Examples__-->>> success1 = Success [42] :: Validation [Text] [Int]->>> success2 = Success [69] :: Validation [Text] [Int]->>> failure1 = Failure ["WRONG"] :: Validation [Text] [Int]->>> failure2 = Failure ["FAIL"] :: Validation [Text] [Int]-->>> success1 <|> success2-Success [42]->>> failure1 <|> failure2-Failure ["WRONG","FAIL"]->>> failure2 <|> success2-Success [69]--}-instance (Semigroup e, Monoid e) => Alternative (Validation e) where- empty :: Validation e a- empty = Failure mempty- {-# INLINE empty #-}-- (<|>) :: Validation e a -> Validation e a -> Validation e a- s@Success{} <|> _ = s- _ <|> s@Success{} = s- Failure e <|> Failure e' = Failure (e <> e')- {-# INLINE (<|>) #-}--instance Foldable (Validation e) where- fold :: Monoid m => Validation e m -> m- fold (Success a) = a- fold (Failure _) = mempty- {-# INLINE fold #-}-- foldMap :: Monoid m => (a -> m) -> Validation e a -> m- foldMap _ (Failure _) = mempty- foldMap f (Success a) = f a- {-# INLINE foldMap #-}-- foldr :: (a -> b -> b) -> b -> Validation e a -> b- foldr f x (Success a) = f a x- foldr _ x (Failure _) = x- {-# INLINE foldr #-}--instance Traversable (Validation e) where- traverse :: Applicative f => (a -> f b) -> Validation e a -> f (Validation e b)- traverse f (Success a) = Success <$> f a- traverse _ (Failure e) = pure (Failure e)- {-# INLINE traverse #-}-- sequenceA :: Applicative f => Validation e (f a) -> f (Validation e a)- sequenceA = traverse id- {-# INLINE sequenceA #-}--instance Bifunctor Validation where- bimap :: (e -> d) -> (a -> b) -> Validation e a -> Validation d b- bimap f _ (Failure e) = Failure (f e)- bimap _ g (Success a) = Success (g a)- {-# INLINE bimap #-}-- first :: (e -> d) -> Validation e a -> Validation d a- first f (Failure e) = Failure (f e)- first _ (Success a) = Success a- {-# INLINE first #-}-- second :: (a -> b) -> Validation e a -> Validation e b- second _ (Failure e) = Failure e- second g (Success a) = Success (g a)- {-# INLINE second #-}--#if MIN_VERSION_base(4,10,0)-instance Bifoldable Validation where- bifoldMap :: Monoid m => (e -> m) -> (a -> m) -> Validation e a -> m- bifoldMap f _ (Failure e) = f e- bifoldMap _ g (Success a) = g a- {-# INLINE bifoldMap #-}--instance Bitraversable Validation where- bitraverse :: Applicative f- => (e -> f d) -> (a -> f b) -> Validation e a -> f (Validation d b)- bitraverse f _ (Failure e) = Failure <$> f e- bitraverse _ g (Success a) = Success <$> g a- {-# INLINE bitraverse #-}-#endif--{- | Transform a 'Validation' into an 'Either'.-->>> validationToEither (Success "whoop")-Right "whoop"-->>> validationToEither (Failure "nahh")-Left "nahh"--}-validationToEither :: Validation e a -> Either e a-validationToEither = \case- Failure e -> Left e- Success a -> Right a-{-# INLINE validationToEither #-}-{-# DEPRECATED validationToEither "Use 'validationToEither' from 'validation-selective' instead"#-}--{- | Transform an 'Either' into a 'Validation'.-->>> eitherToValidation (Right "whoop")-Success "whoop"-->>> eitherToValidation (Left "nahh")-Failure "nahh"--}-eitherToValidation :: Either e a -> Validation e a-eitherToValidation = \case- Left e -> Failure e- Right a -> Success a-{-# INLINE eitherToValidation #-}-{-# DEPRECATED eitherToValidation "Use 'eitherToValidation' from 'validation-selective' instead"#-}--------------------------------------------------------------------------------- Custom errors-------------------------------------------------------------------------------{- | ⚠️__CAUTION__⚠️ This instance is for custom error display only.--It's not possible to implement lawful 'Monad' instance for 'Validation'.--In case it is used by mistake, the user will see the following:-->>> Success 42 >>= \n -> if even n then Success n else Failure ["Not even"]-...-... Type 'Validation' doesn't have lawful 'Monad' instance- which means that you can't use 'Monad' methods with 'Validation'.-...--@since 0.6.0.0--}-instance (NoValidationMonadError, Semigroup e) => Monad (Validation e) where- return = error "Unreachable Validation instance of Monad"- (>>=) = error "Unreachable Validation instance of Monad"---- | Helper type family to produce error messages-type family NoValidationMonadError :: Constraint where- NoValidationMonadError = TypeError- ( 'Text "Type 'Validation' doesn't have lawful 'Monad' instance"- ':$$: 'Text "which means that you can't use 'Monad' methods with 'Validation'."- )
src/Relude/File.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE Safe #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Foldable.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE Safe #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Foldable/Fold.hs view
@@ -10,7 +10,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -18,7 +18,7 @@ Fixes and additions to 'Foldable'. Specifically: -* Space-leak free 'sum' and 'production'+* Space-leak free 'sum' and 'product' * 'elem' and 'notElem' are forbidden for 'Set' and 'HashSet' * Additional combinators for common idioms -}@@ -72,7 +72,7 @@ flipfoldl' f = foldl' (flip f) {-# INLINE flipfoldl' #-} -{- | Alternative version of @asum@.+{- | Alternative version of 'Relude.asum' that takes a function to map over. >>> asumMap (\x -> if x > 2 then Just x else Nothing) [1..4] Just 3@@ -83,18 +83,22 @@ asumMap = coerce (foldMap :: (a -> Alt m b) -> f a -> Alt m b) {-# INLINE asumMap #-} -{- | Polymorphic version of @concatMapA@ function.+{- | Polymorphic version of the 'Relude.concatMapA' function. >>> foldMapA @[Int] (Just . replicate 3) [1..3] Just [1,1,1,2,2,2,3,3,3] @since 0.1.0 -}-foldMapA :: forall b m f a . (Semigroup b, Monoid b, Applicative m, Foldable f) => (a -> m b) -> f a -> m b+foldMapA+ :: forall b m f a . (Semigroup b, Monoid b, Applicative m, Foldable f)+ => (a -> m b)+ -> f a+ -> m b foldMapA = coerce (foldMap :: (a -> Ap m b) -> f a -> Ap m b) {-# INLINE foldMapA #-} -{- | Polymorphic version of @concatMapM@ function.+{- | Polymorphic version of the 'Relude.concatMapM' function. >>> foldMapM @[Int] (Just . replicate 3) [1..3] Just [1,1,1,2,2,2,3,3,3]
src/Relude/Foldable/Reexport.hs view
@@ -2,7 +2,7 @@ {-# LANGUAGE Safe #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -15,11 +15,9 @@ ( -- * @Foldable@ reexports module Data.Foldable , module Data.Traversable-#if MIN_VERSION_base(4,10,0) -- * @Bi@ reexports , module Data.Bifoldable , module Data.Bitraversable-#endif ) where import Data.Foldable (Foldable (fold, foldMap, foldl', foldr, length, null, toList), all, and, any,@@ -29,9 +27,7 @@ import Data.Foldable (foldMap') #endif import Data.Traversable (Traversable (..), forM, mapAccumL, mapAccumR)-#if MIN_VERSION_base(4,10,0) import Data.Bifoldable (Bifoldable (..), biList, biall, biand, biany, biasum, bielem, bifind, bifoldl', bifoldlM, bifoldr', bifoldrM, bifor_, bilength, binull, bior, bisequence_, bitraverse_) import Data.Bitraversable (Bitraversable (..), bifoldMapDefault, bifor, bimapDefault, bisequence)-#endif
src/Relude/Function.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Functor.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Functor/Fmap.hs view
@@ -4,7 +4,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -29,7 +29,12 @@ #else --- | Flipped version of '<$>'.+{- | Flipped version of '<$>'.++@+f <$> a = a <&> f+@+-} infixl 1 <&> (<&>) :: Functor f => f a -> (a -> b) -> f b as <&> f = f <$> as
src/Relude/Functor/Reexport.hs view
@@ -4,7 +4,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Lifted.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -72,6 +72,10 @@ -- $file , module Relude.Lifted.Terminal -- $terminal+ , module Relude.Lifted.Handle+ -- $handle+ , module Relude.Lifted.Env+ -- $env ) where import Relude.Lifted.Concurrent@@ -79,6 +83,8 @@ import Relude.Lifted.File import Relude.Lifted.IORef import Relude.Lifted.Terminal+import Relude.Lifted.Handle+import Relude.Lifted.Env {- $concurrent Lifted 'MVar' and 'STM' functions.@@ -94,4 +100,10 @@ -} {- $terminal Lifted functions to work with stdin and stdout.+-}+{- $handle+Lifted functions to work with 'Relude.Base.IO' 'Handle's.+-}+{- $env+Lifted functions to work with system environment. -}
src/Relude/Lifted/Concurrent.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
+ src/Relude/Lifted/Env.hs view
@@ -0,0 +1,41 @@+{- |+Copyright: (c) 2020-2021 Kowainik+SPDX-License-Identifier: MIT+Maintainer: Kowainik <xrom.xkov@gmail.com>+Stability: Stable+Portability: Portable++Lifted functions to work with system environment.++@since 1.0.0.0+-}++module Relude.Lifted.Env+ ( getArgs+ , lookupEnv+ ) where++import Relude.Base (IO)+import Relude.Function ((.))+import Relude.Monad.Reexport (MonadIO (..), Maybe)+import Relude.String.Reexport (String)++import qualified System.Environment as ENV (getArgs, lookupEnv)++{- | Lifted version of 'System.Environment.getArgs'.++@since 1.0.0.0+-}+getArgs :: MonadIO m => m [String]+getArgs = liftIO ENV.getArgs+{-# SPECIALIZE getArgs :: IO [String] #-}+{-# INLINE getArgs #-}++{- | Lifted version of 'System.Environment.lookupEnv'.++@since 1.0.0.0+-}+lookupEnv :: MonadIO m => String -> m (Maybe String)+lookupEnv = liftIO . ENV.lookupEnv+{-# SPECIALIZE lookupEnv :: String -> IO (Maybe String) #-}+{-# INLINE lookupEnv #-}
src/Relude/Lifted/Exit.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Lifted/File.hs view
@@ -1,9 +1,10 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -14,6 +15,9 @@ module Relude.Lifted.File ( readFile+#if ( __GLASGOW_HASKELL__ >= 900 )+ , readFile'+#endif , writeFile , appendFile ) where@@ -31,6 +35,17 @@ readFile = liftIO . IO.readFile {-# SPECIALIZE readFile :: FilePath -> IO String #-} {-# INLINE readFile #-}++#if ( __GLASGOW_HASKELL__ >= 900 )+{- | Lifted version of 'IO.readFile''. Strict version of 'readFile'.++@since 1.0.0.0+-}+readFile' :: MonadIO m => FilePath -> m String+readFile' = liftIO . IO.readFile'+{-# SPECIALIZE readFile' :: FilePath -> IO String #-}+{-# INLINE readFile' #-}+#endif -- | Lifted version of 'IO.writeFile'. writeFile :: MonadIO m => FilePath -> String -> m ()
+ src/Relude/Lifted/Handle.hs view
@@ -0,0 +1,67 @@+{- |+Copyright: (c) 2020-2021 Kowainik+SPDX-License-Identifier: MIT+Maintainer: Kowainik <xrom.xkov@gmail.com>+Stability: Stable+Portability: Portable++Lifted functions to work with 'IO' 'Handle's.++@since 1.0.0.0+-}++module Relude.Lifted.Handle+ ( hFlush+ , hIsEOF+ , hSetBuffering+ , hGetBuffering+ , Handle+ , stdin+ , stdout+ , stderr+ , withFile+ , BufferMode (..)+ ) where++import Relude.Base (IO)+import Relude.Function ((.))+import Relude.Bool (Bool (..))+import Relude.Monad.Reexport (MonadIO (..))+import qualified System.IO as IO (Handle, hFlush, hIsEOF, hSetBuffering, hGetBuffering, BufferMode)+import System.IO (Handle, stdin, stdout, stderr, withFile, BufferMode (..))++{- | Lifted version of 'IO.hFlush'.++@since 1.0.0.0+-}+hFlush :: MonadIO m => IO.Handle -> m ()+hFlush = liftIO . IO.hFlush+{-# SPECIALIZE hFlush :: IO.Handle -> IO () #-}+{-# INLINE hFlush #-}++{- | Lifted version of 'IO.hIsEOF'.++@since 1.0.0.0+-}+hIsEOF :: MonadIO m => IO.Handle -> m Bool+hIsEOF = liftIO . IO.hIsEOF+{-# SPECIALIZE hIsEOF :: IO.Handle -> IO Bool #-}+{-# INLINE hIsEOF #-}++{- | Lifted version of 'IO.hSetBuffering'.++@since 1.0.0.0+-}+hSetBuffering :: MonadIO m => IO.Handle -> IO.BufferMode -> m ()+hSetBuffering h = liftIO . IO.hSetBuffering h+{-# SPECIALIZE hSetBuffering :: IO.Handle -> IO.BufferMode -> IO () #-}+{-# INLINE hSetBuffering #-}++{- | Lifted version of 'IO.hGetBuffering'.++@since 1.0.0.0+-}+hGetBuffering :: MonadIO m => IO.Handle -> m IO.BufferMode+hGetBuffering = liftIO . IO.hGetBuffering+{-# SPECIALIZE hGetBuffering :: IO.Handle -> IO IO.BufferMode #-}+{-# INLINE hGetBuffering #-}
src/Relude/Lifted/IORef.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Lifted/Terminal.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE Safe #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/List.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -18,14 +18,17 @@ , module Relude.List.NonEmpty -- $nonempty , (!!?)+ , maybeAt+ , partitionWith ) where import Relude.Base ((<)) import Relude.Bool (otherwise)+import Relude.Function (flip, (.)) import Relude.List.NonEmpty import Relude.List.Reexport-import Relude.Monad (Maybe (..))+import Relude.Monad (Either, Maybe (..), partitionEithers) import Relude.Numeric (Int, (-)) @@ -33,8 +36,9 @@ -- >>> import Relude {- | Safer version of 'Relude.Unsafe.!!', returns a Maybe.-get element from list using index value starting from `0`. +Get element from list using index value starting from `0`.+ >>> [] !!? 0 Nothing @@ -60,6 +64,49 @@ go j (_:ys) = go (j - 1) ys go _ [] = Nothing {-# INLINE (!!?) #-}++{- | '!!?' with its arguments flipped.++Get element from list using index value starting from `0`.++>>> maybeAt 0 []+Nothing++>>> maybeAt 3 ["a", "b", "c"]+Nothing++>>> maybeAt (-1) [1, 2, 3]+Nothing++>>> maybeAt 2 ["a", "b", "c"]+Just "c"++@since 1.0.0.0+-}+maybeAt :: Int -> [a] -> Maybe a+maybeAt = flip (!!?)+{-# INLINE maybeAt #-}++{- | Partitions a list based on the result of function which produces an Either+value. List of all elements producing Left are extracted, in order, to the first+element of the output tuple. Similarly, a list of all elements producing Right+are extracted to the second element of output.++>>> :{+ divideEvenOrShow :: Int -> Either Int String+ divideEvenOrShow n+ | even n = Left $ n `div` 2+ | otherwise = Right $ "Odd: " <> show n+ :}++>>> partitionWith divideEvenOrShow [1 .. 6]+([1,2,3],["Odd: 1","Odd: 3","Odd: 5"])++@since 1.0.0.0+-}+partitionWith :: (a -> Either b c) -> [a] -> ([b], [c])+partitionWith f = partitionEithers . map f+{-# INLINE partitionWith #-} {- $reexport Most of the "Data.List" types and function.
src/Relude/List/NonEmpty.hs view
@@ -8,7 +8,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/List/Reexport.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -22,7 +22,7 @@ import Data.List (break, drop, dropWhile, filter, genericDrop, genericLength, genericReplicate, genericSplitAt, genericTake, group, inits, intercalate, intersperse, isPrefixOf, iterate, map, permutations, repeat, replicate, reverse, scanl, scanl', scanl1,- scanr, scanr1, sort, sortBy, sortOn, splitAt, subsequences, tails, take,+ scanr, scanr1, sort, sortBy, sortOn, span, splitAt, subsequences, tails, take, takeWhile, transpose, uncons, unfoldr, unzip, unzip3, zip, zip3, zipWith, (++)) import GHC.Exts (sortWith)
src/Relude/Monad.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -22,12 +22,15 @@ , module Relude.Monad.Trans -- $trans , chainedTo+ , infinitely ) where import Relude.Monad.Either import Relude.Monad.Maybe import Relude.Monad.Reexport import Relude.Monad.Trans+import Control.Applicative (Applicative)+import Relude.Base (Void) -- $setup@@ -46,6 +49,31 @@ chainedTo :: Monad m => (a -> m b) -> m a -> m b chainedTo = (=<<) {-# INLINE chainedTo #-}++{- | Repeat a monadic action indefinitely.++This is a more type safe version of 'forever', which has a convinient+but unsafe type.++Consider the following two examples. In the @getIntForever@ functions, it+falsely expects 'Int' as the result of the 'forever' function. But it would need+to wait *forever* to get that, and this mistake won't be caught by the type+system and compiler:++@+getIntForever :: IO Int+getIntForever = do+ i <- forever $ do ...+ pure i+@++In contrast, using 'infinitely' instead of 'forever' in 'foo' is a type error.++@since 1.0.0.0+-}+infinitely :: Applicative f => f a -> f Void+infinitely = forever+{-# INLINE infinitely #-} {- $reexport Reexports functions to work with different monads.
src/Relude/Monad/Either.hs view
@@ -1,12 +1,11 @@ {-# OPTIONS_GHC -fno-warn-orphans #-} -{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -37,6 +36,7 @@ import Control.Applicative (Applicative) import Control.Monad (Monad (..))+import Data.Either (fromLeft, fromRight) import Data.Function (const) import Data.Maybe (Maybe (..), maybe) @@ -44,32 +44,6 @@ import Relude.Function ((.)) import Relude.Monad.Reexport (Either (..), MonadFail (..), either) import Relude.String.Reexport (IsString (..), String)--#if MIN_VERSION_base(4,10,0)-import Data.Either (fromLeft, fromRight)-#else----- | Extracts value from 'Left' or return given default value.------ >>> fromLeft 0 (Left 3)--- 3--- >>> fromLeft 0 (Right 5)--- 0-fromLeft :: a -> Either a b -> a-fromLeft _ (Left a) = a-fromLeft a (Right _) = a---- | Extracts value from 'Right' or return given default value.------ >>> fromRight 0 (Left 3)--- 0--- >>> fromRight 0 (Right 5)--- 5-fromRight :: b -> Either a b -> b-fromRight b (Left _) = b-fromRight _ (Right b) = b-#endif -- $setup
src/Relude/Monad/Maybe.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Monad/Reexport.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Monad/Trans.hs view
@@ -1,10 +1,9 @@-{-# LANGUAGE CPP #-} {-# LANGUAGE Trustworthy #-} {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -17,10 +16,7 @@ ( -- * Convenient functions to work with 'Reader' monad usingReader , usingReaderT--#if MIN_VERSION_base(4,10,0) , etaReaderT-#endif -- * Convenient functions to work with 'State' monad , evaluatingState@@ -35,9 +31,7 @@ , hoistEither ) where -#if MIN_VERSION_base(4,10,0) import GHC.Exts (oneShot)-#endif import Relude.Applicative (Applicative (pure)) import Relude.Container.Reexport (fst, snd)@@ -68,7 +62,6 @@ usingReader = flip runReader {-# INLINE usingReader #-} -#if MIN_VERSION_base(4,10,0) {- | This function helps with optimizing performance when working with the 'ReaderT' transformer. If you have code like below, that is called in a loop@@ -99,7 +92,6 @@ etaReaderT :: ReaderT r m a -> ReaderT r m a etaReaderT = ReaderT . oneShot . runReaderT {-# INLINE etaReaderT #-}-#endif {- | Shorter and more readable alias for @flip runStateT@.
src/Relude/Monoid.hs view
@@ -10,7 +10,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -47,7 +47,8 @@ import GHC.Generics (Generic1) import Relude.Applicative (Alternative, Applicative (..), liftA2)-import Relude.Base (Bounded (..), Enum, Eq, Generic, Ord, Show)+import Relude.Base (Eq, Generic, Ord, Show)+import Relude.Enum (Bounded (..), Enum) import Relude.Function (($), (.)) import Relude.Functor.Reexport (Functor (..)) import Relude.Monad.Reexport (Monad, MonadFail, MonadPlus)
src/Relude/Nub.hs view
@@ -1,9 +1,10 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -27,6 +28,10 @@ * 'hashNub' is the fastest with 'Data.Text.Text'. + * 'intNub' is faster when you work with lists of 'Int's.++ * 'intNubOn' is fast with the lists of type that can have fixed number representations.+ * 'sortNub' has better performance than 'ordNub' but should be used when sorting is also needed. * 'unstableNub' has better performance than 'hashNub' but doesn't save the original order.@@ -35,6 +40,13 @@ module Relude.Nub ( hashNub , ordNub++#if __GLASGOW_HASKELL__ > 804+ , ordNubOn+ , intNub+ , intNubOn+#endif+ , sortNub , unstableNub ) where@@ -43,18 +55,31 @@ import Data.Hashable (Hashable) import Data.HashSet as HashSet import Data.Ord (Ord)-import Prelude ((.))+import Prelude (Int, (.)) import qualified Data.Set as Set+#if __GLASGOW_HASKELL__ > 804+import qualified Data.Containers.ListUtils as Containers+#endif -{- | Like 'Prelude.nub' but runs in \( O(n \log n) \) time and requires 'Ord'.+-- $setup+-- >>> import Prelude (div, fromEnum) +{- | Removes duplicate elements from a list, keeping only the first occurance of+the element.++Like 'Prelude.nub' but runs in \( O(n \log n) \) time and requires 'Ord'.+ >>> ordNub [3, 3, 3, 2, 2, -1, 1] [3,2,-1,1] -} ordNub :: forall a . (Ord a) => [a] -> [a]+#if __GLASGOW_HASKELL__ > 804+ordNub = Containers.nubOrd+{-# INLINE ordNub #-}+#else ordNub = go Set.empty where go :: Set.Set a -> [a] -> [a]@@ -64,7 +89,23 @@ then go s xs else x : go (Set.insert x s) xs {-# INLINEABLE ordNub #-}+#endif ++#if __GLASGOW_HASKELL__ > 804+{- | Similar to 'ordNub' but performs nub through the mapped list on the given+function.++>>> ordNubOn (`div` 10) [3, 3, 3, 13, 2, 22, -1, 1, 66]+[3,13,22,-1,66]++@since 1.0.0.0+-}+ordNubOn :: forall b a . (Ord b) => (a -> b) -> [a] -> [a]+ordNubOn = Containers.nubOrdOn+{-# INLINE ordNubOn #-}+#endif+ {- | Like 'Prelude.nub' but runs in \( O(n \log_{16} n) \) time and requires 'Hashable'. >>> hashNub [3, 3, 3, 2, 2, -1, 1]@@ -101,3 +142,34 @@ unstableNub :: (Eq a, Hashable a) => [a] -> [a] unstableNub = HashSet.toList . HashSet.fromList {-# INLINE unstableNub #-}+++#if __GLASGOW_HASKELL__ > 804++{- | Removes duplicate elements from a list, keeping only the first occurance of+the element.++Like 'Prelude.nub' but runs in \( O(n \min\(n, int_bits\)) \) time and requires 'Ord'.++>>> intNub [3, 3, 3, 2, 2, -1, 1]+[3,2,-1,1]++@since 1.0.0.0+-}+intNub :: [Int] -> [Int]+intNub = Containers.nubInt++{-# INLINE intNub #-}++{- | Similar to 'intNub' but works on lists of any types by performing "nubbing" through 'Int's.++>>> intNubOn fromEnum "ababbbcdaffee"+"abcdfe"++@since 1.0.0.0+-}+intNubOn :: (a -> Int) -> [a] -> [a]+intNubOn = Containers.nubIntOn+{-# INLINE intNubOn #-}++#endif
src/Relude/Numeric.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE Trustworthy #-} {- |-Copyright: (c) 2018-2020 Kowainik+Copyright: (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable@@ -38,8 +38,9 @@ denominator, even, fromIntegral, gcd, lcm, numerator, odd, realToFrac, (^), (^^)) import Numeric.Natural (Natural) -import Relude.Base (Bounded (..), (<), (>))+import Relude.Base ((<), (>)) import Relude.Bool (otherwise)+import Relude.Enum (Bounded (..)) import Relude.Function (($)) import Relude.Monad (Maybe (..)) @@ -55,6 +56,19 @@ >>> integerToBounded @Int8 1024 Nothing++>>> integerToBounded @Int (toInteger (minBound :: Int))+Just (-9223372036854775808)+>>> integerToBounded @Int $ (toInteger (minBound :: Int)) - 1+Nothing++>>> integerToBounded @Int (toInteger (maxBound :: Int))+Just 9223372036854775807+>>> integerToBounded @Int $ (toInteger (maxBound :: Int)) + 1+Nothing++If you want to convert 'Int' or 'Word' to a bounded type, take a look at+'toIntegralSized' function instead. @since 0.5.0 -}
src/Relude/Print.hs view
@@ -6,7 +6,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/String.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/String/Conversion.hs view
@@ -12,7 +12,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/String/Reexport.hs view
@@ -9,7 +9,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
src/Relude/Unsafe.hs view
@@ -3,7 +3,7 @@ {- | Copyright: (c) 2016 Stephen Diehl (c) 2016-2018 Serokell- (c) 2018-2020 Kowainik+ (c) 2018-2021 Kowainik SPDX-License-Identifier: MIT Maintainer: Kowainik <xrom.xkov@gmail.com> Stability: Stable
− test/Test/Relude/Extra/Validation/Property.hs
@@ -1,168 +0,0 @@-{- HLINT ignore "Alternative law, right identity" -}-{- HLINT ignore "Alternative law, left identity" -}-{- HLINT ignore "Monoid law, right identity" -}-{- HLINT ignore "Monoid law, left identity" -}-{- HLINT ignore "Use <$>" -}--module Test.Relude.Extra.Validation.Property- ( validationLaws- ) where--import Relude-import Relude.Extra.Validation--import Hedgehog (Gen, Group (..), Property, forAll, forAllWith, property, (===))--import qualified Hedgehog.Gen as Gen-import qualified Hedgehog.Range as Range--validationLaws :: [Group]-validationLaws =- [ validationSemigroupProps- , validationMonoidProps- , validationApplicativeProps- , validationAlternativeProps- ]--------------------------------------------------------------------------------- Generators-------------------------------------------------------------------------------genFunction :: Gen (Int -> Int)-genFunction = Gen.element [(+), (*), const] <*> genSmallInt--genSmallInt :: Gen Int-genSmallInt = Gen.int (Range.linear (-10) 10)--genSmallText :: Gen Text-genSmallText = Gen.text (Range.linear 3 10) Gen.unicode--asValidation :: Gen a -> Gen (Validation [Text] a)-asValidation gen = Gen.choice- [ Success <$> gen- , Failure <$> Gen.list (Range.linear 1 5) genSmallText- ]--------------------------------------------------------------------------------- Property helpers-------------------------------------------------------------------------------checkAssociativityFor- :: (Show a, Eq a) => Gen a -> (a -> a -> a) -> Property-checkAssociativityFor gen op = property $ do- a <- forAll gen- b <- forAll gen- c <- forAll gen- a `op` (b `op` c) === (a `op` b) `op` c--------------------------------------------------------------------------------- Semigroup instance properties-------------------------------------------------------------------------------validationSemigroupProps :: Group-validationSemigroupProps =- Group "Semigroup instance for Validation property tests"- [ ("associativity:", prop_semigroupAssociativity)- ]--prop_semigroupAssociativity :: Property-prop_semigroupAssociativity =- checkAssociativityFor (asValidation genSmallText) (<>)--------------------------------------------------------------------------------- Monoid instance properties-------------------------------------------------------------------------------validationMonoidProps :: Group-validationMonoidProps =- Group "Monoid instance for Validation property tests"- [ ("right identity:", prop_monoidRightIdentity)- , ("left identity:", prop_monoidLeftIdentity)- ]--prop_monoidRightIdentity :: Property-prop_monoidRightIdentity = property $ do- x <- forAll $ asValidation genSmallText- x <> mempty === x--prop_monoidLeftIdentity :: Property-prop_monoidLeftIdentity = property $ do- x <- forAll $ asValidation genSmallText- mempty <> x === x--------------------------------------------------------------------------------- Applicative instance properties-------------------------------------------------------------------------------validationApplicativeProps :: Group-validationApplicativeProps =- Group "Applicative instance for Validation property tests"- [ ("identity:", prop_applicativeIdentity)- , ("composition:", prop_applicativeComposition)- , ("homomorphism:", prop_applicativeHomomorphism)- , ("interchange:", prop_applicativeInterchange)- , ("u *> v == (id <$ u) <*> v", prop_applicativeApplyRight)- , ("u <* v == liftA2 const u v", prop_applicativeApplyLeft)- ]--prop_applicativeIdentity :: Property-prop_applicativeIdentity = property $ do- vx <- forAll $ asValidation genSmallText- (pure id <*> vx) === vx--prop_applicativeComposition :: Property-prop_applicativeComposition = property $ do- vf <- forAllWith (const "f") $ asValidation genFunction- vg <- forAllWith (const "g") $ asValidation genFunction- vx <- forAll $ asValidation genSmallInt- (pure (.) <*> vf <*> vg <*> vx) === (vf <*> (vg <*> vx))--prop_applicativeHomomorphism :: Property-prop_applicativeHomomorphism = property $ do- f <- forAllWith (const "f") genFunction- x <- forAll genSmallInt- (pure f <*> pure x) === pure @(Validation [Text]) (f x)--prop_applicativeInterchange :: Property-prop_applicativeInterchange = property $ do- vf <- forAllWith (const "f") $ asValidation genFunction- x <- forAll genSmallInt- (vf <*> pure x) === (pure ($ x) <*> vf)--prop_applicativeApplyRight :: Property-prop_applicativeApplyRight = property $ do- vy <- forAll $ asValidation genSmallInt- vx <- forAll $ asValidation genSmallInt- (vy *> vx) === ((id <$ vy) <*> vx)--prop_applicativeApplyLeft :: Property-prop_applicativeApplyLeft = property $ do- vy <- forAll $ asValidation genSmallInt- vx <- forAll $ asValidation genSmallInt- (vy <* vx) === liftA2 const vy vx--------------------------------------------------------------------------------- Alternative instance properties-------------------------------------------------------------------------------validationAlternativeProps :: Group-validationAlternativeProps =- Group "Alternative instance for Validation property tests"- [ ("associativity:", prop_alternativeAssociativity)- , ("right identity:", prop_alternativeRightIdentity)- , ("left identity:", prop_alternativeLeftIdentity)- ]--prop_alternativeAssociativity :: Property-prop_alternativeAssociativity =- checkAssociativityFor (asValidation genSmallText) (<|>)--prop_alternativeRightIdentity :: Property-prop_alternativeRightIdentity = property $ do- x <- forAll $ asValidation genSmallText- (x <|> empty) === x--prop_alternativeLeftIdentity :: Property-prop_alternativeLeftIdentity = property $ do- x <- forAll $ asValidation genSmallText- (empty <|> x) === x
test/Test/Relude/Property.hs view
@@ -8,7 +8,6 @@ import Hedgehog (Group (..), Property, assert, forAll, property, (===)) import Test.Relude.Container.One (oneProps)-import Test.Relude.Extra.Validation.Property (validationLaws) import Test.Relude.Gen (genBoolList, genIntList, genUtf8ByteString, genUtf8String, genUtf8Text) @@ -18,7 +17,7 @@ , listProps , logicProps , oneProps- ] <> validationLaws+ ] ---------------------------------------------------------------------------- -- utf8 conversion