foldable1 (empty) → 0.1.0.0
raw patch · 5 files changed
+148/−0 lines, 5 filesdep +basedep +transformersdep +utilsetup-changed
Dependencies added: base, transformers, util
Files
- Data/Foldable1.hs +71/−0
- LICENSE +30/−0
- README.md +1/−0
- Setup.hs +2/−0
- foldable1.cabal +44/−0
+ Data/Foldable1.hs view
@@ -0,0 +1,71 @@+module Data.Foldable1 where++import Prelude hiding (head, tail, init, last, scanl1, scanr1)+import Control.Applicative.Backwards+import Data.Foldable (foldl', foldlM)+import Data.Functor.Compose+import Data.Functor.Identity+import Data.Functor.Product+import Data.Functor.Reverse+import Data.Functor.Sum+import Data.List.NonEmpty (NonEmpty (..), head, last, scanl1, scanr1, uncons)+import qualified Data.List.NonEmpty as NE+import Data.Semigroup (Semigroup (..), Dual (..), Max (..), Min (..))+import Util ((&))++class Foldable f => Foldable1 f where+ {-# MINIMAL foldMap1 | toNonEmpty #-}++ fold1 :: Semigroup a => f a -> a+ fold1 = foldMap1 id++ foldMap1 :: Semigroup b => (a -> b) -> f a -> b+ foldMap1 f = sconcat . fmap f . toNonEmpty++ foldr1, foldl1, foldr1', foldl1' :: (a -> a -> a) -> f a -> a+ foldr1 f = head . scanr1 f . toNonEmpty+ foldl1 f = last . scanl1 f . toNonEmpty+ foldl1' f = toNonEmpty & \ (a:|as) -> foldl' f a as+ foldr1' f = toNonEmpty & go+ where go = uncons & \ case (a, Nothing) -> a+ (a, Just as) -> a `f` go as++ toNonEmpty :: f a -> NonEmpty a+ toNonEmpty = foldMap1 pure++ maximum, minimum :: Ord a => f a -> a+ maximum = getMax . foldMap1 Max+ minimum = getMin . foldMap1 Min++intercalate :: (Foldable1 f, Semigroup a) => a -> f a -> a+intercalate a = sconcat . NE.intersperse a . toNonEmpty++foldrM1, foldlM1 :: (Foldable1 f, Monad m) => (a -> a -> m a) -> f a -> m a+foldrM1 f = go . toNonEmpty+ where go (a:|[]) = pure a+ go (a:|b:bs) = f a =<< go (b:|bs)+foldlM1 f = toNonEmpty & \ (a:|as) -> foldlM f a as++instance Foldable1 Identity where+ foldMap1 f = f . runIdentity++instance Foldable1 NonEmpty where+ toNonEmpty = id++instance Foldable1 ((,) a) where+ foldMap1 f = f . snd++deriving instance (Foldable1 f) => Foldable1 (Backwards f)++instance (Foldable1 f) => Foldable1 (Reverse f) where+ foldMap1 f (Reverse as) = getDual $ foldMap1 (Dual . f) as++instance (Foldable1 f, Foldable1 g) => Foldable1 (Compose f g) where+ foldMap1 f = (foldMap1 . foldMap1) f . getCompose++instance (Foldable1 f, Foldable1 g) => Foldable1 (Product f g) where+ foldMap1 f (Pair as bs) = foldMap1 f as <> foldMap1 f bs++instance (Foldable1 f, Foldable1 g) => Foldable1 (Sum f g) where+ foldMap1 f (InL as) = foldMap1 f as+ foldMap1 f (InR bs) = foldMap1 f bs
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright M Farkas-Dyck © 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of M Farkas-Dyck nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,1 @@+# foldable1
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ foldable1.cabal view
@@ -0,0 +1,44 @@+name: foldable1+version: 0.1.0.0+synopsis: Foldable types with at least 1 element+-- description:+license: BSD3+license-file: LICENSE+author: M Farkas-Dyck+maintainer: strake888@gmail.com+copyright: 2018 M Farkas-Dyck+-- category: +build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ exposed-modules: Data.Foldable1+ build-depends: base >= 4.7 && < 5+ , transformers >= 0.3 && < 0.6+ , util+ default-language: Haskell2010+ default-extensions: UnicodeSyntax+ , LambdaCase+ , EmptyCase+ , InstanceSigs+ , PartialTypeSignatures+ , PolyKinds+ , ConstraintKinds+ , FlexibleContexts+ , FlexibleInstances+ , MonadComprehensions+ , StandaloneDeriving+ , GeneralizedNewtypeDeriving+ , DeriveFunctor, DeriveFoldable, DeriveTraversable+ ghc-options: -Wall -Wcompat -Wredundant-constraints -Wno-name-shadowing+ -Wincomplete-record-updates -Wincomplete-uni-patterns+ -Werror=incomplete-patterns+ -Werror=incomplete-uni-patterns+ -Werror=incomplete-record-updates+ -Werror=missing-fields+ -Werror=missing-methods++source-repository head+ type: git+ location: https://github.com/strake/foldable1.hs