spebbe / dartz

Functional programming in Dart
MIT License
756 stars 59 forks source link

Add "isEmpty" to collections - e.g. IList, ISet #23

Closed rich-j closed 5 years ago

rich-j commented 5 years ago

We would like to have a straight-forward way to evaluate collections for "empty". Today we use constructs such as myIList.headOption.isNone() and myISet.all((_)=>false). It would be nice to use something like myIList.isEmpty and myISet.isEmpty.

In https://github.com/spebbe/dartz/issues/17#issuecomment-513777240 you propose myIList == nil() - but that construct doesn't work well for ISet (a matching ISet order must be provided and is evaluated too).

We use "ternary" constructs along with the new collection operators for "spread" and "if". An isEmpty would clean up Flutter code such as

<Widget>[
  if (!myIList.isEmpty) ShowWidget(myIList),
  myIList.isEmpty ? EmptyWidget() : WidgetWithList(myIList),
  ...myIList.map((o)=>LineWidget(o)).toIterable(),
]

We definitely would like this on IList and ISet. For consistency it would probably make sense on the other collections such as IMap, IVector, etc.

Thanks.

spebbe commented 5 years ago

Hi! Sorry for the long wait -- I'm back in the real world now! I've added isEmpty to AVLTree, IList, IMap, ISet and IVector and released as 0.8.6 from master + rebased into 0_9_0_wip_2. I'm hopeful that I'll be able to get rid of that long lived branch and release a proper 0.9.0 soon.