Add Min and Max newtypes (#2011)

This commit is contained in:
Moritz Kiefer 2019-07-04 18:02:59 +02:00 committed by mergify[bot]
parent 6cc5510dae
commit 8b04206a35
5 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,27 @@
-- Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
daml 1.2
module DA.Semigroup where
-- | Semigroup under `min`
--
-- ```
-- > Min 23 <> Min 42
-- Min 23
-- ```
newtype Min a = Min a deriving (Eq, Ord, Show)
instance Ord a => Semigroup (Min a) where
Min a <> Min b = Min (min a b)
-- | Semigroup under `max`
--
-- ```
-- > Max 23 <> Max 42
-- Max 42
-- ```
newtype Max a = Max a deriving (Eq, Ord, Show)
instance Ord a => Semigroup (Max a) where
Max a <> Max b = Max (max a b)

View File

@ -41,6 +41,7 @@ import DA.Optional.Total
import DA.Optional
import DA.Random
import DA.Record
import DA.Semigroup
import DA.Text
import DA.TextMap
import DA.Time

View File

@ -0,0 +1,11 @@
-- Copyright (c) 2019, Digital Asset (Switzerland) GmbH and/or its affiliates.
-- All rights reserved.
daml 1.2
module SemigroupTest where
import DA.Semigroup
test = scenario do
assert $ Min (23 : Int) <> Min 42 == Min 23
assert $ Max (23 : Int) <> Max 42 == Max 42

View File

@ -24,6 +24,7 @@ import DA.NonEmpty()
import DA.Optional()
import DA.Random()
import DA.Record()
import DA.Semigroup()
import DA.Text()
import DA.Time()
import DA.Traversable()

View File

@ -42,3 +42,5 @@ HEAD — ongoing
- [DAML Standard Library] Add ``Sum`` and ``Product`` newtypes that
provide ``Monoid`` instances based on the ``Additive`` and ``Multiplicative``
instances of the underlying type.
- [DAML Standard Library] Add ``Min`` and ``Max`` newtypes that
provide ``Semigroup`` instances based ``min`` and ``max``.