packages feed

one-0.0.1: src/Control/One/ReviewOne.hs

{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wall -Werror #-}

module Control.One.ReviewOne where

import Control.Applicative (Const, ZipList)
import Control.Lens (Review, unto, _Just, _Right, _Wrapped)
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LazyByteString
import qualified Data.ByteString.Short as ShortByteString
import Data.Functor.Identity (Identity)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.HashSet (HashSet)
import qualified Data.HashSet as HashSet
import Data.Hashable (Hashable)
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.List.NonEmpty (NonEmpty (..))
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Ord (Down)
import Data.Semigroup (Dual, First, Last, Max, Min, Product, Sum, WrappedMonoid)
import Data.Sequence (Seq)
import qualified Data.Sequence as Sequence
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LazyText
import Data.Tuple (Solo (..))
import Data.Word (Word8)
import GHC.Generics (Par1)

-- $setup
-- >>> import Control.Lens (review)

class ReviewOne x where
  type ReviewOneItem x

  reviewOne :: Review x (ReviewOneItem x)

-- | >>> review reviewOne 7 :: [Int]
-- [7]
instance ReviewOne [a] where
  type ReviewOneItem [a] = a

  reviewOne = unto (: [])

-- | >>> review reviewOne 7 :: NonEmpty Int
-- 7 :| []
instance ReviewOne (NonEmpty a) where
  type ReviewOneItem (NonEmpty a) = a

  reviewOne =
    unto
      (:| [])

-- | >>> review reviewOne 7 :: Seq Int
-- fromList [7]
instance ReviewOne (Seq a) where
  type ReviewOneItem (Seq a) = a

  reviewOne =
    unto
      Sequence.singleton

-- | >>> review reviewOne 'a' :: Text.Text
-- "a"
instance ReviewOne Text.Text where
  type ReviewOneItem Text.Text = Char

  reviewOne =
    unto
      Text.singleton

-- | >>> review reviewOne 'a' :: LazyText.Text
-- "a"
instance ReviewOne LazyText.Text where
  type ReviewOneItem LazyText.Text = Char

  reviewOne =
    unto
      LazyText.singleton

-- | >>> review reviewOne 65 :: ByteString.ByteString
-- "A"
instance ReviewOne ByteString.ByteString where
  type ReviewOneItem ByteString.ByteString = Word8

  reviewOne =
    unto
      ByteString.singleton

-- | >>> review reviewOne 65 :: LazyByteString.ByteString
-- "A"
instance ReviewOne LazyByteString.ByteString where
  type ReviewOneItem LazyByteString.ByteString = Word8

  reviewOne =
    unto
      LazyByteString.singleton

-- | >>> review reviewOne 65 :: ShortByteString.ShortByteString
-- "A"
instance ReviewOne ShortByteString.ShortByteString where
  type ReviewOneItem ShortByteString.ShortByteString = Word8

  reviewOne =
    unto
      ShortByteString.singleton

-- | >>> review reviewOne ("key", 7 :: Int) :: Map String Int
-- fromList [("key",7)]
instance ReviewOne (Map k v) where
  type ReviewOneItem (Map k v) = (k, v)

  reviewOne =
    unto
      (uncurry Map.singleton)

-- | >>> review reviewOne ("key", 7 :: Int) :: HashMap String Int
-- fromList [("key",7)]
instance (Hashable k) => ReviewOne (HashMap k v) where
  type ReviewOneItem (HashMap k v) = (k, v)

  reviewOne =
    unto
      (uncurry HashMap.singleton)

-- | >>> review reviewOne (1, 7 :: Int) :: IntMap Int
-- fromList [(1,7)]
instance ReviewOne (IntMap v) where
  type ReviewOneItem (IntMap v) = (Int, v)

  reviewOne =
    unto
      (uncurry IntMap.singleton)

-- | >>> review reviewOne 7 :: Set Int
-- fromList [7]
instance ReviewOne (Set a) where
  type ReviewOneItem (Set a) = a

  reviewOne =
    unto
      Set.singleton

-- | >>> review reviewOne 7 :: HashSet Int
-- fromList [7]
instance (Hashable a) => ReviewOne (HashSet a) where
  type ReviewOneItem (HashSet a) = a

  reviewOne =
    unto
      HashSet.singleton

-- | >>> review reviewOne 7 :: IntSet
-- fromList [7]
instance ReviewOne IntSet where
  type ReviewOneItem IntSet = Int

  reviewOne =
    unto
      IntSet.singleton

-- | >>> review reviewOne 7 :: (String, Int)
-- ("",7)
instance (Eq a, Monoid a) => ReviewOne (a, b) where
  type ReviewOneItem (a, b) = b

  reviewOne =
    unto
      (mempty,)

-- | >>> review reviewOne 7 :: Maybe Int
-- Just 7
instance ReviewOne (Maybe a) where
  type ReviewOneItem (Maybe a) = a

  reviewOne =
    _Just
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Either String Int
-- Right 7
instance ReviewOne (Either a b) where
  type ReviewOneItem (Either a b) = b

  reviewOne =
    _Right
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Identity Int
-- Identity 7
instance ReviewOne (Identity a) where
  type ReviewOneItem (Identity a) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Const Int String
-- Const 7
instance ReviewOne (Const a b) where
  type ReviewOneItem (Const a b) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: First Int
-- First {getFirst = 7}
instance ReviewOne (First a) where
  type ReviewOneItem (First a) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Last Int
-- Last {getLast = 7}
instance ReviewOne (Last a) where
  type ReviewOneItem (Last a) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: WrappedMonoid Int
-- WrapMonoid {unwrapMonoid = 7}
instance ReviewOne (WrappedMonoid a) where
  type ReviewOneItem (WrappedMonoid a) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Dual Int
-- Dual {getDual = 7}
instance ReviewOne (Dual a) where
  type ReviewOneItem (Dual a) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Down Int
-- Down 7
instance ReviewOne (Down a) where
  type ReviewOneItem (Down a) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Sum Int
-- Sum {getSum = 7}
instance ReviewOne (Sum a) where
  type ReviewOneItem (Sum a) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Product Int
-- Product {getProduct = 7}
instance ReviewOne (Product a) where
  type ReviewOneItem (Product a) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Min Int
-- Min {getMin = 7}
instance ReviewOne (Min a) where
  type ReviewOneItem (Min a) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Max Int
-- Max {getMax = 7}
instance ReviewOne (Max a) where
  type ReviewOneItem (Max a) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: ZipList Int
-- ZipList {getZipList = [7]}
instance ReviewOne (ZipList a) where
  type ReviewOneItem (ZipList a) = a

  reviewOne =
    _Wrapped . reviewOne
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Par1 Int
-- Par1 {unPar1 = 7}
instance ReviewOne (Par1 a) where
  type ReviewOneItem (Par1 a) = a

  reviewOne =
    _Wrapped
  {-# INLINE reviewOne #-}

-- | >>> review reviewOne 7 :: Solo Int
-- MkSolo 7
instance ReviewOne (Solo a) where
  type ReviewOneItem (Solo a) = a

  reviewOne =
    unto
      MkSolo
  {-# INLINE reviewOne #-}