How to iterate over record fields?

I have the following data type defined as a record

data Config = Config { field1 :: String , field2 :: String , field3 :: String } 

I want to iterate over all Config fields, apply some String -> String function, for example tail and get a new Config in return.

What is an idiomatic way to do this? Preferably, without heavy third-party libraries.

+5
source share
1 answer

Well, the best way to do this is likely to be

 {-# LANGUAGE DeriveFunctor #-} type Config = Config' String data Config' a = Config { field1 :: a , field2 :: a , field3 :: a } deriving (Functor) configHeads :: Config -> Config' Char configHeads = fmap head 
+13
source

Source: https://habr.com/ru/post/1233188/


All Articles