3 ms·
Using the addition operator for manipulation is a bit unusual. Why did you choose it? I don't have R studio or anything on my computer, so I can't test it, but
by tgv 2y ago
Using the addition operator for manipulation is a bit unusual. Why did you choose it? I don't have R studio or anything on my computer, so I can't test it, but do things like
music <- Music() + Meter(4, 4) + Line(...) + (Line(...) + Articulation(">", 1))
work?
- nomonnai 2y agoIn base R, `+` is the usual arithmetic operator for numeric and complex vectors. I'm not a super R expert but I'm pretty sure the addition operator is overloaded here. It simply means: add(Music(), Meter(4, 4), Line(...), Line(...), Articulation(">", 1)) Edit. Found the definition in the source [1]. I think the approach is similar to Go's approach to method definition [2]. Methods are defined on types, i.e., methods are functions with special receiver arguments. The infix notation instead of prefix notation is just syntactic sugar. [1] https://github.com/flujoo/gm/blob/69d639be86b0cef80d815a73a605ad7f9f5e0ce2/R/2-add-music.R#L27 https://github.com/flujoo/gm/blob/69d639be86b0cef80d815a73a6... [2] https://go.dev/tour/methods/1 https://go.dev/tour/methods/1
- deleted 2y ago[deleted]
- andylynch 2y agoThis is quite idiomatic for R; packages like ggplot do the same thing to, for instance, build up graphics- see https://r4ds.hadley.nz/data-visualize https://r4ds.hadley.nz/data-visualize for some nice examples
- erikgahner 2y agoggplot2 is pretty much an exception at this point, and if the package was created today it would use the pipe instead of add [1]. Most packages use pipes now (especially with the introduction of the pipe operator in R 4.1.0). [1] https://forum.posit.co/t/why-cant-ggplot2-use/4372/7 https://forum.posit.co/t/why-cant-ggplot2-use/4372/7
- deleted 2y ago[deleted]
- dr_kiszonka 2y agoI am no R expert, but pipes don't seem the most intuitive for ggplot. This is because they would imply that, e.g., ggtitle() %>% geom_point() means that ggtitle() outputs an input for geom_point().
- hadley 2y agoThat’s exactly how ggplot (not 2!) worked: https://github.com/hadley/ggplot1 https://github.com/hadley/ggplot1
- hackoo 2y agoIn creating a music score, you literally "add" components to the score. So it's natural to implement this operation this way. From the OOP perspective, it's just method overloading. Not uncommon in R development.
- tetris11 2y agoR is a Lisp dialect in disguise.