-
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
Do we need more functional elements? #9
Comments
Added an |
What about |
So you'd do:
Rather than
? What does it gain you? |
Might be nice for Stream to optionally store some stats. Four counts might be enough: processed - count of all items that went through all steps without be filtered out |
More descriptive I suppose. Say I am processing a list of people with some dirty data. I only want males (filter), but say one of the people has a number for a last name, I would put that check in |
Yeah, that would be nice... Need to think where this data can be stored... I was thinking the other week, if we get rid of the Still not decided in my head whether it's worth abandoning |
Although with that example I could still use just conditionals and have |
To get the stats to work, you could implement |
Actually, after looking at |
Added the stats as issue #14 so I don't forget or lose this 😉 |
This is probably totally out of scope, but might as well shoot for the stars! What about chunking? Say you have Stream.from 1..8 chunk(5) This will create a list with two element, each containing a list of their own. The first element contains 1 to 5, the second 6 to 8. Each chunk has no more than 5 elements. Pretty handy if you want to process data to a database in transactional chunks. |
I like it, kinda the opposite of I think Or was that I'll do some searching... Can you add this as a new issue? |
I have a batch iterator lying around somewhere that can probably do this. Basically wraps another iterator and returns 'chunks'. Let me see if I can use it here. |
I like the idea of making the stream a chain of iterators. If someone wanted to make a stream 'plugin', you could do it easily using a groovy extension. You could also have a method |
The only problem is that I think we would have to get rid of the |
I think the flexibility from chained iterators warrants canning |
That's my worry too ;-) Ooooh...shiny! |
so when I see |
Actually never mind, noticed the last example on the web page would not work. Oh well. |
That last example doesn't work with v0.6, as I removed I need to revisit the docs, and get them up to scratch Since v0.6, that code becomes:
Which could equally be written as:
Avoiding the |
Right, thrown up a quick changed version to http://timyates.github.io/groovy-stream/ Documentation is always the first casualty of coding ;-) |
Last thing I thought about... If I am iterating through something big, I frequently want to give feed back for every n records. What about something like Stream.from(somethingBig).atEvery(1000){log.info "I processed [$it] records"}.filter{}.map{} The other option is to do module in map. |
I like that idea 😃 Need to think what it should be called... It's kinda like the Maybe |
hmmm... I like |
I like tap too... ;-) came up with this: |
Damn that was fast. I am sad, was going to pound this out tonight On Wed, Oct 23, 2013 at 3:38 PM, Tim Yates [email protected] wrote:
|
Sorry, I was left home alone with the kids asleep ;-) There's probably a better way of doing it than that way (so it's in a separate branch) :-) Code golf! Game on! :-D |
after using groovy-stream a decent amount, at this point I am not sure I like |
Also, I started created extensions to stream to handle events I commonly deal with. Maybe I could carve them out of my projects and make a groovy-stream-ext project.... or something like that. Basically I have |
Hiya! Sorry for the late response, multiple family birthdays and rampant cold viruses are to blame... Re: Re: extensions. Brilliant! :-D A separate module with static Stream extensions would be brilliant (so we can call Nice work! |
I already have done a lot of this already in a separate project for work. So it will be a matter of carving it out for public consumption. I will create a module called In addition to Anyway, with the extensions we could be on our way to creating a lightweight ETL scripting tool which is pretty darn cool. If you like it, you are welcome to merge it into this project as a multi module build. |
Yeah, if you extend an existing method I believe it completely overwrites the original method, I think it should be ok in this case though as it would have a different method signature 😉 I reckon two separate projects to start with with strong href links between them is the place to start 😄 Going to spend the weekend thinking about "Stream without using", we could take a few methods from RxJava as well (which does a similar thing but in an asynchronous push way, rather then the stream which is a synchronous pull) |
I think I won't extend I was thinking about the async stuff the other day. At very least a decent opportunity for actors (or seda) I think. Basically a unit of work for grab -> process -> put (much like the laundrey problem, wash your clothes while they are drying, but can't dry the washed stuff until the dryer is done). The more steps you have the more you will benefit from parallel processing while everything maintains order. You could still use the main thread as well if using the Actor model (at least I think so) and make it synchronous. Please correct me if I am being stupid, not exactly my specialty. |
From a cursory glance I like it. Might open it up at lunch today for a more thorough review. So what happens when Java 8 comes out? Will this project still be relevant? I know very little about Java 8's stream ambitions, but have seen similar concepts. |
Oh, and what does |
Yeah, the Java 8 streams are a very similar beast. Indeed, I renamed a lot of the functions in this package to follow their naming conventions The only tricky bit I can think of would be using lambdas with the map/filter/etc blocks (and I haven't yet tested to see what happens)... With the map and filter steps, we set the delegate of the passed in closure, so that when you pass I am not sure if lambdas can be used in place of Closures in this instance :-( More testing is required... Also, this package may be made obsolete by Java Streams when java 8 is the de-facto standard... I guess this is also usable in Java 6+ (should be Java5+, but I can't test Java 5 at the moment) My thoughts about a Say you have 2 iterators (or streams), you could have a: Stream streamA = Stream.from 1..3
Stream streamB = Stream.from 'a'..'c'
Stream streamC = streamA.zip( streamB ) { a, b -> [ a: a, b: b ] }
assert streamC.collect() == [ [ a:1, b:'a' ], [ a:2, b:'b' ], [ a:3, b:'c' ] ] Or even: Stream streamA = Stream.from 1..3
Stream streamB = Stream.from 'a'..'c'
Stream streamC = streamA.zip( streamB ) { a, b -> [ a, b ] } flatMap { it }
assert streamC.collect() == [ 1, 'a', 2, 'b', 3, 'c' ] So basically a transpose call on iterators... It worked against Mr Stay-Puft 😆 |
So I guess the main plus over java streams would be the use of closures? I assume you have functionality they don't have too? So what about |
Yeah, they have concat as a static method of Stream: So you'd do: Stream.concat( streamA,streamB ) Not sure whether I prefer this, or: streamA.concat( streamB ) In RxJava (the push version of streams if you think of Streams as being a "pull" technique), they have And |
Yeah, I guess you can't support both. The second option has the appeal of maintaining method chaining which streamA.map{}.filter{}.concat(streamB).map{}.filter{}.etc{} It wouldn't be unusual to transform a stream to adhere to a similar one and then process them both at once. Both methods accomplish this, but the fluid one looks better to me. |
Actually, why not support both with |
I'm also thinking of adding |
yes please! |
If you got really nutty you could also do |
We could call them both concat as they would have different forms wouldn't they? The 2 arg
|
So I thought groovy gets mad if you have a static and non static method with the same name regardless of different parameters? If I am wrong then yeah, seems good. Although this is in java now so maybe it is fine. |
You could also do |
Think it's ok... THis seems to work: class Test {
String name
static stuff( String name ) {
println "Mmmm.. $name..."
new Test( name: name )
}
def stuff() {
println "Lovely $name..."
}
}
Test.stuff( 'Stuff' ).stuff() And prints: Mmmm.. Stuff...
Lovely Stuff... |
Huh... Maybe this is a grails thing? I swear I upgraded something groovy related and had to stop doing that. I tested it out, seems to work for me too. Yeah, supporting both seems reasonable then. |
Added |
And added |
Not sure if this is a functional element, but I've needed to do a lazy sort (even multi sort at times). I think RxJava has a toSortedList(Func2). |
So currently you can do:
And to stop a stream you can do:
But I need to think whether it's worth adding some different constructs instead, something like:
Anyone got any opinions before I arbitrarily make a decision?
Knowing me, you've got a good few weeks ;-)
The text was updated successfully, but these errors were encountered: