Utils.purs 1.07 KB
Newer Older
1
module Gargantext.Utils where
2 3

import Prelude
4
import Data.Lens (Lens', lens)
5
import Data.Newtype (class Newtype, unwrap, wrap)
6 7
import Data.Set as Set
import Data.Set (Set)
8

James Laver's avatar
James Laver committed
9 10 11 12
-- | Astonishingly, not in the prelude
id :: forall a. a -> a
id a = a

13 14 15 16 17 18 19 20
setterv :: forall nt record field. Newtype nt record => (record -> field -> record) -> field -> nt -> nt
setterv fn v t = (setter (flip fn v) t)

setter :: forall nt record. Newtype nt record => (record -> record) -> nt -> nt
setter fn = wrap <<< fn <<< unwrap

getter :: forall record field nt. Newtype nt record => (record -> field) -> nt -> field
getter fn = fn <<< unwrap
21 22 23 24 25 26

-- TODO: not optimal but Data.Set lacks some function (Set.alter)
toggleSet :: forall a. Ord a => a -> Set a -> Set a
toggleSet a s
  | Set.member a s = Set.delete a s
  | otherwise      = Set.insert a s
27 28 29 30 31 32 33 34 35 36

-- Default sort order is ascending, we may want descending
invertOrdering :: Ordering -> Ordering
invertOrdering LT = GT
invertOrdering GT = LT
invertOrdering EQ = EQ

-- A lens that always returns unit
_unit :: forall s. Lens' s Unit
_unit = lens (\_ -> unit) (\s _ -> s)