Skip to content

Commit

Permalink
Merge pull request #1540 from motlin/reduceBy
Browse files Browse the repository at this point in the history
Implement RichIterable.reduceBy(). Fixes #134
  • Loading branch information
donraab committed Feb 17, 2024
2 parents c3bec30 + 0b2914c commit 6d7c8a6
Show file tree
Hide file tree
Showing 30 changed files with 343 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -2363,6 +2363,42 @@ default <K, V, R extends MutableMapIterable<K, V>> R aggregateBy(
return target;
}

/**
* Applies an aggregate function over the iterable grouping results into a map based on the specific groupBy function.
* Aggregate results are allowed to be immutable as they will be replaced in place in the map.
*
* @since 12.0
*
*/
default <K> MapIterable<K, T> reduceBy(
Function<? super T, ? extends K> groupBy,
Function2<? super T, ? super T, ? extends T> reduceFunction)
{
return this.reduceBy(
groupBy,
reduceFunction,
Maps.mutable.empty());
}

/**
* Applies an aggregate function over the iterable grouping results into a map based on the specific groupBy function.
* Aggregate results are allowed to be immutable as they will be replaced in place in the map.
*
* @since 12.0
*/
default <K, R extends MutableMapIterable<K, T>> R reduceBy(
Function<? super T, ? extends K> groupBy,
Function2<? super T, ? super T, ? extends T> reduceFunction,
R target)
{
this.forEach(each ->
{
K key = groupBy.valueOf(each);
target.merge(key, each, reduceFunction);
});
return target;
}

/**
* Applies a groupBy function over the iterable, followed by a collect function.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -383,4 +383,21 @@ default <K, V, R extends MutableMapIterable<K, V>> R aggregateBy(
});
return target;
}

@Override
default <K, R extends MutableMapIterable<K, T>> R reduceBy(
Function<? super T, ? extends K> groupBy,
Function2<? super T, ? super T, ? extends T> reduceFunction,
R target)
{
this.forEachWithOccurrences((each, occurrences) ->
{
K key = groupBy.valueOf(each);
for (int i = 0; i < occurrences; i++)
{
target.merge(key, each, reduceFunction);
}
});
return target;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -194,6 +194,15 @@ default <K1, V1, V2> ImmutableMap<K1, V2> aggregateBy(
return map.toImmutable();
}

@Override
default <KK> ImmutableMapIterable<KK, V> reduceBy(
Function<? super V, ? extends KK> groupBy,
Function2<? super V, ? super V, ? extends V> reduceFunction)
{
MutableMap<KK, V> map = this.reduceBy(groupBy, reduceFunction, Maps.mutable.empty());
return map.toImmutable();
}

/**
* @deprecated in 8.0. Use {@link OrderedIterable#zip(Iterable)} instead.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -167,6 +167,14 @@ default <K1, V1, V2> MutableMap<K1, V2> aggregateBy(
return map;
}

@Override
default <KK> MutableMap<KK, V> reduceBy(
Function<? super V, ? extends KK> groupBy,
Function2<? super V, ? super V, ? extends V> reduceFunction)
{
return this.reduceBy(groupBy, reduceFunction, Maps.mutable.empty());
}

@Override
MutableBiMap<K, V> withKeyValue(K key, V value);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -43,7 +43,9 @@
import org.eclipse.collections.api.collection.primitive.ImmutableShortCollection;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.ImmutableMapIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.MutableMapIterable;
import org.eclipse.collections.api.map.primitive.ImmutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ImmutableObjectLongMap;
import org.eclipse.collections.api.multimap.ImmutableMultimap;
Expand Down Expand Up @@ -239,6 +241,15 @@ default <K, V> ImmutableMap<K, V> aggregateBy(
return map.toImmutable();
}

@Override
default <K> ImmutableMapIterable<K, T> reduceBy(
Function<? super T, ? extends K> groupBy,
Function2<? super T, ? super T, ? extends T> reduceFunction)
{
MutableMapIterable<K, T> map = this.reduceBy(groupBy, reduceFunction, Maps.mutable.empty());
return map.toImmutable();
}

/**
* @since 9.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -231,6 +231,15 @@ default <K1, V1, V2> ImmutableMap<K1, V2> aggregateBy(
return map.toImmutable();
}

@Override
default <KK> ImmutableMap<KK, V> reduceBy(
Function<? super V, ? extends KK> groupBy,
Function2<? super V, ? super V, ? extends V> reduceFunction)
{
MutableMap<KK, V> map = Maps.mutable.empty();
return this.reduceBy(groupBy, reduceFunction, map).toImmutable();
}

@Override
ImmutableMap<V, K> flipUniqueValues();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -173,4 +173,16 @@ default <K1, V1, V2> ImmutableMapIterable<K1, V2> aggregateBy(
valueFunction.valueOf(value)));
return map.toImmutable();
}

@Override
default <KK> ImmutableMapIterable<KK, V> reduceBy(
Function<? super V, ? extends KK> groupBy,
Function2<? super V, ? super V, ? extends V> reduceFunction)
{
MutableMap<KK, V> map = this.reduceBy(
groupBy,
reduceFunction,
Maps.mutable.empty());
return map.toImmutable();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -178,4 +178,9 @@ default <K1, V1, V2> ImmutableOrderedMap<K1, V2> aggregateBy(
{
throw new UnsupportedOperationException("Not implemented");
}

@Override
<KK> ImmutableOrderedMap<KK, V> reduceBy(
Function<? super V, ? extends KK> groupBy,
Function2<? super V, ? super V, ? extends V> reduceFunction);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -228,6 +228,17 @@ default <K1, V1, V2> MutableMap<K1, V2> aggregateBy(
return map;
}

@Override
default <KK> MutableMap<KK, V> reduceBy(
Function<? super V, ? extends KK> groupBy,
Function2<? super V, ? super V, ? extends V> reduceFunction)
{
return this.reduceBy(
groupBy,
reduceFunction,
Maps.mutable.empty());
}

@Override
MutableMap<V, K> flipUniqueValues();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -435,6 +435,17 @@ default <K1, V1, V2> MutableMapIterable<K1, V2> aggregateBy(
return map;
}

@Override
default <KK> MutableMapIterable<KK, V> reduceBy(
Function<? super V, ? extends KK> groupBy,
Function2<? super V, ? super V, ? extends V> reduceFunction)
{
return this.reduceBy(
groupBy,
reduceFunction,
Maps.mutable.empty());
}

@Override
default void forEach(BiConsumer<? super K, ? super V> action)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -77,6 +77,15 @@ default <K, VV> ImmutableMap<K, VV> aggregateBy(
return map.toImmutable();
}

@Override
default <K> ImmutableMap<K, V> reduceBy(
Function<? super V, ? extends K> groupBy,
Function2<? super V, ? super V, ? extends V> reduceFunction)
{
MutableMap<K, V> map = this.reduceBy(groupBy, reduceFunction, Maps.mutable.empty());
return map.toImmutable();
}

@Override
<VV> ImmutableBagMultimap<VV, V> groupByEach(Function<? super V, ? extends Iterable<VV>> function);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -58,6 +58,14 @@ default <K, VV> MutableMap<K, VV> aggregateBy(Function<? super V, ? extends K> g
Maps.mutable.empty());
}

@Override
default <K> MutableMap<K, V> reduceBy(
Function<? super V, ? extends K> groupBy,
Function2<? super V, ? super V, ? extends V> reduceFunction)
{
return this.reduceBy(groupBy, reduceFunction, Maps.mutable.empty());
}

@Override
<VV> MutableBagMultimap<VV, V> groupByEach(Function<? super V, ? extends Iterable<VV>> function);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -247,4 +247,13 @@ default <K1, V1, V2> ImmutableMap<K1, V2> aggregateBy(
valueFunction.valueOf(value)));
return map.toImmutable();
}

@Override
default <KK> ImmutableMap<KK, V> reduceBy(
Function<? super V, ? extends KK> groupBy,
Function2<? super V, ? super V, ? extends V> reduceFunction)
{
MutableMap<KK, V> map = this.reduceBy(groupBy, reduceFunction, Maps.mutable.empty());
return map.toImmutable();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -236,6 +236,15 @@ default <K, V> ImmutableMap<K, V> aggregateBy(
return map.toImmutable();
}

@Override
default <K> ImmutableMap<K, T> reduceBy(
Function<? super T, ? extends K> groupBy,
Function2<? super T, ? super T, ? extends T> reduceFunction)
{
MutableMap<K, T> map = this.reduceBy(groupBy, reduceFunction, Maps.mutable.empty());
return map.toImmutable();
}

@Override
<V> ImmutableObjectLongMap<V> sumByInt(Function<? super T, ? extends V> groupBy, IntFunction<? super T> function);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs and others.
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -259,4 +259,12 @@ default <K, V> MutableMap<K, V> aggregateBy(
});
return map;
}

@Override
default <K> MutableMap<K, T> reduceBy(
Function<? super T, ? extends K> groupBy,
Function2<? super T, ? super T, ? extends T> reduceFunction)
{
return this.reduceBy(groupBy, reduceFunction, Maps.mutable.empty());
}
}
Loading

0 comments on commit 6d7c8a6

Please sign in to comment.