-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.en.Rmd
More file actions
534 lines (404 loc) · 15.4 KB
/
Copy pathindex.en.Rmd
File metadata and controls
534 lines (404 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
---
title: Argument Matching Across Languages
author: Jonathan Carroll
date: '2023-08-06'
categories:
- haskell
- julia
- rstats
- rust
- typescript
tags:
- haskell
- julia
- rstats
- rust
- typescript
slug: argument-matching-across-languages
editor_options:
chunk_output_type: console
---
```{r, setup, include = FALSE}
knitr::opts_chunk$set(
class.output = "bg-success",
class.message = "bg-info text-info",
class.warning = "bg-warning text-warning",
class.error = "bg-danger text-danger"
)
```
With Functional Programming, we write functions which take arguments and do something with
or based on those arguments. You might not think there's much to learn about given that
tiny description of "an argument to a function" but the syntax and mechanics of different
languages is actually widely variable and intricate.
<!--more-->
With Functional Programming, we write functions which take arguments and do something with
or based on those arguments. You might not think there's much to learn about given that
tiny description of "an argument to a function" but the syntax and mechanics of different
languages is actually widely variable and intricate.
Let's say I have some function in R that takes three arguments, `x`, `y`, and `z`,
and just prints them out in a string in that order.
```{r}
r_fun <- function(x, y, z) {
sprintf("arguments are: %s, %s, %s", x, y, z)
}
```
Calling this function with good practices (specifying all the argument names in full)
would look like this
```{r}
r_fun(x = "a", y = "b", z = "c")
```
I said "in full" because by default, R will happily do *partial* matching, so long
as it can uniquely figure out which argument you mean
```{r}
long_args <- function(alphabet = "a to z", altitude = 100) {
print(sprintf("alphabet: %s", alphabet))
print(sprintf("altitude: %d", altitude))
}
long_args(alphabet = "[A-Z]", altitude = 50)
```
In this case, both arguments start with `"al"` so it's ambiguous up to there
```{r, error = TRUE}
long_args(al = "letters")
```
but we only *need* to specify enough letters to disambiguate
```{r}
long_args(alpha = "LETTERS", alt = 200)
```
Relying on this behaviour is dangerous, and it's recommended to turn on warnings
when this happens with
```{r, warning = TRUE}
options(warnPartialMatchArgs = TRUE)
long_args(alpha = "LETTERS", alt = 200)
```
```{r, echo = FALSE}
options(warnPartialMatchArgs = FALSE)
```
You don't _have to_ use argument names when calling the function, though - you can just rely on positional arguments
```{r}
r_fun("a", "b", "c")
```
and this is *very* commonly done, despite it being less clear to what any of those
refer, and runs the risk that the function changes argument ordering in an updated
version. It works, though.
Extensive sidenote: square-bracket matrix subsetting officially uses the (poorly? traditionally?)
named arguments `i` and `j` as `[i, j]` but it actually entirely ignores them and uses
positional arguments. The documentation (`` ?`[` ``) does warn about this
> "Note that these operations do not match their index arguments in the standard way:
argument names are ignored and positional matching only is used. So m[j = 2, i = 1] is
equivalent to m[2, 1] and not to m[1, 2]."
but it would be very easy to get bitten by it if one tried to use the names directly
```{r}
m <- matrix(1:9, 3, 3, byrow = TRUE)
m
m[i = 1, j = 2]
m[j = 2, i = 1]
```
[Thomas Lumley](https://fediscience.org/@tslumley/110670564210497040)
notes that
> "it used to be that no primitive functions did argument matching by name. "/" and "-'
and switch() and some others still don't. I'm not sure why "[" wasn't changed in 2.11
when a bunch of primitives got normal argument matching."
Worse still, perhaps - the `seq()` function creates a sequence of values. It has the
formal arguments with defaults `from = 1` and `to = 1` so you can calculate
```{r}
seq(from = 2, to = 5)
```
or you can leverage the default of `from = 1`
```{r}
seq(to = 5)
```
However, there are **five** "forms" in which
you can provide arguments to this function and they behave differently. If you _only_
specify the first argument unnamed, it treats this as `to` despite the first argument being `from`
```{r}
seq(5)
```
which is extra strange, because if you _do_ specify `to` with its ostensibly default value `1`, the sequence is backwards
```{r}
seq(5, to = 1)
```
Back to our function - a feature that makes R really neat is that you can specify
the named arguments in *any order*
```{r}
r_fun(z = "c", x = "a", y = "b")
```
If you don't specify them by name, R will default to positions, so specifying just
one (e.g. `z`) but leaving the rest unspecified, R will presume you want the others
in positional order
```{r}
r_fun(z = "c", "a", "b")
```
Where it gets really interesting is you can go *back* to named arguments further along
and again, R will figure out that you mean the remaining unnamed argument
```{r}
r_fun(z = "c", "b", x = "a")
```
This only holds if the function _doesn't_ use the ellipses `...` which
captures "any other arguments" when calling the function, often to be passed
on to another function. If the function signature has `...` then all the
unnamed arguments are captured. This example function just
combines any other arguments into a comma-separated string, if there
are any (tested with the under-documented `...length()` which returns the number
of arguments captured via `...`)
```{r}
dot_f <- function(a = 1, b = 2, ...) {
print(sprintf("named arguments: %s, %s", a, b))
if (...length()) {
print(sprintf("additional arguments: %s", toString(list(...))))
}
}
```
You can call this with just the named arguments
```{r}
dot_f(a = 3, b = 4)
```
or you can add more argument (no name required)
```{r}
dot_f(a = 3, b = 4, 5)
```
As before, none of the names are really required, and we can add as
many as we want
```{r}
dot_f(3, 4, 5, 6, 7)
```
We _can_ name them if we want
```{r}
dot_f(a = 3, b = 4, blah = 5)
```
but here be danger, because those names can be _anything_ and aren't matched
to the actual function, so this works (say, I misspelled an argument name `a` as `A`)
```{r}
dot_f(A = 3, B = 4, 5)
```
Notice that the additional arguments are the ones I named (not those in
the function definition); the `5` has been positionally matched to `a`; and `b`
has taken its default value of `2` because no other arguments were provided.
We can still mix up the ordering of positions, provided everything else matches up
```{r}
dot_f(3, b = 4, 5)
dot_f(3, b = 4, 5, a = 2)
```
The flexibility in all of this is what encouraged Joe Cheng to use R as an
interface to HTML in the form of shiny, what he calls
["a bizzarely good host language"](https://youtu.be/HpqLXB_TnpI?t=1723) (should link
to the right timestamp) and he notes that other languages don't let you do
this sort of mixing up of named and positional arguments.
Okay, that's R - weird and fun, but a lot of flexibility.
I saw [this post](https://thoughtbot.com/blog/rust-doesn-t-have-named-arguments-so-what) mentioned in the `#rust` hashtag on Mastodon and had a look - it surprised me
at first because I thought "what do you mean Rust doesn't have named arguments?"...
I've become so used to the inline help from VSCode when I'm writing Rust that I
didn't realise I wasn't using named arguments.
Here's a function I wrote for my [toy rock-paper-scissors game in Rust](https://github.com/jonocarroll/rps.rs)
```rust
fn play(a: Throw, b: Throw) -> GameResult {
let result = match a.cmp(&b) {
Ordering::Equal => GameResult::Tie,
Ordering::Greater => GameResult::YouWin,
Ordering::Less => GameResult::YouLose,
};
println!("{} {}", "Result:".purple().bold(), result);
result
}
```
It has arguments `a` and `b` because I did a terrible job naming them - I knew
exactly how I planned to use them, so bad luck to anyone else.
Calling that function further down in the code I have
```rust
let user = val.user();
let computer = Throw::computer();
play(user, computer);
```
BUT what I see in the editor has the argument names, unless I switch off
hints (which I have bound to holding Ctrl+Alt at the moment)

So, I can't just rearrange arguments in Rust?
If I define a function with two arguments
```rust
>> fn two_args(a: f64, b: &str) -> String {
let res = format!("all arguments: {a}, {b}");
res
}
```
then I can call it
```rust
>> two_args(42.0, "forty-two")
"all arguments: 42, forty-two"
```
Just swapping the arguments obviously fails because `42.0` isn't a `&str` and
`"forty-two"` isn't a `f64`. But there isn't a way to say "the value for that
argument is this"; I can't use any of these
```rust
two_args(a = 42.0, b = "forty-two")
two_args(a: 42.0, b: "forty-two")
two_args(b = "forty-two", a = 42.0)
two_args(b: "forty-two", a: 42.0)
```
I suspect the fact that this was a surprise to me means I'm earlier in my Rust
learning than I had thought - I clearly haven't built anything that has
functionality I didn't directly need, because I haven't had to worry about
calling functions in strange ways yet.
There is one loophole... time to break out another cool toy: [{rextendr}](https://github.com/extendr/rextendr)
```{r, eval = TRUE}
library(rextendr)
rust_function(
'fn two_args(a: f64, b: &str) -> String {
let res = format!("all arguments: {a}, {b}");
res
}'
)
```
This produces an R function that takes two arguments, `a` and `b` which I can call
as if it was an R function
```{r}
two_args(a = 42, b = "forty-two")
```
I can call it without argument names
```{r}
two_args(42, "forty-two")
```
and I *can* swap them
```{r}
two_args(b = "forty-two", a = 42)
```
This is just because the argument matching happens before the values get
sent down to the Rust code - the function here is an R function that calls
other code internally
```{r}
two_args
```
I somewhat started out the idea for this blogpost as I was learning some Typescript and
came across this [https://github.com/gibbok/typescript-book#typescript-fundamental-comparison-rules](https://github.com/gibbok/typescript-book#typescript-fundamental-comparison-rules)
_"Function parameters are compared by types, not by their names:"_
```typescript
type X = (a: number) => void;
type Y = (a: number) => void;
let x: X = (j: number) => undefined;
let y: Y = (k: number) => undefined;
y = x; // Valid
x = y; // Valid
```
which initially struck me as strange, and I needed to work through some examples in a live
setting. On reflection, I think I see that this is exactly what I would specify in
e.g. [Haskell](http://learnyouahaskell.com/types-and-typeclasses) - "a function that takes a number", not "a function with an argument named `a` which
is a number"
```haskell
x :: Float -> Nothing
```
Because technically all functions in Haskell [actually only take a single argument](https://wiki.haskell.org/Currying) (the notation `Int -> Int -> Int` reveals this
fact nicely, but in practice the notation makes it feel like multiple arguments can be used)
there is no way to "pass arguments by name" but there _is_ a neat way to swap the order
of arguments that a function expects to receive; [`flip`](https://hackage.haskell.org/package/base-4.18.0.0/docs/Prelude.html#v:flip)
```haskell
flip :: (a -> b -> c) -> b -> a -> c
>>> flip (++) "hello" "world"
"worldhello"
-- or
>>> "hello" ++ "world"
"helloworld
```
Those of you familiar with R's S3 dispatch functionality will perhaps note that
the 'first' argument has a special role; it controls exactly which method will
be called. If we had some function which was flexible in the sense that it could
take several different 'classes' and do something different with them, we would
write that as
```{r}
flexi <- function(a, b) {
UseMethod("flexi")
}
flexi.matrix <- function(a, b) {
paste0("a is a matrix, b = ", b)
}
flexi.data.frame <- function(a, b) {
paste0("a is a data.frame, b = ", b)
}
flexi.default <- function(a, b) {
paste0("a is something else, b = ", b)
}
```
Now, depending on whether `a` is a `matrix`, a `data.frame`, or something else, one
of the 'methods' will be called
```{r}
flexi(a = matrix(), b = 7)
flexi(a = data.frame(), b = 8)
flexi(a = 1, b = 9)
```
even if we swap the order of the arguments in the call
```{r}
flexi(b = 3, a = matrix())
```
S4 dispatch goes even further and dispatches based on more than just the class of
the first argument. Stuart Lee has [a great guide on S4](https://stuartlee.org/2019/07/09/s4-short-guide/). The point is, you can do something
different depending on what you pass to multiple arguments
```{r, eval = FALSE}
s4flexi(matrix(), data.frame(), 7)
s4flexi(matrix(), data.frame(), list())
s4flexi(matrix(), data.frame(), NULL)
```
Julia has some of the most interesting argument parsing. I love the Haskell-like
function declarations - so little boilerplate! We define some function `f` that
takes two arguments
```{julia}
f(a, b) = a + b
f(4, 5)
```
Similar to the Rust situation, though - these aren't named outside of the function body,
so we can't refer to them either in that order or reversed
```{julia, eval = FALSE}
f(a = 4, b = 5)
```
```{julia, eval = FALSE, class.source = "bg-danger text-danger"}
MethodError: no method matching f(; a=4, b=5)
Closest candidates are:
f(!Matched::Any, !Matched::Any) at none:3 got unsupported keyword arguments "a", "b"
```
The reason is that Julia uses the python-esque keyword argument syntax, where unnamed
arguments appear first, followed by any keyword arguments following a `;`, so we can specify
these correctly as
```{julia}
f(; a, b) = a + b
f(a = 4, b = 6)
```
Julia is optionally typed, which means we can be flippant with the types here, or we
can be very specific - we can specify that `a` should be an integer and `b` should be
a string, and that produces a _different_ method compared to what we already defined. In
this case, I want to return a string with the two values
```{julia}
f(; a::Int, b::String) = "$a; $b"
f(a = 42, b = "life, universe, everything")
```
Since these are now named, we can swap them
```{julia}
f(b = "L, U, E", a = 42)
```
but what's even more powerful is we can define a general method, and add type-specific methods
for whatever combination of argument types we want; the first of these returns an integer,
while the other two return strings
```{julia}
g(a, b) = a + b
g(a::Int, b::String) = "unnamed int, string: $a; $b"
g(a::String, b::Int) = "unnamed string, int: $a; $b"
```
Then, depending on what types we provide in each argument, a different method is called
```{julia}
g(3, 2)
g("abc", 123)
g(123, "abc")
```
Similar to S4, but so easy to declare and use! Of course, this doesn't work if we want these
to be named since that would be ambiguous.
As I'm slowly learning APL, I've found it interesting that there's a well-known approach of
writing ["point-free" ("tacit")](https://en.wikipedia.org/wiki/Tacit_programming) functions which don't specify arguments at all.
Last of all, I've had the pleasure of dealing with C this week including passing a _pointer_
to some object into a function, in which case the value _outside of the function_ is updated.
That's a whole other post I'm working on.
How does your favourite language use arguments? Let me know! I can be found on [Mastodon](https://fosstodon.org/@jonocarroll) or use the comments below.
<br />
<details>
<summary>
<tt>devtools::session_info()</tt>
</summary>
```{r sessionInfo, echo = FALSE}
devtools::session_info()
```
</details>
<br />