traverse-with-class 1.0 release
Published on
I have released the 1.0
version of traverse-with-class. This library generalizes many
Foldable
and Traversable
functions to
heterogeneous containers such as records.
For instance, you can apply Show
to all fields and
collect the results:
{-# LANGUAGE TemplateHaskell, MultiParamTypeClasses, FlexibleInstances,
ConstraintKinds, UndecidableInstances, TypeApplications #-}
import Data.Generics.Traversable
import Data.Generics.Traversable.TH
data User a = User
name :: String
{ age :: Int
, misc :: a
,
}
'User
deriveGTraversable '
= gfoldMap @Show (\x -> [show x]) $ User "Alice" 22 True
allFields -- ["\"Alice\"","22","True"]
You also get a free zipper for your data types.
The main change in the version 1.0 is that the constraint with which the traversal is conducted is specified via a visible type application. Type applications weren’t available when I originally wrote this library in 2013, so in that version I used implicit parameters to pass around the annoying proxies.
Thanks to Hao Lian for his help with this transition.
Right after I published this blog post, I saw this tweet:
Days since last mailing list discussion of Foldable tuples:
— Michael Snoyman (@snoyberg) April 23, 2017
0
RESTART THE CLOCK!
Guess what, traverse-with-class provides the sensible
Foldable
-like instance for tuples:
{-# LANGUAGE FlexibleInstances, TypeApplications #-}
import Data.Generics.Traversable
-- U is a trivial constraint satisfied by all types
class U a
instance U a
= gfoldl' @U (\c _ -> c + 1) 0 (1::Int, True)
tupleLength -- returns 2