-
Notifications
You must be signed in to change notification settings - Fork 0
/
currr.R
89 lines (77 loc) · 2.57 KB
/
currr.R
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
#' Create a curryable verb.
#'
#' currify_verb(foo) creates a function of dots (...).
#' Passing arguments to this function in turn creates a new function foo(.data, ...)
#' @param verb A function that takes .data and args (...)
#' @examples
#' filtering = currify_verb(dplyr::filter)
#' @export
currify_verb <- function(verb) {
function(...) {
intention <- function(.data) {
return(purrr::partial(verb)(.data, ...))
}
memoise::memoise(intention)
}
}
#' Function composition operator.
#'
#' `g %.% f` creates a function equivalent to g(f(x)) for some input `x`.
#' Meant to emulate infix notation for function composition, $f . g$.
#' @param g A function
#' @param f A function
#' @examples
#' (length %.% unique)(c(1, 1, 2, 2, 3))
#' @export
`%.%` <- function(g, f) function(...) g(f(...))
#' Postfix function composition operator.
#'
#' `f %;% g` creates a function equivalent to g(f(x)) (or `x |> f() |> g()`) for some input `x`.
#' Unlike conventional function composition, which evaluates right to left, postfix notation operates left to right.
#' This is similar to the direction of the pipe operator `|>`, except the postfix operator creates a function (without evaluating it).
#' @param f A function
#' @param g A function
#' @examples
#' (unique %;% length)(c(1, 1, 2, 2, 3))
#' @export
`%;%` <- function(f, g) g %.% f
#' Curryable dplyr::select.
#'
#' Pass all arguments except .data to create a new function of .data.
#' @export
selecting <- currify_verb(dplyr::select)
#' Curryable dplyr::filter
#'
#' Pass all arguments except .data to create a new function of .data.
#' @export
filtering <- currify_verb(dplyr::filter)
#' Curryable dplyr::group_by
#'
#' Pass all arguments except .data to create a new function of .data.
#' @export
grouping <- currify_verb(dplyr::group_by)
#' Curryable dplyr::ungroup
#'
#' Pass all arguments except .data to create a new function of .data.
#' @export
ungrouping <- currify_verb(dplyr::ungroup)
#' Curryable dplyr::mutate
#'
#' Pass all arguments except .data to create a new function of .data.
#' @export
mutating <- currify_verb(dplyr::mutate)
#' Curryable dplyr::summarize
#'
#' Pass all arguments except .data to create a new function of .data.
#' @export
summarizing <- currify_verb(dplyr::summarize)
#' Curryable dplyr::arrange
#'
#' Pass all arguments except .data to create a new function of .data.
#' @export
arranging <- currify_verb(dplyr::arrange)
#' Curryable dplyr::rename
#'
#' Pass all arguments except .data to create a new function of .data.
#' @export
renaming <- currify_verb(dplyr::rename)