#2111 List.excludeNull()

SlimerDude Sun 17 Mar 2013

A couple of times now I've found myself writing:

myList.exclude |obj| { obj == null }

Would it be worth adding an excludeNull method to List?

DanielFath Sun 17 Mar 2013

No it wouldn't be worth it. You have a list that can never store null. On it the method would be pointless.

SlimerDude Sun 17 Mar 2013

I hear what you say about non nullable Lists, it's just that my use cases come from using map:

newList := myList
  .map |obj| {
     // may return null
     doSomething(obj)
  }
  .exclude |obj| {
     obj == null
  }

Anyway, t'was just a thought.

Jens Sat 23 Mar 2013

I also think excludeNull would be a bit too specific to be worth it.

It can be written a little more compactly with an it-block:

myList.exclude { it == null }

Login or Signup to reply.