Skip to content

Commit

Permalink
refactor(base): move implementation of Data.Vect.nubBy to global scope
Browse files Browse the repository at this point in the history
Closes #3285
  • Loading branch information
troiganto authored and gallais committed Jun 5, 2024
1 parent bcf8598 commit 2c128e2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ This CHANGELOG describes the merged but unreleased changes. Please see [CHANGELO
* Added `funExt0` and `funExt1`, functions analogous to `funExt` but for functions
with quantities 0 and 1 respectively.

* Moved definition of `Data.Vect.nubBy` to the global scope as `nubByImpl` to
allow compile time proofs on `nubBy` and `nub`.

#### Contrib

* `Data.List.Lazy` was moved from `contrib` to `base`.
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Thomas E. Hansen
Tim Süberkrüb
Timmy Jose
Tom Harley
troiganto
Wen Kokke
Wind Wong
Zoe Stafford
Expand Down
17 changes: 9 additions & 8 deletions libs/base/Data/Vect.idr
Original file line number Diff line number Diff line change
Expand Up @@ -662,21 +662,22 @@ filter p (x::xs) =
else
(_ ** tail)

public export
nubByImpl : Vect m elem -> (elem -> elem -> Bool) -> Vect len elem -> (p ** Vect p elem)
nubByImpl acc p [] = (_ ** [])
nubByImpl acc p (x::xs) with (elemBy p x acc)
nubByImpl acc p (x :: xs) | True = nubByImpl acc p xs
nubByImpl acc p (x :: xs) | False with (nubByImpl (x::acc) p xs)
nubByImpl acc p (x :: xs) | False | (_ ** tail) = (_ ** x::tail)

||| Make the elements of some vector unique by some test
|||
||| ```idris example
||| nubBy (==) (fromList [1,2,2,3,4,4])
||| ```
public export
nubBy : (elem -> elem -> Bool) -> Vect len elem -> (p ** Vect p elem)
nubBy = nubBy' []
where
nubBy' : forall len . Vect m elem -> (elem -> elem -> Bool) -> Vect len elem -> (p ** Vect p elem)
nubBy' acc p [] = (_ ** [])
nubBy' acc p (x::xs) with (elemBy p x acc)
nubBy' acc p (x :: xs) | True = nubBy' acc p xs
nubBy' acc p (x :: xs) | False with (nubBy' (x::acc) p xs)
nubBy' acc p (x :: xs) | False | (_ ** tail) = (_ ** x::tail)
nubBy = nubByImpl []

||| Make the elements of some vector unique by the default Boolean equality
|||
Expand Down

0 comments on commit 2c128e2

Please sign in to comment.