Comments (4)
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.
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.
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.
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)
- Unable to build v0.15~preview.125.13+424 HOT 4
- OCaml 5.00 support HOT 1
- int_math_stub.c should use BitScanReverse for clz (and BitScanForward for ctz) HOT 1
- Core.List.exists doesn't work as expected HOT 1
- Support for non-empty lists HOT 4
- Difference between `base`, and `core_kernel`? HOT 2
- Cannot build on system with only ocaml and dune installed. HOT 1
- Please don't auto-register printers for exceptions defined outside of Base HOT 2
- Map.update changes the "physical" key in certain circumstances HOT 1
- What is the policy regarding support for older OCaml versions ? HOT 3
- Base v0.16.0 build failure on windows HOT 2
- make List.take return physical identity for n = length HOT 1
- Linker error on macOS with base 0.16.1 HOT 3
- v0.16.2 is broken with MSVC on Windows HOT 3
- Use `write-file` instead of `bash` in dune action HOT 1
- No docs HOT 2
- Add String.to_sequence (the standard library has String.to_seq)
- Allow for libraries depending on base to enable dune's (implicit_transitive_deps false) HOT 2
- Segmentation fault with `String.filter_map` HOT 3
- Build failure with clang: unsupported `-mpopcnt` option HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from base.