{-# LANGUAGE Rank2Types #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Data.HashSet.Lens
-- Copyright   :  (C) 2012-16 Edward Kmett
-- License     :  BSD-style (see the file LICENSE)
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  provisional
-- Portability :  portable
--
----------------------------------------------------------------------------
module Data.HashSet.Lens
  ( setmapped
  , setOf
  , hashMap
  ) where

import Control.Lens.Getter (Getting, views)
import Control.Lens.Iso (iso)
import Control.Lens.Setter (setting)
import Control.Lens.Type
import qualified Data.HashSet as HashSet
import Data.HashSet (HashSet, fromMap, toMap)
import Data.HashMap.Lazy (HashMap)
import Data.Hashable

-- $setup
-- >>> :set -XNoOverloadedStrings
-- >>> import Control.Lens

-- | This 'Setter' can be used to change the type of a 'HashSet' by mapping
-- the elements to new values.
--
-- Sadly, you can't create a valid 'Traversal' for a 'Set', but you can
-- manipulate it by reading using 'Control.Lens.Fold.folded' and reindexing it via 'setmapped'.
setmapped :: (Eq j, Hashable j) => IndexPreservingSetter (HashSet i) (HashSet j) i j
setmapped :: forall j i.
(Eq j, Hashable j) =>
IndexPreservingSetter (HashSet i) (HashSet j) i j
setmapped = ((i -> j) -> HashSet i -> HashSet j)
-> IndexPreservingSetter (HashSet i) (HashSet j) i j
forall a b s t.
((a -> b) -> s -> t) -> IndexPreservingSetter s t a b
setting (i -> j) -> HashSet i -> HashSet j
forall b a.
(Hashable b, Eq b) =>
(a -> b) -> HashSet a -> HashSet b
HashSet.map
{-# INLINE setmapped #-}

-- | Construct a set from a 'Getter', 'Control.Lens.Fold.Fold', 'Control.Lens.Traversal.Traversal', 'Control.Lens.Lens.Lens' or 'Control.Lens.Iso.Iso'.
--
-- @
-- 'setOf' :: 'Hashable' a         => 'Getter' s a     -> s -> 'HashSet' a
-- 'setOf' :: ('Eq' a, 'Hashable' a) => 'Fold' s a       -> s -> 'HashSet' a
-- 'setOf' :: 'Hashable' a         => 'Iso'' s a       -> s -> 'HashSet' a
-- 'setOf' :: 'Hashable' a         => 'Lens'' s a      -> s -> 'HashSet' a
-- 'setOf' :: ('Eq' a, 'Hashable' a) => 'Traversal'' s a -> s -> 'HashSet' a
-- @
setOf :: Hashable a => Getting (HashSet a) s a -> s -> HashSet a
setOf :: forall a s. Hashable a => Getting (HashSet a) s a -> s -> HashSet a
setOf Getting (HashSet a) s a
l = Getting (HashSet a) s a -> (a -> HashSet a) -> s -> HashSet a
forall s (m :: * -> *) r a.
MonadReader s m =>
LensLike' (Const r) s a -> (a -> r) -> m r
views Getting (HashSet a) s a
l a -> HashSet a
forall a. Hashable a => a -> HashSet a
HashSet.singleton
{-# INLINE setOf #-}

-- | An `Iso` between a `HashSet` and a `HashMap` with unit values. \(\mathcal{O}(1)\).
hashMap :: Iso' (HashSet a) (HashMap a ())
hashMap :: forall a (p :: * -> * -> *) (f :: * -> *).
(Profunctor p, Functor f) =>
p (HashMap a ()) (f (HashMap a ()))
-> p (HashSet a) (f (HashSet a))
hashMap = (HashSet a -> HashMap a ())
-> (HashMap a () -> HashSet a)
-> Iso (HashSet a) (HashSet a) (HashMap a ()) (HashMap a ())
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso HashSet a -> HashMap a ()
forall a. HashSet a -> HashMap a ()
toMap HashMap a () -> HashSet a
forall a. HashMap a () -> HashSet a
fromMap