dictionary - Scala filter by set -


say have map looks this

val map = map("shoes" -> 1, "heels" -> 2, "sneakers" -> 3, "dress" -> 4, "jeans" -> 5, "boyfriend jeans" -> 6) 

and have set or collection looks this:

val set = array(array("shoes", "heels", "sneakers"), array("dress", "maxi dress"), array("jeans", "boyfriend jeans", "destroyed jeans")) 

i perform filter operation on map 1 element in each of set retains. expected output should this:

map = map("shoes" -> 1, "dress" -> 4 ,"jeans" -> 5) 

the purpose of doing if have multiple sets indicate different categories of outfits, output map doesn't "repeat" on technically same objects.

any appreciated, thanks!

first rid of confusion sets arrays. rest of example use definition instead:

val arrays = array(array("shoes", "heels", "sneakers"), array("dress", "maxi dress"), array("jeans", "boyfriend jeans", "destroyed jeans")) 

so in sense have array of arrays of equivalent objects , want remove 1 of them?

well first have find of elements in array used keys in mep. filter out elements not used keys:

array.filter(map.keyset) 

now, have chose 1 element. said, take first one:

array.filter(map.keyset).head 

as "sets" arrays, first element in array used key. if use sets code still work sets have "first element". highly implementations specific , might not deterministic on various executions of same program. @ least immutable sets should deterministic on several calls head, i.e., should same element.

instead of first element interested in other elements, want remove them map:

array.filter(map.keyset).tail 

now, have remove map:

map -- array.filter(map.keyset).tail 

and arrays:

map -- arrays.flatmap(_.filter(map.keyset).tail) 

this works fine long arrays disjoined. if not, can not take initial map filter array in every step. instead, have use 1 array compute new map, take next starting result last , on. luckily, not have much:

arrays.foldleft(map){(m,a) => m -- a.filter(m.keyset).tail} 

note: sets functions elements boolean, is, why solution works.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -