Comments (13)
Good point and I hope you won't need channels for this.
So I think I'll keep the above Ruby inspired Enumerable and I'll also add the c++ stateful iterators:
it := list.Iterator()
for it.Next() {
value := it.Value()
...
}
Does this work for you?
For multiple lists it goes the same:
it1 := list1.Iterator()
it2 := list2.Iterator()
for it1.Next() && it2.Next() {
value1 := it1.Value()
value2 := it1.Value()
...
}
I think that's the cleanest pattern for god's containers, since there is no operator overloading.
from gods.
Is there reverse Iterator?
from gods.
Enumerator similar to C++ STL iterators, Ruby's Enumerator and Java's Enumeration classes that allows for internal and external iteration? I wanted to do that from the start, but never found the time for it. I think that's a great addition and allows for writing more optimized code! Let me first do some research on how other languages had implemented it, because this would require a large change and only ordered collection can support this feature.
from gods.
great work
from gods.
There are a few ways of implementing this.
I'd exclude the channels approach (inc. buffered channels) for performance reasons (albeit the channels have gotten faster) and (mis/over)use thereof. People seem to open a channel nowadays to add two numbers together and it feels like going after a fly with a bazooka just because bazookas are cool.
Also I am uneasy about the performance of the closurer approach .
So that leaves me with two patterns:
- Stateful iterator (Java style)
- Callback "iterator"/enumerator (Ruby and JavaScript style)
Despite being inclined towards Ruby's readability (dreaming of each/map/select/etc.), I will run some tests on both approaches this weekend and see which one works and looks better.
Thanks for pointing this out, it will be great addition to this project 👍
from gods.
I started working on enumerables and would appreciate your opinion if you would use this:
import "github.com/emirpasic/gods/lists/arraylist"
func main() {
list := arraylist.New()
list.Add("a", "b", "c")
list.Each(func(index interface{}, value interface{}) {
fmt.Println(value)
})
fmt.Println(list.Map(func(index interface{}, value interface{}) interface{} {
return value
}))
fmt.Println(list.Select(func(index interface{}, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "b"
}))
fmt.Println(list.Any(func(index interface{}, value interface{}) bool {
return value.(string) == "c"
}))
fmt.Println(list.All(func(index interface{}, value interface{}) bool {
return value.(string) >= "a" && value.(string) <= "c"
}))
fmt.Println(list.Find(func(index interface{}, value interface{}) bool {
return value.(string) == "c"
}))
}
Currently the enumrable interface supports the following:
package enumerable
import "github.com/emirpasic/gods/containers"
type Interface interface {
// Calls the given function once for each element, passing that element's index(key) and value.
Each(func(index interface{}, value interface{}))
// Invokes the given function once for each element and returns a
// container containing the values returned by the given function.
Map(func(index interface{}, value interface{}) interface{}) containers.Interface
// Returns a new container containing all elements for which the given function returns a true value.
Select(func(index interface{}, value interface{}) bool) containers.Interface
// Passes each element of the collection to the given function and
// returns true if the function ever returns true for any element.
Any(func(index interface{}, value interface{}) bool) bool
// Passes each element of the collection to the given function and
// returns true if the function returns true for all elements.
All(func(index interface{}, value interface{}) bool) bool
// Passes each element of the collection to the given function and returns
// the first for which the function is true or nil,nil otherwise if no element
// matches the criteria.
Find(func(index interface{}, value interface{}) bool) (index interface{}, value interface{})
}
Current implementation is on enumerable
branch and is purely Ruby inspired for readability. Only arraylist currently implemented, pending opinion if this is usable.
from gods.
I think it's ok.
But ciuld you think about cpp iterator?
If we want to use it throw the Each func on above, we must use channel to yield the loop.
If we want to use list1[x] list2[x] and so on...one by one, we can not break the loop and continue it unless using channel.
Or we use get func, the performance will be degenerate
from gods.
I think it's very good!
from gods.
Can add insert func with iter?
from gods.
Can add insert func with iter?
Insert func is already on master, thus I closed that issue:
List's insert definition
Example in test
Did you mean the above or the possibility to insert values into list during iteration (which would be hard)?
from gods.
This is almost done, PR is up, but need to update with examples and documentatio. Then I will merge it, for now you can checkout #12 if that's OK with you. Thanks again for giving me inspiration to do this :)
from gods.
thanks a lot for your work:)
with pleasure
from gods.
Is there reverse Iterator?
No, not yet, but it would be good to have, especially for some data structures where that is natural. For singly linked list that wouldn't be feasible, but for doubly linked list it would make absolute sense.
Can you open a new issue for reverse iterators please, because I am merging the PR that closes this issue. I'll tackle the reverse iterators in another issue + PR.
from gods.
Related Issues (20)
- priorityqueue question HOT 1
- What scenarios will this project be imported HOT 4
- Coverity Scan - Explicit null dereferenced HOT 1
- hashbidimaps Values() returns keys HOT 2
- Why not replace "%+v" to json.Marshal in utils.ToString
- Does it make sense to add goroutine / thread safe data structures for all the existing data structures? HOT 1
- arraylist.List should not maintain size HOT 1
- Add a Ordered linked HOT 2
- Hey! I am interested in translating this project into Portuguese, Brazil. Can I contribute? HOT 1
- I would like to know how to delete a node data while treemap loop read. I want to know where is my code wrong? HOT 2
- Add to Go official wiki? HOT 3
- generic upgrade request HOT 3
- RedBlackTree: Iterators become invalid after removing an element. HOT 2
- DS which can give element if present or next greater if not present, elements should be stored in sorted order HOT 2
- LinkedHashMap Sort? HOT 3
- Multiset support HOT 1
- deque support
- hashset should support NewWith Comparator
- Add method of Set lack info about the insert take place or not
- treeset:The names of each element are not equal, why is second_8 disappearing? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gods.