safe-length (empty) → 0.1.0.0
raw patch · 11 files changed
+156/−0 lines, 11 filesdep +QuickCheckdep +basedep +hspecsetup-changed
Dependencies added: QuickCheck, base, hspec, hspec-core, safe-length, should-not-typecheck
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- example/BrokenTuple.hs +4/−0
- example/GoodTuple.hs +8/−0
- example/List.hs +7/−0
- example/NestedList.hs +8/−0
- example/OldList.hs +8/−0
- example/Tuple.hs +7/−0
- safe-length.cabal +36/−0
- src/Safe/Length.hs +9/−0
- tests/Main.hs +37/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Jeremy Shaw++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 Jeremy Shaw 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,2 @@+import Distribution.Simple+main = defaultMain
+ example/BrokenTuple.hs view
@@ -0,0 +1,4 @@+module Main where++main :: IO ()+main = print $ length ('a', 'b')
+ example/GoodTuple.hs view
@@ -0,0 +1,8 @@+module Main where++import Safe.Length (safeLength)+import Data.Proxy (Proxy(..))++main :: IO ()+main = print $ safeLength (Proxy :: Proxy (Char, Char)) ('a', 'b')+
+ example/List.hs view
@@ -0,0 +1,7 @@+module Main where++import Data.Proxy (Proxy(..))+import Safe.Length (safeLength)++main :: IO ()+main = print $ safeLength (Proxy :: Proxy [Char]) ['a', 'b']
+ example/NestedList.hs view
@@ -0,0 +1,8 @@+module Main where++import Data.Proxy (Proxy(..))+import Safe.Length (safeLength)++main :: IO ()+main = print $ safeLength (Proxy :: Proxy [[Char]]) (head [['a', 'b', 'c']])+
+ example/OldList.hs view
@@ -0,0 +1,8 @@+module Main where++import GHC.OldList as OldList++main :: IO ()+main = print $ OldList.length (head [['a','b','c']])++
+ example/Tuple.hs view
@@ -0,0 +1,7 @@+module Main where++import Data.Proxy (Proxy(..))+import Safe.Length (safeLength)++main :: IO ()+main = print $ safeLength (Proxy :: Proxy [Char]) ('a', 'b')
+ safe-length.cabal view
@@ -0,0 +1,36 @@+name: safe-length+version: 0.1.0.0+synopsis: Tired of accidentally calling length on tuples? Relief at last!+homepage: http://www.github.com/stepcut/safe-length+license: BSD3+license-file: LICENSE+author: Jeremy Shaw+maintainer: jeremy@n-heptane.com+category: Data+build-type: Simple+cabal-version: >=1.10+data-files:+ example/BrokenTuple.hs+ example/GoodTuple.hs+ example/List.hs+ example/NestedList.hs+ example/OldList.hs+ example/Tuple.hs++library+ exposed-modules: Safe.Length+ build-depends: base >=4.8 && <4.9+ hs-source-dirs: src+ default-language: Haskell2010++test-suite tests+ type: exitcode-stdio-1.0+ main-is: Main.hs+ hs-source-dirs: tests+ build-depends: base,+ hspec,+ hspec-core,+ QuickCheck,+ safe-length,+ should-not-typecheck+ default-language: Haskell2010
+ src/Safe/Length.hs view
@@ -0,0 +1,9 @@+module Safe.Length where++import Data.Proxy (Proxy(..))++safeLength :: (Foldable f) => Proxy (f a) -> f a -> Int+safeLength _ f = length f++list :: Proxy [a]+list = Proxy
+ tests/Main.hs view
@@ -0,0 +1,37 @@+{-# OPTIONS_GHC -fdefer-type-errors #-}+------------------------------------------------------------------------------+-- |+-- Module : Main+-- Stability : experimental+-- Portability : POSIX+--+------------------------------------------------------------------------------+module Main where++import Data.Proxy (Proxy(..))+import Safe.Length (safeLength)+import Test.Hspec+import Test.QuickCheck (property)+import Test.ShouldNotTypecheck (shouldNotTypecheck)++------------------------------------------------------------------------------+-- | Testing main+main :: IO ()+main = hspec spec++------------------------------------------------------------------------------+-- | Spec+spec :: Spec+spec = do+ describe "safeLength tests" $+ tests++tests = do+ it "length of a tuple is 1" $+ property $ safeLength (Proxy :: Proxy (Char, Char)) ('a', 'b') == 1+ it "Can't accidentally take the length of a tuple when we don't mean to" $+ shouldNotTypecheck (safeLength (Proxy :: Proxy [Char]) ('a', 'b'))+ it "Can't accidentally take the length of the wrong level of a nested list" $+ shouldNotTypecheck (safeLength (Proxy :: Proxy [[Char]]) (head [['a', 'b', 'c']] :: [Char]))+ it "Can take the length of a list." $+ property $ safeLength (Proxy :: Proxy [Char]) ['a', 'b'] == 2