compact<A> function
- ImmutableList<
Option< listOfOptionsA> >
Compacts a list by filtering out None
values and extracting values from Some
options.
The compact
function transforms an ImmutableList of option.Option values into an ImmutableList
containing only the values inside the Some
options, effectively filtering out any None
values.
Example:
final optionsList = ImmutableList<option.Option<int>>([
option.Some(1),
option.None(),
option.Some(2),
option.None(),
option.Some(3),
]);
final compacted = compact(optionsList);
print(compacted); // Outputs: ImmutableList([1, 2, 3])
@param listOfOptions An ImmutableList of option.Option values to be compacted.
@return An ImmutableList containing only the values from Some
options.
Implementation
ImmutableList<A> compact<A>(ImmutableList<option.Option<A>> listOfOptions) =>
flatMap<option.Option<A>, A>((value) => value is option.Some<A>
? ImmutableList<A>([value.value])
: ImmutableList<A>([]))(listOfOptions);