List how does Find:Node<T> ( value:T ) works ??Monkey Forums/Monkey Beginners/List how does Find:Node<T> ( value:T ) works ??
| The learning curve is high when you only have this information.|
Maybe it does what I want: "search a list by field name value"
I can use a Map or loop all the values, but maybe its possible using this list Find function.
Method Find:Node<T> ( value:T )
Finds the node containing the first element in the list equal to value.
If no matching node is found, Null is returned.
Local list:=New List<Aclass> list.AddLast(New Aclass("test")) list.AddLast(New Aclass("monkey")) ' and then I was hoping not to get a "monkey" by finding it like this list.Find:<Aclass.name.Value>("monkey") Class Aclass Field name:String Method New (_name:String) name=_name End End
| To do what you want, you will have to create an extended version of List that overrides the Equals() function. Otherwise it will just compare the addresses of the two objects. |
Look at Mark's implementation of IntList:
Make something similar to the above, with the appropriate comparison. If you want to do sorting and such, you will need a new Compare function too.
I haven't tested it, but I think the following should work:
Some classes in Monkey such as List are quite simply coded and may be easier to understand from the code than the documentation!
| Oke thanks, I will test it later today.|
And that Find thing , what does that do and how to use that ?
I really miss some extra short examples the documentation than I can figure out of my own ;)
| If you write:|
acList.Find( monkey ) 'or acList.Find( New ACLass( "monkey" )
will both return a node pointing to monkey, because an object equal to it has been found in the list.
In retrospect, this probably isn't really what you want. I'd recommend making an ordinary list of List< AClass > like you did originally and iterating through the nodes. This is how it's done:
I haven't tested the above but I think it should work.
| Or you could incprporate that into an extended class, if you will be using it a lot:|
Now you can do what you probably wanted originally:
Local nod:Node< AClass > = acList.Find( "monkey" )
| Diddy's DiddyStack/DiddyList/DiddySet handle a lot of this for you if you make your class implement IComparable, or if you give it a comparator (a separate class that just does the comparison).|
| There was a small thing I needed to change. |
I'm trying to not using any extra modules, and to learn the basics ;)
| Ah yes, I forgot that, because when I'm using Nodes I always use an alias.|
Alias Node = list.Node
..at the top of the file will allow you to use Node as I did. (It's actually the only thing I ever use Alias for!)