vert-x / mod-lang-scala

Vert.x 2.x is deprecated - use instead
https://github.com/vert-x3/vertx-lang-scala
Apache License 2.0
77 stars 35 forks source link

JsonObject and JsonArray should have (Scala) monad and higher-order functions #170

Open christian-bick opened 10 years ago

christian-bick commented 10 years ago

JsonObject and especially JsonArray would be good candidates to enrich with monad (map, flatMap, filter) and higer-order functions (forall, etc.). As they are Java types at the moment, it needs some nasty conversions into equivalent Scala collections to get there.

Looping over a JsonArray looks like this at the moment:

val items: JsonArray = someFunction()
for (index <- 0 until items.size()) yield items.get[JsonObject](index).get("someKey")

But what i would love to see for JsonArray is:

val items: JsonArray[JsonObject] = someFunction()
// for expression
for (item <- items) yield item.get[Integer]("someKey")
// map
items.map(item => item.get[Integer]("someKey"))
// filter
items.filter(item => item.get[Integer]("someKey") > 5)

The same makes sense for JsonObject, even though JsonArray is the more natural candidate here.

galderz commented 9 years ago

Yeah, very valid points. We didn't get around to doing it for 1.x. We hope to improve on it for 2.0.