Java Stream: divide into two lists by boolean predicate

Collectors.partitioningBy: Map<Boolean, List<Employee>> partitioned = listOfEmployees.stream().collect( Collectors.partitioningBy(Employee::isActive)); The resulting map contains two lists, corresponding to whether or not the predicate was matched: List<Employee> activeEmployees = partitioned.get(true); List<Employee> formerEmployees = partitioned.get(false); There are a couple of reasons to use partitioningBy over groupingBy (as suggested by Juan Carlos Mendoza): Firstly, the parameter of groupingBy is a Function<Employee, …

Read more

Why doesn’t java.util.Collection implement the new Stream interface?

Yes, there are excellent reasons for these decisions 🙂 The key is the difference between eager and lazy operations. The examples you give under the first question show eager operations where mapping or filtering a list produces a new list. There’s nothing wrong with this, but it is often not what you want, because you’re …

Read more

Is there a Python equivalent of the Haskell ‘let’

You could use a temporary list comprehension [(barcode(productId), metric(size)) for name, size in [lookup(productId)]][0] or, equivalently, a generator expression next((barcode(productId), metric(size)) for name, size in [lookup(productId)]) but both of those are pretty horrible. Another (horrible) method is via a temporary lambda, which you call immediately (lambda (name, size): (barcode(productId), metric(size)))(lookup(productId)) I think the recommended “Pythonic” …

Read more

Are there any purely functional Schemes or Lisps?

The new Racket language (formerly PLT Scheme) allows you to implement any semantics you like with s-expressions (really any syntax). The base language is an eagerly evaluated, dynamically typed scheme variant but some notable languages built on top are a lazy scheme and a functional reactive system called Father Time. An easy way to make …

Read more

Is Haskell truly pure (is any language that deals with input and output outside the system)?

Take the following mini-language: data Action = Get (Char -> Action) | Put Char Action | End Get f means: read a character c, and perform action f c. Put c a means: write character c, and perform action a. Here’s a program that prints “xy”, then asks for two letters and prints them in …

Read more