Hello, I’m updating to latest version of arrow, I ...
# arrow
i
Hello, I’m updating to latest version of arrow, I have a problem with one method. What’s the equivalent of this method using latest arrow version, thanks in advance.
Copy code
/**
 * Given a path as string dot-separated component, extracts the item found at that path location.
 * Eg.:
 * Given m = mapOf("p1" to mapOf("p2" to "value_1_2"))
 * Then m.readPath("p1.p2") -> Option["value_1_2"]
 */
inline fun <reified T> Map<String, Any>.readPath(pathComponents: String): Option<T> {
    val extract = { v: Any, p: String -> (v as? Map<*, *>)?.get(p).toOption() }
    return pathComponents.split(".").foldM(Option.monad(), this, extract).fix().flatMap { (it as? T).toOption() }
}
s
This would be a direct translation of
foldM
.
Copy code
/**
 * Given a path as string dot-separated component, extracts the item found at that path location.
 * Eg.:
 * Given m = mapOf("p1" to mapOf("p2" to "value_1_2"))
 * Then m.readPath("p1.p2") -> Option["value_1_2"]
 */
inline fun <reified T> Map<String, Any>.readPath(pathComponents: String): Option<T> {
  val extract = { v: Any, p: String -> (v as? Map<*, *>)?.get(p).toOption() }
  return pathComponents.split(".").fold(Option(this)) { acc: Option<Any>, s: String ->
    acc.flatMap { extract(it, s) }
  }.flatMap { (it as? T).toOption() }
}
🙏 1