Hi guys, is there a nicer way of mutating single i...
# arrow
g
Hi guys, is there a nicer way of mutating single items in a list?
Copy code
fun ListK<Tuple2<String, Int>>.modifyListItem(key: String, amount: Int) = Option.fx {
        //Find the item
        val index = indexOfFirst { it.a == key }.let { if(it == -1) None else Some(it) }.bind()
        //Get the item
        val item = get(index)
        //Modify
        val modified = itemLens.set(item, item.b + amount)
        //Create an index
        val listIndex = ListK.index<Item>().index(index)
        //Replace the item
        listIndex.set(this@modifyListItem, modified)
}
j
Doesn't index have also modify method?
g
Ah true it does @jereksel, wasn't aware of that. Thanks Is having
ListK.index<Item>().index(...)
inline like that "correct"? It seems a bit long-winded