ArrowMacros
A Julia package providing the macros @↓
, @↑
, @⤓
, @⤒
, and @←
.
Usage
using ArrowMacros
@↓
and @↑
provide ExtractMacro
-like features with UnPack
-like syntax and speed:
mutable struct A
a
b
end
s = A(1, -1)
@↓ a, b ← abs(b) + 1 = s
# (a, b) == (1, 2)
a += 1
@↑ s = a, b ← 2b - 1
# (s.a, s.b) == (2, 3)
@⤓
and @⤒
work like @↓
and @↑
, but they search in the tree structure of the struct
:
mutable struct B
c
d
end
s = A(1, B(2, [3, 4]))
@⤓ a, b ← c, c ← d[1] = s
# (a, b, c) == (1, 2, 3)
a += 1
@⤒ s = a, b ← 2b
# (s.a, s.b) == (2, 4)
@←
allows for a common syntax between in-place and standard functions:
f(b) = b
@← a = f(1) # a = f(1)
# a == 1
a = [0, 0]
g(a, b) = a .= b
@← a = g(1) # g(a, 1)
# a == [1, 1]
h!(a, b) = a .= b
@← a = h(2) # h!(a, 2)
# a == [2, 2]
Timings
using ExtractMacro
using UnPack
using BenchmarkTools
s = A(1, [2, 3])
@btime @↓ a, b = s
@btime @extract s : a b
@btime @unpack a, b = s
julia>
37.429 ns (0 allocations: 0 bytes)
60.720 ns (0 allocations: 0 bytes)
37.525 ns (0 allocations: 0 bytes)
Installation
ArrowMacros
is compatible with Julia v1.0
and above, and it can be installed by running
]add ArrowMacros
What's next
- Improve error messages.
- Allow for
@← a .= f(b...)