hydrogen-prelude

Hydrogen Prelude http://scravy.de/hydrogen-prelude/

Latest on Hackage:0.20

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.

MIT licensed by Julian Fleischer
Maintained by julian@scravy.de

hydrogen-prelude

Build Status

about

A Prelude that exports much of the standard library (more then Prelude), without conflicts. If for example you were to import Prelude and Data.List or Data.Foldable you will run into ambiguous imports (regarding foldr for example). This Prelude aims at exporting the most general functions (in this case foldr from Data.Foldable).

It also pulls in some default packages like cereal for serialization and containers for data types like Map and Set. Every datatype exported by this Prelude comes with instances for Serialize.

Longs story short, instead of:

import Prelude hiding (
  all, and, any, concat, concatMap, elem, foldl, foldl1, foldr, foldr1,
  mapM, mapM_, maximum, minimum, notElem, or, product, sequence, sequence_, sum
 )
import "base" Control.Monad hiding (
  forM, forM_, mapM, mapM_, msum, sequence, sequence_
 )
import Data.Foldable
import Data.Traversable
import Data.List hiding (
  all, and, any, concat, concatMap, elem, find, foldl, foldl', foldl1, foldr, foldr1,
  mapAccumL, mapAccumR, maximum, maximumBy, minimum, minimumBy, notElem, or, product, sum
 )

it suffices to:

import Hydrogen.Prelude

goodies

Beyond existing functions from well-known standard packages, this prelude defines a few utilities (mostly aimed at unifying functionality across different packages, like containers and array).


(!) :: Has a ⇒ a → HasKey a → HasValue a

(!) is provided for several data types which associate a key and a value.

[(1, 'a'), (3, 'v')] ! 1  →  'a'

Instances are defined for

  • Eq k ⇒ [(k, v)]
  • Ix i ⇒ Array i e with HasKey → i
  • Ord k ⇒ Map k v with HasValue → v
  • Ord k ⇒ MultiMap k v with HasValue → [v]

(?) :: Container a ⇒ a → Contained a → Bool

Check whether the element on the right is contained in the collection on the left.

[1, 2, 4] ? 3  →  False

Instances are defined for

  • Eq a ⇒ [a]
  • Ord a ⇒ Set a
  • Eq a ⇒ Seq a
  • Ord k ⇒ Map k v with Contained → k
  • Ord k ⇒ MultiMap k v with Contained → k

tmap

A little bit like fmap but defined differently on some datatypes (applies e.g. to both components of a tuple).

tmap succ (3, 4)  →  (4, 5)

Instances are defined for

  • (a, a)
  • (a, a, a)
  • (a, a, a, a)
  • [a]
  • Seq a
  • Map k v
  • MultiMap k v

fmap vs map

Hydrogen Prelude exports fmap as map - the way it ought to be.


__ :: a

A handy shortcut for undefined.


FSharp's |> (which is flip ($))

Use it to pipe things from one function to the other, left to right:

head xs |> fromEnum |> show

safeHead :: a → [a] → a

The head of the list, or the default given as first argument.

safeHead x xs = maybe x head . listToMaybe

ShowBox :: forall a. (Show a) ⇒ a → ShowBox

Wrap anything that is showable (can be used to build heterogeneous lists).


.|, .&, .^ :: (a → Bool) → (a → Bool) → (a → Bool)

Combines predicates.

filter (isDigit .| isLetter)

type List a = [a]

A longhand for the type of lists, if you prefer this more wordy version.


class Default a

A class that provides the def function for default values for types. Instances of MonadPlus automatically have an instance where def = mzero.

Default instances for most primitive types are also provided.


class Apply a

Provides the *$* operator which is your all-purpose application operator. It does uncurrying (if you want to apply a tupel result of a function to a function that is curryied, works with tupels of up to 5 components) and also works with Applicative, i.e. it also does fmap . uncurry:

data Operator = Operator Value OperatorType Value

parseInfixOperation :: Parser (Value, OperatorType, Value)

... Operator *$* parseInfixOperation ...

re-exports

Hydrogen.Prelude

The Hydrogen Prelude offers you the functions and datatypes from these modules, all with one import:

  • from base + module Prelude + module Control.Applicative + module Control.Arrow + module Control.Monad + module Data.Bits + module Data.Bool + module Data.Char + module Data.Complex + module Data.Complex + module Data.Dynamic + module Data.Either + module Data.Fixed + module Data.Function + module Data.Foldable + module Data.Int + module Data.Ix + module Data.List + module Data.Maybe + module Data.Ord + module Data.Ratio + module Data.String + module Data.Time + module Data.Traversable + module Data.Tuple + module Data.Typeable + module Data.Word + module Numeric + module Text.Printf

  • from array + module Data.Array

  • from cereal + module Data.Serialize

  • from containers + Data.Set, Data.Map, and Data.Seq

  • from hashable + module Data.Hashable

  • form hydrogen-multimap + Hydrogen.MultiMap

  • from hydrogen-version + module Hydrogen.Version

  • from regex-tdfa + module Text.Regex.TDFA

  • from time + module Data.Time

  • from transformers + module Data.Functor.Identity + module Data.Functor.Reverse

  • from uuid + Data.UUID + Data.UUID.fromString as uuidFromString + Data.UUID.V4.nextRandom as randomUUID

Hydrogen.Prelude.IO

  • from base + module Data.IORef + module Control.Concurret + module Control.Exception + module System.IO + module System.Timeout

  • from strict + strict IO functions hGetContents', getContents', readFile', interact'

Hydrogen.Prelude.System

  • from base + module System.CPUTime + module System.Environment + module System.Exit + module System.Info

  • from directory + module System.Directory

  • from filepath + module System.FilePath

  • from process + module System.Process

  • from random + module System.Random

Hydrogen.Prelude.Network

FAQ

How is some of the magic accomplished?

Mostly with XTypeFamilies and XStandaloneDeriving.

So this works only with GHC?

Yes, for now at least.

What is hydrogen

https://www.youtube.com/watch?v=rbBX6aEzEz8

Changes

v0.1

  • initial release

v0.1.0.1

  • Patch: Modules were not properly exported in Cabal file

v0.1.1

  • Added module Numeric, Data.Map (Map), and Data.Set (Set)

v0.2

  • Added for

v0.2.0.1

  • Added CHANGELOG.md to source package

v0.2.1

  • Added __ (short hand for undefined)

v0.3

  • Also exports + module Data.Ix, + module Data.Array, + Data.Bits, + Data.Ratio, + and Data.Complex
  • New dependency on hydrogen-version + Exports module Hydrogen.Version as replacement for module Data.Version

v0.4

Skipped, bumped to v0.5 to be aligned with other hydrogen packages

v0.5

  • Deriving instance Eq for ZonedTime

v0.6

  • New dependencies + cereal for serialization + strict for strict io in Hydrogen.Prelude.IO
  • Also exports + module Data.Dynamic + module Data.Typeable
  • Added ShowBox class (an existentially qualified container type)

v0.7

  • Newly deriving instances + Serialize for ZonedTime + Serialize for Version

v0.8

  • Added class TMap (tmap) and class Has (HasKey, HasValue, (!))
  • Introduced type List a as alias for [a]
  • Also exports + Data.Seq
  • Added the F-Sharp inspired |> (which is flip ($))
  • Exporting only Generic from GHC.Generics
  • Added Hydrogen.Prelude.Network

v0.9

  • Also exports + module Data.Traversable
  • Added class Container (Contained, (?))
  • fmap is exported as map

v0.10

  • Added safeHeadAndTail, safeHeadAndTail2, firstJust
  • Little documentation improvements
  • Added findFilesRecursively and findFilesRecursivelyWithContext in Hydrogen.Prelude.System

v0.11

  • Added nicify form Text.Nicify
  • Upgraded dependency hydrogen-version from >=1.2 to >=1.3

v0.12

  • Added Default

v0.13

  • Skipped

v0.14

  • Aligned with other hydrogen-* packages

v0.15

  • Added Apply
  • Added uncurry3, uncurry4, uncurry5

v0.16

  • Changed Apply to use -XMultiParamTypeClasses and -XTypeFamilies for better type inference
  • Added $$, $$$, $$$$, $$$$$ (like uncurryN)
  • Added <$$>, <$$$>, <$$$$>, <$$$$$> (like <$> and uncurryN)

v0.17

  • Removed instance MonadPlus m => Default (m a).

v0.18

  • Removed dependency on nicify

v0.19

  • Removed Apply and Applicator again
  • Lowered dependencies to work with GHC 7.6 and base 4.6 too

v0.20

  • Support for GHC 7.4, 7.6, and 7.8
  • Instances for Serializable but also for Binary
comments powered byDisqus