-
-
Save pwm/fc01806bacccaaa997b28e28f27554a8 to your computer and use it in GitHub Desktop.
Revisions
-
pwm created this gist
Dec 8, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE OverloadedLabels #-} module A where import Control.Lens import Data.Generics.Labels () import GHC.Generics (Generic) import Prelude data Person = MkPerson { name :: String, age :: Int } deriving stock (Show, Generic) getName :: Person -> String getName p = p ^. #name getAge :: Person -> Int getAge p = p ^. #age dAdams :: Person dAdams = MkPerson "Douglas Adams" 42 {- λ> getName dAdams "Douglas Adams" λ> getAge dAdams 42 -} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE OverloadedLabels #-} module B where import Control.Lens import Data.Generics.Labels () import Data.Generics.Product import GHC.Generics (Generic) import Prelude data Person = MkPerson { name :: String, age :: Int } deriving stock (Show, Generic) data Group = MkGroup { name :: String, members :: [Person] } deriving stock (Show, Generic) _name :: (HasField "name" s t a b) => Lens s t a b _name = #name dAdams :: Person dAdams = MkPerson "Douglas Adams" 42 greatWriters :: Group greatWriters = MkGroup { name = "Great writers", members = [dAdams] } {- λ> view _name dAdams "Douglas Adams" λ> view _name greatWriters "Great writers" -}