i'm trying find best way conditionally add element collection. let me try explain.
let:
val a="1234" val b="1arst" val c=""
(you can see i'm using colemak layout bet)
i want make map include element if length greater one.
i can go:
map("a" -> a, "b" ->b, "c"->c).filter(_._2.length>0)
which gives me desired map:
res7: scala.collection.immutable.map[java.lang.string,java.lang.string] = map(a -> 1234, b -> 1arst)
i can things like:
map( if(a.length>0) "a"->a else "" -> "" , if(b.length>0) "b"->b else "" -> "" , if(c.length>0) "c"->c else "" -> "" )
but gives me undesired empty string value in map still need filter:
res12: scala.collection.immutable.map[java.lang.string,java.lang.string] = map(a -> 1234, b -> 1arst, "" -> "")
so thinking of using right associative operators against nil doesn't work end empty list item in list.
scala> "test" -> "test" :: nil :: "test"->"test" :: nil res1: list[product serializable] = list((test,test), list(), (test,test))
is there more efficient way done without filtering on elements second time remove 0 length items?
Comments
Post a Comment