Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ByteSize measure #490

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions measures/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Changelog for `measures`

## 0.1.0.3
## 0.2.0.0

*
* Removed `Eq` superclass from `Measure`. I now see some types can not and/or
should not provide it.

* Added a `Bounded` instance for `Measure` instances.

* Added the `ByteSize` measure.

## 0.1.0.2

Expand Down
6 changes: 6 additions & 0 deletions measures/measures.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ library

exposed-modules:
Data.Measure
Data.Measure.ByteSize
Data.Measure.Class

build-depends: base
, base-deriving-via
, cardano-binary
, cardano-strict-containers
, deepseq
, nothunks
, quiet

test-suite test
hs-source-dirs: test
Expand Down
10 changes: 5 additions & 5 deletions measures/src/Data/Measure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ infix 4 <=, >=
--
-- It's only true if every component on the left is @<=@ the corresponding
-- component on the right.
(<=) :: Measure a => a -> a -> Prelude.Bool
(<=) :: (Prelude.Eq a, Measure a) => a -> a -> Prelude.Bool
x <= y = x Prelude.== min x y

-- | The partial order induced by 'max'
--
-- It's only true if every component on the left is @>=@ the corresponding
-- component on the right.
(>=) :: Measure a => a -> a -> Prelude.Bool
(>=) :: (Prelude.Eq a, Measure a) => a -> a -> Prelude.Bool
x >= y = x Prelude.== max x y

-- | Split a list once a prefix fills up the given capacity
--
-- Note that this just splits the given list; it does not attempt anything
-- clever like bin-packing etc.
splitAt :: Measure a => (e -> a) -> a -> [e] -> ([e], [e])
splitAt :: (Prelude.Eq a, Measure a) => (e -> a) -> a -> [e] -> ([e], [e])
splitAt measure limit =
go zero []
where
Expand All @@ -60,7 +60,7 @@ splitAt measure limit =
tot' = plus tot (measure e)

-- | @fst . 'splitAt' measure limit@, but non-strict
take :: Measure a => (e -> a) -> a -> [e] -> [e]
take :: (Prelude.Eq a, Measure a) => (e -> a) -> a -> [e] -> [e]
take measure limit =
go zero
where
Expand All @@ -74,7 +74,7 @@ take measure limit =
tot' = plus tot (measure e)

-- | @snd . 'splitAt' measure limit@, with a bit less allocation
drop :: Measure a => (e -> a) -> a -> [e] -> [e]
drop :: (Prelude.Eq a, Measure a) => (e -> a) -> a -> [e] -> [e]
drop measure limit =
go zero
where
Expand Down
Loading
Loading