Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 025c6121ba8a649ebb746a887134684094b92868 @finnsson committed Jun 9, 2010
Showing with 284 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +14 −0 COPYING.txt
  3. +15 −0 README.markdown
  4. +6 −0 Setup.lhs
  5. +46 −0 not-in-base.cabal
  6. +33 −0 src/NIB/List.hs
  7. +90 −0 src/NIB/Pointfree.hs
  8. +77 −0 src/NIB/String.hs
@@ -0,0 +1,3 @@
+.DS_Store
+dist
+*.swp
@@ -0,0 +1,14 @@
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ Version 2, December 2004
+
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
+
+ Everyone is permitted to copy and distribute verbatim or modified
+ copies of this license document, and changing it is allowed as long
+ as the name is changed.
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
+
@@ -0,0 +1,15 @@
+# not-in-base
+
+Contains useful utility-functions for base.
+
+## NIB.String
+
+Filter, trim, etc strings.
+
+## NIB.Pointfree
+
+Improve pointfree programming.
+
+## NIB.List
+
+Useful functions on lists.
@@ -0,0 +1,6 @@
+#!/usr/bin/runhaskell
+> module Main where
+> import Distribution.Simple
+> main :: IO ()
+> main = defaultMain
+
@@ -0,0 +1,46 @@
+name: not-in-base
+version: 0.1.1
+cabal-version: -any
+build-type: Simple
+license: OtherLicense
+license-file: "COPYING.txt"
+copyright:
+maintainer: Oscar Finnsson
+build-depends: base >= 4, haskell98
+stability:
+homepage: http://github.com/finnsson/not-in-base
+package-url:
+bug-reports:
+synopsis: Useful utility functions that only depend on base.
+description: Useful utility functions that only depend on base.
+category: Utility
+author: Oscar Finnsson
+tested-with: GHC -any
+data-files:
+data-dir: ""
+extra-source-files:
+extra-tmp-files:
+exposed-modules: NIB.String, NIB.Pointfree, NIB.List
+exposed: True
+buildable: True
+build-tools:
+cpp-options:
+cc-options:
+ld-options:
+pkgconfig-depends:
+frameworks:
+c-sources:
+extensions:
+extra-libraries:
+extra-lib-dirs:
+includes:
+install-includes:
+include-dirs:
+hs-source-dirs: src
+other-modules:
+ghc-prof-options:
+ghc-shared-options:
+ghc-options:
+hugs-options:
+nhc98-options:
+jhc-options:
@@ -0,0 +1,33 @@
+{-# LANGUAGE UndecidableInstances, OverlappingInstances, FlexibleInstances, TypeSynonymInstances
+ #-}
+
+module NIB.List where
+
+import Data.Char
+import Data.Maybe
+import Data.List
+import Control.Monad
+
+
+-- | Splits a list @x@ of @a@ into a list of lists of @a@ at every @c@.
+--
+-- > "splitBy "foo,bar" "',' == ["foo","bar"] '
+splitBy :: (Eq a) => a -> [a] -> [[a]]
+splitBy _ [] = [[]]
+splitBy c x = if fst p == [] then [snd p] else fst p : splitBy c ( snd p )
+ where p = break (== c) x
+
+-- | Trims every element satisfying @c@ from the beginning or end of the list.
+--
+-- > trim (==' ') " foo " == "foo"
+trim :: (a -> Bool) -> [a] -> [a]
+trim c = reverse . dropWhile c . reverse . dropWhile c
+
+-- | Convert first element in list
+--
+-- > convertFirst (toUpper) "fO0" == "FO0"
+convertFirst :: (a -> a) -> [a] -> [a]
+convertFirst _ [] = []
+convertFirst f (x:xs) = f x:xs
+
+
@@ -0,0 +1,90 @@
+{-# LANGUAGE UndecidableInstances, OverlappingInstances, FlexibleInstances, TypeSynonymInstances
+ #-}
+
+module NIB.Pointfree where
+
+import Data.Char
+import Data.Maybe
+import Data.List
+import Control.Monad
+
+-- \ Tuple functions
+
+fst3 :: (a,b,c) -> a
+fst3 (a,b,c) = a
+
+snd3 :: (a,b,c) -> b
+snd3 (a,b,c) = b
+
+trd3 :: (a,b,c) -> c
+trd3 (a,b,c) = c
+trd (a,b,c) = c
+
+
+-- | Lambdifies a function. See '(||*)' and '(&&*)' for uses of 'lambdify'.
+-- | Used in order to make operators capable of operating on functions that later on
+-- | are supplied some value that all functions operate on.
+--
+-- > (+*) = lambdify (+)
+-- > fourTwo = (*4) +* (*2)
+-- > 42 == fourTwo 7
+lambdify :: (x -> y -> z) -> (t -> x) -> (t -> y) -> t -> z
+lambdify f a b x = f (a x) (b x)
+
+-- | Lambdifies '(||)'.
+--
+-- > isBlankOrCommaChecker = (==' ') ||* (==',')
+-- > isBlankOrComma = isBlankOrCommaChecker 'j'
+(||*) :: (a -> Bool) -> (a -> Bool) -> a -> Bool
+(||*) = lambdify (||)
+
+-- | Lambdifies '(&&)'.
+--
+-- > isInRangeChecker = (>9) &&* (<30)
+-- > isInRange = isInRangeChecker 17
+(&&*) :: (a -> Bool) -> (a -> Bool) -> a -> Bool
+(&&*) = lambdify (&&)
+
+-- | 2-point-free operator. Similar to '.', but where
+-- | the second function takes two (2) arguments instead of one (1).
+--
+-- > multAndSquare (^2) .^.. (*)
+-- > 36 == multAndSqare 2 3
+(^..) :: (c -> d) -> (a -> b -> c) -> a -> b-> d
+(f ^.. g) a = f . g a
+
+-- | 3-point-free operator. See '(^..)'.
+(^...) :: (d -> e) -> (a -> b -> c -> d) -> a -> b -> c -> e
+(f ^...g ) a b = f . g a b
+
+-- | Split a 2-tuple 'x' into a 2-stack and pass it to 'f'.
+-- | The same as uncurry.
+(..%) :: (a -> b -> c) -> (a,b) -> c
+(..%) = uncurry
+
+(..%..) :: (c->d->e) -> (a->b->(c,d)) -> a -> b -> e
+(f ..%.. g) a b = f ..% g a b
+
+-- | Split a 3-tuple 'x' into a 3-stack and pass it to 'f'.
+(...%) :: (a -> b -> c -> d) -> (a,b,c) -> d
+(...%) f x = f (fst3 x) (snd3 x) (trd3 x)
+
+
+-- | Pipes a monadic return through a non-monadic transformation function.
+-- | liftM with arguments flipped.
+--
+-- > readIO >>* toUpper
+(>>*) :: Monad m => m a -> (a -> b) -> m b
+(>>*) v f = liftM f v -- v >>= (return . f)
+
+-- > f = ((+) 2, (*) 3)
+-- > x = 7
+-- > r = f ..@ x
+--
+-- | gives `(9, 21)`, i.e. `(2 + 9, 3 * 7)`.
+(..@) :: (a -> b, a -> c) -> a -> (b,c)
+f ..@ x = (fst f x, snd f x)
+
+-- | Same as `..@`, but with a 3-tuple.
+(...@) :: (a -> b, a -> c, a -> d) -> a -> (b,c,d)
+f ...@ x = (fst3 f x, snd3 f x, trd3 f x)
@@ -0,0 +1,77 @@
+-- {-# OPTIONS_GHC -fglasgow-exts -,
+--
+-- #-}
+{-# LANGUAGE UndecidableInstances, OverlappingInstances, FlexibleInstances, TypeSynonymInstances
+ #-}
+{-|
+ Contains useful generic functions not found elsewhere.
+
+
+-}
+module NIB.String where
+
+import Data.Char
+
+import NIB.List
+import NIB.Pointfree
+
+
+-- \ String functions
+
+-- \ ToString type class as found on stackoverflow (Porges answer at
+-- http://stackoverflow.com/questions/968198/haskell-show-screwed-up)
+-- Expanded with instance ToString Char.
+class ToString a where
+ toString :: a -> String
+
+instance ToString String where
+ toString = id
+
+instance ToString Char where
+ toString c = [c]
+
+instance Show a => ToString a where
+ toString = show
+
+
+
+-- | Remove all line breaks in a string
+--
+-- > "testtest" == removeBreak "test\n\rtest\r"
+removeBreak :: String -> String
+removeBreak = filter ((/= '\r') &&* (/= '\n'))
+
+-- | Convert first character in String to lower.
+--
+-- > lowerFirst "Foo" == "foo"
+-- > lowerFirst "BaR" == "baR"
+-- > lowerFirst "g0O" == "g0O".'
+lowerFirst :: String -> String
+lowerFirst = convertFirst toLower
+
+-- | Convert first character in String to upper.
+--
+-- > upperFirst "foo" == "Foo"
+-- > upperFirst "bAr" == "BAr"
+-- > upperFirst "G0O" == "G0O".'
+upperFirst :: String -> String
+upperFirst = convertFirst toUpper
+
+-- | Convert every space (' ') in a string to a blank ('_') instead.
+--
+-- > spaceToBlank " " == "_"
+-- > spaceToBlank " foo " == "_foo__"
+-- > spaceToBlank "b a r" == "b_a_r"
+spaceToBlank :: String -> String
+spaceToBlank "" = ""
+spaceToBlank (x:xs) = (if x == ' ' then '_' else x) : spaceToBlank xs
+
+
+
+-- | Trims whitespace from the beginning or end.
+--
+-- > trimWs " foo " == "foo"
+trimWs :: String -> String
+trimWs = trim (==' ')
+
+

0 comments on commit 025c612

Please sign in to comment.