https://github.com/tkf/atbackslash.jl
https://github.com/tkf/atbackslash.jl
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/tkf/atbackslash.jl
- Owner: tkf
- License: mit
- Created: 2019-11-22T23:49:18.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-16T21:14:01.000Z (almost 6 years ago)
- Last Synced: 2024-10-19T17:47:32.671Z (about 1 year ago)
- Language: Julia
- Size: 12.7 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AtBackslash
[](https://travis-ci.com/tkf/AtBackslash.jl)
[](https://codecov.io/gh/tkf/AtBackslash.jl)
[](https://coveralls.io/github/tkf/AtBackslash.jl?branch=master)
`AtBackslash` exports a macro `@\` to easily create functions that
work with named tuples as input and/or output.
The symbol literal like `:x` in the tuple argument is expanded to be
the property/field of the named tuple of the input and output:
```julia
julia> using AtBackslash
julia> (x = 1, y = 2, z = 3) |> @\(:x, :y)
(x = 1, y = 2)
```
It also supports normal "verbose" syntax for creating a named tuple:
```julia
julia> (x = 1, y = 2) |> @\(x = :x, y = :y)
(x = 1, y = 2)
```
which is handy when adding new properties:
```julia
julia> (x = 1, y = 2) |> @\(:x, z = :x + :y)
(x = 1, z = 3)
```
The argument can be explicitly referred to by `_`:
```julia
julia> (x = 1, y = 2) |> @\(_..., z = :x + :y)
(x = 1, y = 2, z = 3)
```
```julia
julia> (x = 1, y = 2) |> @\_.x
1
```
```julia
julia> 1 |> @\(x = _, y = 2_)
(x = 1, y = 2)
```
Automatic conversions of `:x` and `(; :x, :y)` work at any level of
expression:
```julia
julia> (x = 1, y = 2) |> @\ merge((; :x, :y), (a = :x, b = :y))
(x = 1, y = 2, a = 1, b = 2)
```
```julia
julia> (x = 1, y = 2) |> @\(:x < :y < 3)
true
```
Use `$:x` to avoid automatic conversion to `_.x`:
```julia
julia> (x = 1, y = 2) |> @\(x = $:x, :y)
(x = :x, y = 2)
```
Use plain names to refer to the variables in the outer scope:
```julia
julia> let z = 3
(x = 1, y = 2) |> @\(:x, :y, z)
end
(x = 1, y = 2, z = 3)
```
The input can be any object that support `getproperty`. For example,
it works with `Complex`:
```julia
julia> 1 + 2im |> @\(:re, :im)
(re = 1, im = 2)
```