From 802ce118c29fb5034f3cf11e8c65fc7ab2ce14f9 Mon Sep 17 00:00:00 2001 From: James Laver <james.laver@gmail.com> Date: Fri, 7 Jun 2019 15:44:14 +0200 Subject: [PATCH] Add G.Utils.Range for working with closed intervals --- src/Gargantext/Utils/Range.purs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/Gargantext/Utils/Range.purs diff --git a/src/Gargantext/Utils/Range.purs b/src/Gargantext/Utils/Range.purs new file mode 100644 index 00000000..b6c06121 --- /dev/null +++ b/src/Gargantext/Utils/Range.purs @@ -0,0 +1,23 @@ +module Gargantext.Utils.Range + ( class Range, clamp, within + , Closed(..), closedProbability + ) where + +import Prelude ((<<<), (&&), (<=), (>=), min, max) +import Data.Ord (class Ord) + +class Range r v where + clamp :: r -> v -> v + within :: r -> v -> Boolean + +-- | A Closed Interval, in math speak +newtype Closed t = Closed { min :: t, max :: t } + +instance closedRange :: Ord t => Range (Closed t) t where + clamp (Closed r) = max r.min <<< min r.max + within (Closed r) v = (v <= r.max) && (v >= r.min) + +-- | A closed range between 0 and 1 +closedProbability :: Closed Number +closedProbability = Closed { min: 0.0, max: 1.0 } + -- 2.21.0