subG (empty) → 0.1.0.0
raw patch · 5 files changed
+122/−0 lines, 5 filesdep +basesetup-changed
Dependencies added: base
Files
- CHANGELOG.md +5/−0
- Data/SubG.hs +70/−0
- LICENSE +20/−0
- Setup.hs +2/−0
- subG.cabal +25/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for subG++## 0.1.0.0 -- 2020-10-16++* First version. Released on an unsuspecting world.
+ Data/SubG.hs view
@@ -0,0 +1,70 @@+-- |+-- Module : Data.SubG+-- Copyright : (c) OleksandrZhabenko 2020+-- License : MIT+-- Stability : Experimental+-- Maintainer : olexandr543@yahoo.com+--+-- ++{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}++module Data.SubG (+ subG+ , dropWhile+ , takeWhile+ , span+ , preAppend+) where++import Prelude hiding (dropWhile, span, takeWhile)+import qualified Data.Foldable as F+import Data.Monoid++infixr 1 %@, %^ ++class (Foldable t, Eq a, Eq (t a)) => InsertLeft t a where+ (%@) :: a -> t a -> t a -- infixr 1+ (%^) :: t a -> t (t a) -> t (t a)++instance (Eq a) => InsertLeft [] a where+ (%@) = (:)+ (%^) = (:)++-- | Inspired by: 'https://hackage.haskell.org/package/base-4.14.0.0/docs/src/Data.OldList.html#words'+-- and Graham Hutton. A tutorial on the universality and expressiveness of fold. J. Functional Programming 9 (4): 355–372, July 1999.+-- that is available at the URL: 'https://www.cs.nott.ac.uk/~pszgmh/fold.pdf'.+subG :: (InsertLeft t a, Monoid (t a), Monoid (t (t a))) => t a -> t a -> t (t a)+subG whspss xs = if F.null ts then mempty else w %^ subG whspss s''+ where ts = dropWhile (`F.elem` whspss) xs+ (w, s'') = span (`F.notElem` whspss) ts++dropWhile' :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> (t a, t a)+dropWhile' p = F.foldr f v+ where f x (ys, xs) = (if p x then ys else x %@ xs, x %@ xs)+ v = (mempty,mempty)++dropWhile :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> t a +dropWhile p = fst . dropWhile' p++span :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> (t a, t a)+span p = fst . span' p++span' :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> ((t a, t a), t a)+span' p = F.foldr f v+ where f x ((ys, zs), xs) = (if p x then (x %@ ys, zs) else (mempty,x %@ xs), x %@ xs)+ v = ((mempty, mempty), mempty)++takeWhile :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> t a+takeWhile p = fst . takeWhile' p++takeWhile' :: (InsertLeft t a, Monoid (t a)) => (a -> Bool) -> t a -> (t a, t a)+takeWhile' p = F.foldr f v+ where f x (ys,xs) = (if p x then x %@ ys else mempty, x %@ xs)+ v = (mempty,mempty)++-- | Prepends and appends the given two first arguments to the third one.+preAppend :: (InsertLeft t a, Monoid (t (t a))) => t a -> t (t a) -> t (t a) -> t (t a)+preAppend ts uss tss = mconcat [ts %^ tss, uss]+{-# INLINE preAppend #-}+
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2020 OleksandrZhabenko++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ subG.cabal view
@@ -0,0 +1,25 @@+-- Initial subG.cabal generated by cabal init. For further documentation,+-- see http://haskell.org/cabal/users-guide/++name: subG+version: 0.1.0.0+synopsis: Some extension to the Foldable and Monoid classes.+description: Some extension to the Foldable and Monoid classes. Introduces a new class InsertLeft of the types that can be inserted with values from the left.+homepage: https://hackage.haskell.org/package/subG+license: MIT+license-file: LICENSE+author: OleksandrZhabenko+maintainer: olexandr543@yahoo.com+copyright: Oleksandr Zhabenko+category: Data, Development+build-type: Simple+extra-source-files: CHANGELOG.md+cabal-version: >=1.10++library+ exposed-modules: Data.SubG+ -- other-modules:+ other-extensions: MultiParamTypeClasses, FlexibleInstances+ build-depends: base >=4.7 && <4.15+ -- hs-source-dirs:+ default-language: Haskell2010