-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce needed? #20
Comments
You're right... There's Now, do I call it |
Given there is map and filter, it should be reduce. Given there is collect, it should be inject. |
Ah, the joys of naming things... Think I'll go for |
Is this purely from a Java perspective as you can inject from groovy? |
Reduce works for me :-) I'm not sure what the question re Java is asking (possibly just me being slow, sorry). |
As of v0.8.0, you can do this in Java using the library: Map<String,List<Integer>> map = new HashMap<String,List<Integer>>() {{
put( "x", Arrays.asList( 1, 2, 3 ) ) ;
put( "y", Arrays.asList( 4, 5, 6 ) ) ;
}} ;
Stream.from( map )
.map( e -> e.get( "x" ) + e.get( "y" ) )
.filter( e -> e % 2 == 1 )
.forEachRemaining( System.out::println ) ; Or, in pre java 8: Stream<Integer> s = Stream.from( map )
.map( new Function<Map<String, Integer>, Integer>() {
@Override
public Integer call( Map<String, Integer> map ) {
return map.get( "x" ) + map.get( "y" );
}
} )
.filter( new Predicate<Integer>() {
@Override
public boolean call( Integer val ) {
return val % 2 == 1;
}
} ) ;
while( s.hasNext() ) {
System.out.println( s.next() );
} So from this Java point of view, there's no But from Groovy, you could use the standard @Grab( 'com.bloidonia:groovy-stream:0.8.1' )
import groovy.stream.*
// print the sum of the lengths of the words in dict/words
println new File( '/usr/share/dict/words' ).withReader { r ->
Stream.from( r ).map { s -> s.length() }.inject { a, b -> a + b }
} Which behaves as a |
I guess the question is, is there any new functionality (over the stock |
Java 8 Streams do have reduce methods. My worry about using the Groovy inject is that it might force the creation of a data structure rather than continuing to work with the iterator. |
Ahhh, I think I understand... Are you saying you want a In Java 8, I might be missing the point entirely though (as is often the case) And as I said before, as you have no |
Actually the issue is where the input is not a sequence of scalars, but a sequence of non-scalars. So for example:
The expression for The expression for Maybe this is just a theoretical problem and will never be met in reality? PS Is this |
Yeah, for def d = Stream.from(s).inject {t, i -> t + i}.sum()
assert d == 45
assert d instanceof Integer
assert [ [ 1 ], 2, [ 3, 4 ] ].sum() == [ 1, 2, 3, 4 ] As for the Java 8 question, my gut feeling tells me yes... The only thing I add is faux list comprehension (which could probably be written as a custom Supplier), and the |
The problem with I find the I hadn't spotted that |
Yeah, I've searched for the reason I see what you mean now about // Return a Stream containing a single Integer 6
Stream.from( [ 1, 2, 3 ] ).reduce { a, b -> a + b } Wouldn't I have to exhaust the input iterator to the reduce and then wrap that in a Stream? Maybe in this instance, |
I'll try and come up with an example. |
An example would be brilliant 😸 I had a thought, do you mean some sort of feedback iterator, where it passes the previous result and the next element from the upstream iterator? Something like: // Feedback with `null` as the initial last element
def elements = Stream.from( [ 1, 2, 3 ] ).feedback { last, next -> [ last, next ] }.collect()
assert elements == [ [ null, 1 ], [ [ null, 1 ], 2 ], [ [ [ null, 1 ], 2 ], 3 ] ] Or (as a more simple example) // Feedback starting with an initial value of 0 for `last`
def elements = Stream.from( [ 1, 2, 3 ] ).feedback( 0 ) { last, next -> last + next }.collect()
assert elements == [ 1, 3, 6 ] Not sure if this would cover your use-case, or indeed if |
I may just be missing it, but there appears not to be a reduce operation on streams.
The text was updated successfully, but these errors were encountered: