Created
April 23, 2020 16:09
-
-
Save prednaz/e07b10d4692712596c739855c2f22ece to your computer and use it in GitHub Desktop.
This file contains 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 characters
{-# language FlexibleInstances #-} | |
{-# language UndecidableInstances #-} | |
{-# language TemplateHaskell #-} | |
{-# language DataKinds #-} | |
{-# language OverloadedLabels #-} | |
{-# language MultiParamTypeClasses #-} | |
{-# language GADTs #-} | |
module Main where | |
import Control.Monad (join) | |
import Optics | |
data Box a = | |
Box { | |
list :: [a], | |
index :: Maybe Int | |
} | |
makeFieldLabelsWith noPrefixFieldLabels ''Box | |
content :: AffineTraversal' (Box a) a | |
content = | |
affineTraversalOfFunction contentFunction | |
where | |
contentFunction :: Box a -> AffineTraversal' (Box a) a | |
contentFunction s = | |
#list | |
% | |
maybe trivialAffineTraversal ix (s ^. #index) | |
trivialAffineTraversal :: AffineTraversal s s a b | |
trivialAffineTraversal = atraversal Left const | |
affineTraversalOfFunction :: (s -> AffineTraversal s t a b) -> AffineTraversal s t a b | |
affineTraversalOfFunction affineTraversal = | |
atraversal | |
(collapseDoubleParameter $ matching . affineTraversal) | |
(collapseDoubleParameter $ (flip . set) . affineTraversal) | |
where | |
collapseDoubleParameter :: (a -> a -> b) -> (a -> b) | |
collapseDoubleParameter = join | |
main :: IO () | |
main = pure () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment