Add setBy#136
Conversation
|
Hey @vjrasane, Do you have another use case for this function aside from setting something by id? Ids go along well with other data types like |
|
The use cases all do revolve around some identifying value, of course. Quite often its an ID, but I've used it to set a user by their name, a bank account by its IBAN and a financial instrument by its ISIN. I think the key thing to recognize is that in some cases the order of items is important, so a dictionary would lose that information although it would make lookup by some comparable value easier. Same with Array. since the lookup is done by index. Another consideration is that you might need to set values based on several different values, so using a dictionary wouldn't help. In any case I do see your point, although personally I have found this function to be useful on several occasions. |
A utility function for cases that Ive ran into few times now and have had to write it every time so I figured I might as well create a pull request :)
Most often when I use this I need to replace a value in a list by ID or name, so I'd call this by saying:
setBy .id value listwhich is much nicer than:
setIf (\item -> item.id == value.id) value list(or the shorter
setIf (.id >> (==) value.id) value list)Let me know if there are any problems with this or if I've missed some utility that achieves the same.