ZipFold (empty) → 0.0
raw patch · 6 files changed
+183/−0 lines, 6 filesdep +TypeComposedep +basesetup-changed
Dependencies added: TypeCompose, base
Files
- Makefile +1/−0
- README +17/−0
- Setup.lhs +3/−0
- ZipFold.cabal +35/−0
- src/Data/Zip/FoldL.hs +114/−0
- wikipage.tw +13/−0
+ Makefile view
@@ -0,0 +1,1 @@+include ../conal-cabal-make.inc
+ README view
@@ -0,0 +1,17 @@+ZipFold is a small package zipping folds, as described in a collection of+blog posts, <http://conal.net/blog/tag/zip>, inspired by the post "Beautiful Folds" by Max Rabkin+<http://squing.blogspot.com/2008/11/beautiful-folding.html>.++Please share any comments & suggestions on the discussion (talk) page at+[1].++You can configure, build, and install all in the usual way with Cabal+commands.++ runhaskell Setup.lhs configure+ runhaskell Setup.lhs build+ runhaskell Setup.lhs install++References:++[1] http://haskell.org/haskellwiki/ZipFold
+ Setup.lhs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain
+ ZipFold.cabal view
@@ -0,0 +1,35 @@+Name: ZipFold+Version: 0.0+Cabal-Version: >= 1.2+Synopsis: Zipping folds+Category: Data+Description:+ ZipFold is a small package zipping folds, as described in a collection of+ blog posts, <http://conal.net/blog/tag/zip>, inspired by a post by Max Rabkin+ <http://squing.blogspot.com/2008/11/beautiful-folding.html>.+ .+ Project wiki page: <http://haskell.org/haskellwiki/ZipFold>+ .+ The module documentation pages have links to colorized source code and+ to wiki pages where you can read and contribute user comments. Enjoy!+ .+ © 2008 by Conal Elliott; BSD3 license.+Author: Conal Elliott +Maintainer: conal@conal.net+Homepage: http://haskell.org/haskellwiki/ZipFold+Package-Url: http://code.haskell.org/ZipFold+Copyright: (c) 2008 by Conal Elliott+License: BSD3+Stability: experimental+build-type: Simple++Library+ hs-Source-Dirs: src+ Extensions:+ Build-Depends: base, TypeCompose+ Exposed-Modules: + Data.Zip.FoldL+ + ghc-options: -Wall++-- ghc-prof-options: -prof -auto-all
+ src/Data/Zip/FoldL.hs view
@@ -0,0 +1,114 @@+{-# LANGUAGE ExistentialQuantification, FlexibleContexts #-}+{-# OPTIONS_GHC -Wall #-}+----------------------------------------------------------------------+-- |+-- Module : Data.Zip.FoldL+-- Copyright : (c) Conal Elliott 2008+-- License : BSD3+-- +-- Maintainer : conal@conal.net+-- Stability : experimental+-- +-- Zipping of non-strict left folds.+-- +-- Based on "Beautiful Folds" by Max Rabkin+-- <http://squing.blogspot.com/2008/11/beautiful-folding.html>+--+-- See also <http://conal.net/blog/posts/more-beautiful-fold-zipping>+----------------------------------------------------------------------++module Data.Zip.FoldL+ ( Fold(..), cfoldl, cfoldl'+ , WithCont , FoldC , cfoldlc+ , WithCont', FoldC', cfoldlc'+ , Pair'(..), P+ ) where++import Data.Monoid+import Control.Applicative+import Data.List (foldl')++import Data.Pair++-- | Data representation of a left fold+data Fold b a = F (a -> b -> a) a++-- | Interpretation of a 'Fold' as non-strict+cfoldl :: Fold b a -> [b] -> a+cfoldl (F op e) = foldl op e++-- Non-strict left-fold zipping+zipF :: Fold b a -> Fold b a' -> Fold b (a,a')+F op e `zipF` F op' e' = F op'' (e,e')+ where+ (a,a') `op''` b = (a `op` b, a' `op'` b)++instance Pair (Fold b) where pair = zipF++-- | Interpretation of a 'Fold' as non-strict+cfoldl' :: Fold b a -> [b] -> a+cfoldl' (F op e) = foldl' op e++-- Strict left-fold zipping+zipF' :: Fold b a -> Fold b a' -> Fold b (P a a')+F op e `zipF'` F op' e' = F op'' (P e e')+ where+ P a a' `op''` b = P (a `op` b) (a' `op'` b)+++instance Pair' (Fold b) where pair' = zipF'+++-- | Add a continuation.+data WithCont h b c = forall a. WC (h b a) (a -> c)++instance Functor (WithCont h b) where+ fmap g (WC f k) = WC f (fmap g k)++instance Pair (h b) => Applicative (WithCont h b) where+ pure a = WC (error "unneeded pre-cont") (pure a)+ WC f k <*> WC f' k' =+ WC (f `pair` f') (\ (a,a') -> (k a) (k' a'))+++-- | Non-strict left fold with continuation.+type FoldC = WithCont Fold++-- | Interpretation of a 'FoldC'+cfoldlc :: FoldC b a -> [b] -> a+cfoldlc (WC f k) = fmap k (cfoldl f)++++-- | Like 'WithCont' but with pair-strict '(<*>)'+data WithCont' h b c = forall a. WC' (h b a) (a -> c)++instance Functor (WithCont' h b) where+ fmap g (WC' f k) = WC' f (fmap g k)++instance Pair' (h b) => Applicative (WithCont' h b) where+ pure a = WC' (error "unneeded pre-cont") (pure a)+ WC' f k <*> WC' f' k' =+ WC' (f `pair'` f') (\ (P a a') -> (k a) (k' a'))++-- | Strict left fold with continuation.+type FoldC' = WithCont' Fold++-- | Interpretation of a 'FoldC'+cfoldlc' :: FoldC' b a -> [b] -> a+cfoldlc' (WC' f k) = fmap k (cfoldl' f)+++----++-- | Strict pairs+data P c c' = P !c !c'++-- | Strict generalized pair+class Pair' f where+ pair' :: f a -> f b -> f (P a b)++instance Pair' [] where pair' = liftA2 P+instance Monoid u => Pair' ((,) u) where pair' = liftA2 P+instance Pair' ((->) u) where pair' = liftA2 P+instance Pair' IO where pair' = liftA2 P
+ wikipage.tw view
@@ -0,0 +1,13 @@+[[Category:Packages]]++== Abstract ==++'''ZipFold''' is a small package zipping folds, as described in a [http://conal.net/blog/tag/zip collection of blog posts' and inspired by the post [http://squing.blogspot.com/2008/11/beautiful-folding.html "Beautiful Folds"] by Max Rabkin.++Besides this wiki page, here are more ways to find out about ZipFold:+* Visit the [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/ZipFold Hackage page] for library documentation and to download & install.+* Or install with <tt>cabal install ZipFold</tt>.+* Get the code repository: <tt>darcs get http://code.haskell.org/ZipFold</tt>.+<!-- * See the [[ZipFold/Versions| version history]]. -->++Please leave comments at the [[Talk:ZipFold|Talk page]].