pointless-fun (empty) → 1.1.0
raw patch · 4 files changed
+237/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- LICENSE +33/−0
- Setup.hs +7/−0
- pointless-fun.cabal +50/−0
- src/Data/Function/Pointless.hs +147/−0
+ LICENSE view
@@ -0,0 +1,33 @@+Copyright (c) 2009, 2010, 2011, 2012, wren ng thornton.+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 the copyright holders 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.+
+ Setup.hs view
@@ -0,0 +1,7 @@+#!/usr/bin/env runhaskell++module Main (main) where+import Distribution.Simple++main :: IO ()+main = defaultMain
+ pointless-fun.cabal view
@@ -0,0 +1,50 @@+----------------------------------------------------------------+-- wren ng thornton <wren@community.haskell.org> ~ 2012.01.06+----------------------------------------------------------------++Name: pointless-fun+Version: 1.1.0+Stability: provisional+Homepage: http://code.haskell.org/~wren/+Author: wren ng thornton+Maintainer: wren@community.haskell.org+Copyright: Copyright (c) 2009--2012 wren ng thornton+License: BSD3+License-File: LICENSE++Category: Combinators, Composition, Utils+Synopsis: Some common point-free combinators.+Description: Some common point-free combinators.++-- By and large Cabal >=1.2 is fine; but >= 1.6 gives tested-with:+-- and source-repository:.+Cabal-Version: >= 1.6+Build-Type: Simple+Tested-With: GHC == 6.12.1, GHC == 6.12.3++Source-Repository head+ Type: darcs+ Location: http://community.haskell.org/~wren/pointless-fun++----------------------------------------------------------------+Flag base4+ Default: True+ Description: base-4.0 emits "Prelude deprecated" messages in+ order to get people to be explicit about which+ version of base they use.++-- TODO: add flags for compiling against haskell98 or haskell2010+-- instead of base?++----------------------------------------------------------------+Library+ Hs-Source-Dirs: src+ Exposed-Modules: Data.Function.Pointless+ + if flag(base4)+ Build-Depends: base >= 4 && < 5+ else+ Build-Depends: base < 4++----------------------------------------------------------------+----------------------------------------------------------- fin.
+ src/Data/Function/Pointless.hs view
@@ -0,0 +1,147 @@+{-# OPTIONS_GHC -Wall -fwarn-tabs #-}+----------------------------------------------------------------+-- 2012.01.06+-- |+-- Module : Data.Function.Pointless+-- Copyright : Copyright (c) 2009--2012 wren ng thornton+-- License : BSD+-- Maintainer : wren@community.haskell.org+-- Stability : provisional+-- Portability : Haskell98+--+-- Pointless fun :)+----------------------------------------------------------------+module Data.Function.Pointless+ (+ -- * Multicomposition+ -- | Based on <http://matt.immute.net/content/pointless-fun>.+ -- These combinators allow you to easily modify the types of a+ -- many-argument function with syntax that looks like giving+ -- type signatures. For example,+ --+ -- > foo :: A -> B -> C+ -- >+ -- > albert :: A -> X+ -- > beth :: B -> Y+ -- > carol :: C -> Z+ -- > + -- > bar :: X -> Y -> Z+ -- > bar = foo $:: albert ~> beth ~> carol+ ($::), (~>), (!~>)+ + -- * Composition for arity 2+ , (.:), (.^)+ + -- * Strict composition+ , (.!)+ ) where+----------------------------------------------------------------+----------------------------------------------------------------+{- TODO: actually put this in.+-- Note: @pl had nothing to do with the invention of this combinator. I constructed it by hand after noticing a common pattern. -- Cale++swing :: (((a -> b) -> b) -> c -> d) -> c -> a -> d+swing = flip . (. flip id)+swing f = flip (f . runCont . return)+swing f c a = f ($ a) c++-- Some examples of use:++swing map :: forall a b. [a -> b] -> a -> [b]+swing any :: forall a. [a -> Bool] -> a -> Bool+swing foldr :: forall a b. b -> a -> [a -> b -> b] -> b+swing zipWith :: forall a b c. [a -> b -> c] -> a -> [b] -> [c]+swing find :: forall a. [a -> Bool] -> a -> Maybe (a -> Bool)+ -- applies each of the predicates to the given value, returning the first predicate which succeeds, if any+swing partition :: forall a. [a -> Bool] -> a -> ([a -> Bool], [a -> Bool])+-}+----------------------------------------------------------------+++-- | Lift a function for multicomposition. This is like the @::@+-- of a type signature.++($::) :: (a -> b) -> ((a -> b) -> (c -> d)) -> (c -> d)+($::) = flip ($)+{-# INLINE ($::) #-}+infixl 1 $::+++-- | Multicompose a function on the appropriate argument. This is+-- like the @->@ arrows in a type signature.++(~>) :: (a -> b) -> (c -> d) -> ((b -> c) -> (a -> d))+f ~> g = (. f) . (g .)+{-# INLINE (~>) #-}+infixr 2 ~>+++-- | Multicompose a function on the appropriate argument, calling+-- the left function eagerly. That is, the resulting function will+-- be strict in @a@ if the left argument is strict in @a@ (assuming+-- the final function of the multicomposition, the one applied to+-- the return value, is also strict).++(!~>) :: (a -> b) -> (c -> d) -> ((b -> c) -> (a -> d))+f !~> g = (.! f) . (g .)+{-# INLINE (!~>) #-}+infixr 2 !~>+++----------------------------------------------------------------+-- | Binary composition: pass two args to the right argument before+-- composing.+--+-- > (f .: g) x y = f (g x y)+--+-- or,+--+-- > f .: g = curry (f . uncurry g)+--+-- This is the same as the common idiom @(f .) . g@ but more easily+-- extended to multiple uses, due to the fixity declaration.++(.:) :: (c -> d) -> (a -> b -> c) -> (a -> b -> d)+(.:) = (.) . (.)+{-# INLINE (.:) #-}+infixl 8 .:+++-- | Secondary composition: compose the right argument on the second+-- arg of the left argument.+--+-- > (f .^ g) x y = f x (g y)++(.^) :: (a -> c -> d) -> (b -> c) -> (a -> b -> d)+(.^) = flip .: (.) . flip+{-# INLINE (.^) #-}+infix 9 .^+++-- | Function composition which calls the right-hand function+-- eagerly; i.e., making the left-hand function strict in its first+-- argument.+--+-- > (f .! g) x = f $! g x+--+-- This defines the composition for the sub-category of strict+-- Haskell functions. If the 'Functor' class were parameterized by+-- the domain and codomain categories (e.g., a regular @Functor f@+-- would be @CFunctor (->) (->) f@ instead) then this would allow+-- us to define functors @CFunctor (->) (!->) f@ where+-- @fmap f . fmap g = fmap (f .! g)@.++(.!) :: (b -> c) -> (a -> b) -> a -> c+(.!) = (.) . ($!)+{-# INLINE (.!) #-}+infixr 9 .!+-- For some reason this definition is significantly faster than the+-- pointful version in the documentation. Even after INLINE. Who knew?+--+-- cf vs @($!) .: (.)@ == @\f g x -> f . g $! x@ which stictifies+-- the first argument of RH-function. However, this doesn't behave+-- the way you may think it should; i.e., it isn't compositional+-- in the way you think it should be.++----------------------------------------------------------------+----------------------------------------------------------- fin.