BoxLang: Our new JVM Dynamic Language made by Ortus! Check it out: https://www.boxlang.io

Issues

Select view

Select search mode

 
50 of 79

Please add new high-order function partitionBy()/separate()/whateveryouwanttocallit()

Description

Filter is great, but sometimes you need to process both sides of the operation. It would be handy to have a bif like the bad-practice filter() example below that returned a struct with two arrays - pass/fail, true/false, succeeded/failed w/e.  Maybe separate() or partitionBy(). In the example below, see the filter function itself populating a top-level struct with succeeded and failed elements in one pass, rather than running the filter operation twice. The rest of the code is just for context. This is processing the results from importing a number of disparate spreadsheets, and both those that failed and those that succeeded need to be dealt with, though the handling and messaging are different for each category. 

So we don’t get hung up on the example, the reason for the top-level struct is that getResults() can be called randomly from different channel providers in megaphone, based on user preferences, but they all need to process the same results.

Attachments

1
Pinned fields
Click on the next to a field label to start pinning.

Details

Assignee

Reporter

Components

Priority

Sentry

Created July 18, 2024 at 3:08 PM
Updated August 28, 2024 at 3:52 AM

Activity

Show:

John WilsonJuly 18, 2024 at 8:02 PM

Nice. Very fast compilation now, too!

Brad WoodJuly 18, 2024 at 7:55 PM
Edited

Note, Java’s stream partitioning does not separate contiguous values like Clojure, which makes it work the same as a binary group by. Here is some example BoxLang code you can run right now, showcasing how easy streams are to use. Note our BoxLang lambda gets coerced into a Predicate functional interface.

and here’s an example of using Java’s stream grouping in BoxLang. Note a parallel stream doesn’t guarantee encounter order will be maintained in the final result.

You can paste either of those examples into try.boxlang.io

Brad WoodJuly 18, 2024 at 7:22 PM

Great suggestions guys. You could prolly dip into Java streams to do this pretty easy, but I can see a use case for a built in BIF as well. We’ll discuss.

Sean CorfieldJuly 18, 2024 at 4:43 PM

CF-specific (or at least CF-peculiar) solutions would belong in bx-compat – and CFCollection is a grab bag of all sorts of things that should not be built-in to the language itself.

Adding groupBy feels borderline to me but sufficiently useful that having it in BoxLang is probably reasonable.

John WilsonJuly 18, 2024 at 4:09 PM

I would like a groupBy function, too, and use it regularly from CFCollection, many of which functions should be added as native members. ( Chunk is another favorite, as is unique, and I like the shortcuts offered by pluck, sum, avg, count, etc. vs map/reduce… ) Your example does make sense. Maybe something could be done using signatures, simple-value vs closure? Simple values should work as the CFCollection version does.

Flag notifications