List how does Find:Node<T> ( value:T ) works ??

Monkey Forums/Monkey Beginners/List how does Find:Node<T> ( value:T ) works ??

GC-Martijn(Posted 2015) [#1]
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



Gerry Quinn(Posted 2015) [#2]
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!


GC-Martijn(Posted 2015) [#3]
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 ;)


Gerry Quinn(Posted 2015) [#4]
If you write:



then
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.


Gerry Quinn(Posted 2015) [#5]
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" )



Samah(Posted 2015) [#6]
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).

https://github.com/swoolcock/diddy
https://github.com/swoolcock/diddy/blob/master/src/diddy/diddystack.monkey
https://github.com/swoolcock/diddy/blob/master/src/diddy/containers.monkey


GC-Martijn(Posted 2015) [#7]
There was a small thing I needed to change.
Local nod:list.Node

@Samah
I'm trying to not using any extra modules, and to learn the basics ;)


Gerry Quinn(Posted 2015) [#8]
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!)