functional programming - Does Haskell provide a way to map a function to a data member? -


i haskell rookie , find myself having decompose data pattern matching apply function 1 of member , reassemble it.

say have:

data car = car { gas :: int, licenseplate :: string } 

and want halve gas when drives, , refuel it, i'm doing:

mapgas:: (int -> int) -> car -> car mapgas f (car agas alicenseplate) = car (f agas) alicenseplate  drive:: car -> car drive = mapgas (flip div 2)  refuel:: int -> car -> car refuel = mapgas . (+) 

is there way without having define auxiliary function mapgas? since can become rather bothersome having write map function every member of data when it's made of many fields. know possible assign value 1 of members accessors:

runoutoffuel:: car -> car runoutoffuel acar = acar { gas = 0 } 

is possible map function accessors too? if so, how?

using core libraries? no. used lens package, yes. here looks in case:

{-# language templatehaskell #-}  import control.lens.th import control.lens  data car = car { _gas :: int, _licenseplate :: string }  makelenses ''car 

now, can get/set/modify fields nested in data structures.

runoutoffuel:: car -> car runoutoffuel = gas .~ 0  drive:: car -> car drive = gas %~ (`div` 2)  refuel:: int -> car -> car refuel c = gas +~ c 

the magic here makelenses ''car generates gas , licenseplate functions similar (but more powerful) mapgas (in fact, mapgas = (gas %~)). getting started lens pretty daunting, recommend reading examples section.


Comments