How to filter a map using Lambda?

人走茶凉 提交于 2019-12-13 06:05:32

问题


I have a map variable:

var bitmapDepths:Map<BitmapData, Int>;

What I need is to remove all keys with value of 0, I tried this:

bitmapDepths= Lambda.filter(Lambda.list(bitmapDepths.keys), function(v) { return (v > 0); });

So, I used Lambda.list to iterate on bitmapDepths.keys inside Lambda, but I get this error:

Void -> Iterator<flash.display.BitmapData> should be Iterable<Unknown<0>>

I tried Lambda.array to iterate on bitmapDepths.keys, I got the same error, so who can handle this? to remove keys based on values using Lambda?


回答1:


Don't use Lambda. It's a class that has been added in Haxe 1. In Haxe 3, for loops/comprehensions are almost always the better choice.

To remove the keys in place:

for (k in bitmapDepths.keys()) if (k == 0) bitmapDepths.remove(k);

To construct a new map:

bitmapDepths = [for (k in bitmapDepths.keys()) if (k != 0) k => bitmapDepths.get(k)];

Not only is it shorter, it also has better runtime performance.



来源:https://stackoverflow.com/questions/20299483/how-to-filter-a-map-using-lambda

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!