timefold-solver icon indicating copy to clipboard operation
timefold-solver copied to clipboard

flattenLambda()

Open ge0ffrey opened this issue 3 months ago • 2 comments

Motivation:

.flattenLast() presumes the last argument is the collection to flatten. But it could be the first argument, or a property of the any argument, or a list calculated from 2 arguments.

Proposal:

provide a lambda method to the flatten() so the user can figure it out themselves

Strawmans:

Uni:

forEach(Shift) // Uni<Shift>
.flatten(shift -> shift.getRequiredSkills)) // Bi<Shift, Skill>

Bi:

forEach(Shift).join(Day, overlapping(...) // Bi<Shift, Day>
.flatten((shift, day) -> extractOverlappingHours(shift, day)) // Tri<Shift, Day, Hour>

ge0ffrey avatar Sep 12 '25 05:09 ge0ffrey

This would make .flattenFirst() no longer a feature gap (at most a syntactic sugar gap). This would make .expand().flattenLast() less code and faster (no intermediate tuples)

This would turn the tables: .flattenLast() is syntactic sugar for .flattenLambda.

ge0ffrey avatar Sep 12 '25 05:09 ge0ffrey

Naming proposals:

A) .flattenLambda() B) .flatten() C) .flattenAndExpand() D).expandAndFlatten()

I think A) .flattenLambda() is a terrible name. I maybe prefer B so far.

ge0ffrey avatar Sep 12 '25 05:09 ge0ffrey