Sha256: c2dff45124ba0ce3cfed0a28ebb2774ad7925f0ba7d4f7541aebd87c13b548bb

Contents?: true

Size: 1.5 KB

Versions: 69

Compression:

Stored size: 1.5 KB

Contents

{-# LANGUAGE TemplateHaskell #-}

module Person where

import           Control.Lens
import           Data.Time.Calendar

data Person = Person {
      _name    :: Name,
      _born    :: Born,
      _address :: Address
    }

data Name = Name {
      _foreNames :: String, -- Space separated
      _surName   :: String
    }

data Born = Born {
      _bornAt :: Address,
      _bornOn :: Day
    }

data Address = Address {
      _street      :: String,
      _houseNumber :: Int,
      _place       :: String, -- Village / city
      _country     :: String
    }

-- Valid values of Gregorian are those for which 'Data.Time.Calendar.fromGregorianValid'
-- returns Just.
data Gregorian = Gregorian {
      _year  :: Integer,
      _month :: Int,
      _day   :: Int
    }

makeLenses ''Person
makeLenses ''Name
makeLenses ''Born
makeLenses ''Address
makeLenses ''Gregorian

gregorianDay :: Iso' Gregorian Day
gregorianDay = iso toDay fromDay
  where
    toDay (Gregorian y m d) = fromGregorian y m d
    fromDay d' = let (y, m, d) = toGregorian d' in Gregorian y m d

bornStreet :: Born -> String
bornStreet = view (bornAt . street)

setCurrentStreet :: String -> Person -> Person
setCurrentStreet = set (address . street)

setBirthMonth :: Int -> Person -> Person
setBirthMonth = set (born . bornOn . from gregorianDay . month)

renameStreets :: (String -> String) -> Person -> Person
renameStreets f = over birthStreet f . over currentStreet f
  where
    birthStreet = born . bornAt . street
    currentStreet = address . street

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.0.6.13 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.12 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.11 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.10 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.9 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.8 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.7 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.6 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.5 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.4 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.3 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.2 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.1 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.6.0 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.5.18 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.5.17 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.5.16 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.5.15 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.5.14 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs
trackler-2.0.5.13 tracks/haskell/exercises/lens-person/examples/success-standard/src/Person.hs