{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE TypeFamilies          #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.RAList.Lens (
    -- * Indexing
    ix,
    ) where

import Control.Applicative (Applicative (..), (<$>))
import Prelude             (Int)

import qualified Control.Lens              as L
import qualified Data.RAList.NonEmpty.Lens as NE

import Data.RAList

-------------------------------------------------------------------------------
-- Indexing
-------------------------------------------------------------------------------

ix :: forall f a. Applicative f => Int -> L.LensLike' f (RAList a) a
ix :: forall (f :: * -> *) a.
Applicative f =>
Int -> LensLike' f (RAList a) a
ix Int
_ a -> f a
_ RAList a
Empty         = RAList a -> f (RAList a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure RAList a
forall a. RAList a
Empty
ix Int
i a -> f a
f (NonEmpty NERAList a
xs) = NERAList a -> RAList a
forall a. NERAList a -> RAList a
NonEmpty (NERAList a -> RAList a) -> f (NERAList a) -> f (RAList a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> LensLike' f (NERAList a) a
forall (f :: * -> *) a.
Applicative f =>
Int -> LensLike' f (NERAList a) a
NE.ix Int
i a -> f a
f NERAList a
xs

-------------------------------------------------------------------------------
-- Instances
-------------------------------------------------------------------------------

instance L.Each (RAList a) (RAList b) a b

type instance L.Index (RAList a)   = Int
type instance L.IxValue (RAList a) = a

instance L.Ixed (RAList a) where
    ix :: Index (RAList a) -> Traversal' (RAList a) (IxValue (RAList a))
ix Index (RAList a)
i = Int -> LensLike' f (RAList a) a
forall (f :: * -> *) a.
Applicative f =>
Int -> LensLike' f (RAList a) a
ix Int
Index (RAList a)
i