Skip to content
Browse files

Handle arrays as Stream sources. Fixes #4

  • Loading branch information...
1 parent 6d5d6b8 commit 86d83a1a7a99cffd602c2229bcd6592e79b9a473 @timyates committed Jul 3, 2012
Showing with 36 additions and 1 deletion.
  1. +1 −1 build.gradle
  2. +23 −0 src/main/groovy/groovy/stream/Stream.groovy
  3. +12 −0 src/test/groovy/groovy/stream/ListTests.groovy
View
2 build.gradle
@@ -12,7 +12,7 @@ dependencies {
compileGroovy.options.compilerArgs << "-Xlint:unchecked" << "-Werror"
group = 'com.bloidonia'
-version = '0.3'
+version = '0.4'
groovydoc {
docTitle = "Groovy Stream: A Lazy Generator"
View
23 src/main/groovy/groovy/stream/Stream.groovy
@@ -178,6 +178,29 @@ public class Stream<T> implements StreamInterface<T> {
}
/**
+ * The starting point for a Stream taking an array.
+ *
+ * @param array The The array to use for the stream
+ * @return A Stream that will iterate over the passed array, with
+ * <code>where</code> set to <code>{true}</code>,
+ * <code>transform</code> set to <code>{it}</code> and
+ * <code>using</code> set to the empty Map.
+ */
+ public static <T> Stream from( T[] array ) { fromArray( array ) }
+ public static Stream from( byte[] array ) { fromArray( array ) }
+ public static Stream from( char[] array ) { fromArray( array ) }
+ public static Stream from( short[] array ) { fromArray( array ) }
+ public static Stream from( int[] array ) { fromArray( array ) }
+ public static Stream from( long[] array ) { fromArray( array ) }
+ public static Stream from( float[] array ) { fromArray( array ) }
+ public static Stream from( double[] array ) { fromArray( array ) }
+ public static Stream from( boolean[] array ) { fromArray( array ) }
+
+ private static Stream fromArray( array ) {
+ new Stream( type:StreamType.OTHER, wrapped:new StreamImpl( { array.toList() }, { true }, { it }, [:] ) )
@jetztgradnet
jetztgradnet added a note Jul 3, 2012

How about using java.util.Arrays.asList(array) instead of array.toList()?

array.toList() is implemented like this in DGM.toList(T[] array):
return new ArrayList(Arrays.asList(array));

This means the list is copied, what might not be the intended behavior.

@timyates
Owner
timyates added a note Jul 4, 2012

@jetztgradnet I had a think about this overnight, and I think doing it this way is more consistent for Object[] vs primitive[]

The array.toList() call for primitive arrays goes through DefaultTypeTransformation.primitiveArrayToList, which I would need to either call directly, or replicate its functionality and we would end up with different behaviour for Object arrays vs primitive arrays (the array backed list for Object[] and a copy for int[]). Does that seem right?

@jetztgradnet
jetztgradnet added a note Jul 5, 2012

Hm, having it consistently would be nice, I agree with you. But I'd rather have it consistent by not copying the array. A List wrapper for each of the scalar types is probably not what you want, right?

I suggest adding a hint in the javadoc, which indicates that the array will be copied.

@timyates
Owner
timyates added a note Jul 5, 2012

@jetztgradnet Good idea :-) Will this suffice?

Thanks for the input :-) I really appreciate it!

@jetztgradnet
jetztgradnet added a note Jul 5, 2012

Yes, that's ok.

Would you possibly accept a patch, that implements using arrays withouth copying into a list?

@timyates
Owner
timyates added a note Jul 5, 2012

What would the use case be for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
+ /**
* A basic filter for the Stream. Elements in the Stream are passed to this
* Closure, and only those that cause this closure to return something
* passing Groovy Truth will be returned from the Stream. For Map based
View
12 src/test/groovy/groovy/stream/ListTests.groovy
@@ -12,4 +12,16 @@ public class ListTests extends spock.lang.Specification {
then:
result == [ [ 1, 0 ], [ 2, 1 ], [ 3, 2 ] ]
}
+
+ def "test arrays"() {
+ setup:
+ int[] array = 1..3
+ def stream = Stream.from array transform { [ it, idx++ ] } using idx:0
+
+ when:
+ def result = stream.collect()
+
+ then:
+ result == [ [ 1, 0 ], [ 2, 1 ], [ 3, 2 ] ]
+ }
}

0 comments on commit 86d83a1

Please sign in to comment.
Something went wrong with that request. Please try again.