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 new collection predicates: Contains, Index, and Unique #6

Closed
fthomas opened this issue May 24, 2015 · 14 comments
Closed

Add new collection predicates: Contains, Index, and Unique #6

fthomas opened this issue May 24, 2015 · 14 comments

Comments

@fthomas
Copy link
Owner

fthomas commented May 24, 2015

Contains is just a type alias (Contains[A] = Exists[Equal[A]]), Index[N, P] checks if the value at index N satisfies the predicate P, and Unique checks if all elements in a collection are unique.

@jgpacker
Copy link

For ordered collections, we could also have a Sorted[A] predicate.
Though I'm not sure how the user could choose between ascendent and descendent order.

@fthomas
Copy link
Owner Author

fthomas commented May 24, 2015

Good idea, @jgpacker ! We could have SortedAsc and SortedDesc or provide an extra type for the sort order: trait SortOrder; trait Ascending extends SortOrder; trait Descending extends SortOrder. Another possibility is to have something like SortedWith[P] where P is some kind of binary predicate (which we don't have yet). For example: SortedWith[GreaterEqual2] where GreaterEqual2 gives you a BinaryPredicate instance with isValid(t1: T, t2: T): Boolean = t1 >= t2.

@fthomas
Copy link
Owner Author

fthomas commented May 25, 2015

Another collection predicate: IndexWhere[PA, PI]. It could be used like this IndexWhere[Digit, LessEqual[_2]] which validates any collection of Char where one of the first three chars is a digit.

@fthomas
Copy link
Owner Author

fthomas commented May 25, 2015

  • Contains[A]
  • Index[N, P]
  • Unique
  • Head[P]
  • Last[P]
  • Tail[P]
  • Init[P]

@fthomas
Copy link
Owner Author

fthomas commented Nov 30, 2015

SortedWith as outlined in #6 (comment) is a misnomer. It should better be called Pairwise[P] since it checks if consecutive pairs of elements in a sequence satisfy the binary predicate P.

@sh0hei
Copy link
Contributor

sh0hei commented Mar 25, 2016

Hi, @fthomas :)

I want to Implement Unique Predicate.
I want you to tell me Signature of Unique and Usage of Unique.

@fthomas
Copy link
Owner Author

fthomas commented Mar 25, 2016

@sh0hei The idea of Unique is that it checks that a collection has no duplicated elements. It has no parameters, so would be defined as empty case class: case class Unique().

Usage would look something like this:

type UniqueList[A] = List[A] Refined Unique 

scala> RefType.applyRef[UniqueList[Int]](List(1, 2, 3))
res0: Either[String, UniqueList[Int]]= Right(Refined(List(1, 2, 3)))

scala>RefType.applyRef[UniqueList[Int]](List(1, 2, 2))
res1: Either[String, UniqueList[Int]]= Left("Predicate failed: List(1, 2, 2) contains duplicated elements: 2")

@sh0hei
Copy link
Contributor

sh0hei commented Mar 26, 2016

okay :)
I try it !

@fthomas
Copy link
Owner Author

fthomas commented Mar 26, 2016

Awesome! :-)

@sh0hei
Copy link
Contributor

sh0hei commented Apr 7, 2016

@fthomas Excuse me, I'd like also to know about Usage Tail, Init. I'll try to implement it.

@fthomas
Copy link
Owner Author

fthomas commented Apr 7, 2016

@sh0hei Tail and Init would be similar to the already existing Head and Last predicates (see collection.scala). They check if all but the first/last element of a sequence satisfy a given predicate.

@fthomas
Copy link
Owner Author

fthomas commented Apr 16, 2016

Closing since most of the predicates in this ticket are now implemented.

@fthomas fthomas closed this as completed Apr 16, 2016
@kubukoz
Copy link
Contributor

kubukoz commented Jun 11, 2018

Hey, what's the status on Sorted predicates?

@fthomas
Copy link
Owner Author

fthomas commented Jun 13, 2018

@kubukoz It hasn't been implemented yet. :) But I'm open to adding Sorted or Sorted{Asc, Desc} or something similar to the librarary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants