Skip to content

A terse syntax of function declaration in R. 🔫

Notifications You must be signed in to change notification settings

Marlin-Na/terser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

terser

A natural and terse syntax of function declaration in R for people who are interested in shooting yourself in the foot 🔫.

You can use f(x, y) = x + y to define a function f.

f(x, y) = x + y
f(3, 4)
#> [1] 7

How... ?

Yes! I overwrite the = operator.

If the left hand side is a symbol, it just does normal assignment like the <- operator. If the left hand side is a function call, it defines a function.

Some more examples:

x = 3
x
#> [1] 3

f(x, y) = x + y
f(2, 3)
#> [1] 5
identical(f, function(x, y) x + y)
#> [1] TRUE

g(x = 231) = log(x)
g()
#> [1] 5.442418
identical(g, function(x = 231) log(x))
#> [1] TRUE

h(a, b = a^2) = a + b
h(1)
#> [1] 2
h(1, 2)
#> [1] 3
identical(h, function(a, b = a^2) a + b)
#> [1] TRUE

tan2(a) = sin(a)/cos(a)
tan2(pi)
#> [1] -1.224647e-16

`∑`(...) = sum(...)
`∑`(2, 3, 4)
#> [1] 9

Warning

The operator = can no longer do assignment for attributes and elements, the following will no longer work. Use <- instead.

names(x) = c("a", "b", "c")
x[[2]] = 32

See also

This package is inspired by a syntax for defining a function in Julia.

julia> f(x, y) = x + y
f (generic function with 1 method)

About

A terse syntax of function declaration in R. 🔫

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages