Giter VIP home page Giter VIP logo

Comments (4)

yminsky avatar yminsky commented on August 21, 2024

Map.update and Map.change do indeed seem to be in the same neighborhood. My first instinct is to think that this doesn't provide enough unique value to be worth adding. A good way of arguing the other side would be to provide some examples of code that would be made cleaner and nicer with this function, as opposed to change or update.

from base.

copy avatar copy commented on August 21, 2024

It's mostly a generalisation of incr (which exists in Hashtbl but not in Map) that let's you specify the combining operator. It's also than update because it only mentions the value-to-be-inserted once.

-      let amount = entry.amount in
-      M.update per_category ~key:c (function None -> amount | Some old -> old + amount)
+      M.update_with per_category ~key:c ~data:entry.amount ~f:(+)

I actually haven't found many use-cases besides insert_with ~f:(+) or insert_with ~f:List.append. Feel free to close if you think it doesn't provide enough unique value.

from base.

yminsky avatar yminsky commented on August 21, 2024

Yeah, my tendency is to not think it's worth adding a special function to Map for this.

FWIW, maybe another nice approach would be to define a function like:

val z f x opt =
   match opt with
   | None -> x
   | Some x' -> f x' x

And then you could do:

M.update per_category ~key:c (z (+) x)

But one would need a better name than z. But with an appropriate name, I could imagine adding such a thing to Option.

from base.

copy avatar copy commented on August 21, 2024

But one would need a better name than z. But with an appropriate name, I could imagine adding such a thing to Option.

Interesting. Looks like Option.fold:

M.update per_category c (Option.fold ~init:entry.amount ~f:(+))

I think this works for me. Cheers!

from base.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.