#2341 A question about find method in maps

rasa Fri 5 Sep 2014

Take a look at this method in Fantom Examples (file sys.maps.fan):

Void search()
{
  echo("\n--- search ---")
  x := [0:"zero", 1:"one", 2:"two", 3:"three"]
  show(x.find |v| { v[0] == 't' },     "two")
  show(x.find |v| { v[0] == 'x' },     "null")
  show(x.findAll |v| { v[0] == 't' },  "[2:two, 3:three]")
  show(x.findAll |v, k| { k.isEven },  "[0:zero, 2:two]")
  show(x.exclude |v, k| { k.isEven },  "[1:one, 3:three]")
}

If x.find returns a value, why then x.findAll returns a map instead of a list of values? For me, that would be the logic and preferred way. Or take it contrary, if x.findAll returns a map, why isn't so with x.find, i.e. why doesn't it return a map with one map entry but a single value? It looks for me like inconsistency in map design. What do you think about it?

brian Fri 5 Sep 2014

All the collection methods b/w List and Map parallel each other. So in both cases find returns one value, and findAll returns a new collection of the source type. If you want a list then just pipe to a list:

map.list.findAll |x| { ... }

rasa Fri 5 Sep 2014

I've just tried it in fansh and it doesn't work.

This works:

x := [0:"zero", 1:"one", 2:"two", 3:"three"]
x.findAll {it[0] == 't'} // result: [2:two, 3:three]

and this doesn't work:

x := [0:"zero", 1:"one", 2:"two", 3:"three"]
x.list.findAll {it[0] == 't'} // result: ERROR(7): Unknown slot 'sys::Map.list'

Besides, I've already just checked docs for the sys::Map and got a little confused with the description of the addList slot. It says:

Add the specified list to this map where the values are the list items and the keys are derived by calling the specified function on each item.

and in the attached example we have:

m := [0:"0"]
m.addList(["1","2"]) |Str s->Int| { return s.toInt }
m  =>  [0:0, 1:1, 2:2]

According to slot description, the right result should be m => [0:"0", 1:"1", 2:"2"] but it's not? Whether it is a good description of this slot?

brian Fri 5 Sep 2014

Sorry, its vals, not list

rasa Fri 5 Sep 2014

OK, that works. Thanks. I've just updated my previous post, so take a look.

rasa Sun 7 Sep 2014

Besides, I've already just checked docs for the sys::Map and got a little confused with the description of the addList slot. It says:

Add the specified list to this map where the values are the list items and the keys are derived by calling the specified function on each item.

and in the attached example we have:

m := [0:"0"]
m.addList(["1","2"]) |Str s->Int| { return s.toInt }
m  =>  [0:0, 1:1, 2:2]

According to slot description, the right result should be m => [0:"0", 1:"1", 2:"2"] but it is [0:0, 1:1, 2:2]? Whether it is a wrong description of this slot or its implementation?

brian Sun 7 Sep 2014

Whether it is a wrong description of this slot or its implementation?

Those docs were copied from fansh which didn't include the quotes to illustrate the values are strings. I fixed the documentation so its clearer

rasa Sun 7 Sep 2014

You should update docs at this place also.

Login or Signup to reply.