Lazy.takeuntil
— Methodtakeuntil(pred, list)
Take the elements in list
until the pred
function return true. Notice that the one which makes pred
true is also taken. All elements will be taken if no one satisfy the pred
function.
Lazy.@>
— MacroThe threading macro is like a more flexible version of the |>
operator.
@> x f = f(x)
@> x g f == f(g(x))
@> x a b c d e == e(d(c(b(a(x)))))
Unlike |>
, functions can have arguments - the value preceding a function will be treated as its first argument
@> x g(y, z) f == f(g(x, y, z))
@> x g f(y, z) == f(g(x), y, z)
Lazy.@>>
— MacroLazy.@as
— Macro@as as, exs...
@as
lets you name the threaded argmument
@as _ x f(_, y) g(z, _) == g(z, f(x, y))
All threading macros work over begin blocks
6 === @as x 2 begin
x^2
x+2
end
Lazy.@bounce
— MacroTail recursion that doesn't blow the stack.
@bounce even(n) = n == 0 ? true : odd(n-1)
@bounce odd(n) = n == 0 ? false : even(n-1)
even(1_000_000) # Blows up without `@bounce`.
#> true
For simple cases you probably want the much faster @rec
.
Lazy.@d
— MacroCreates an untyped dictionary, e.g.
julia> @d(:a=>1, :b=>2)
Dict{Any,Any} with 2 entries:
:a => 1
:b => 2
Lazy.@defonce
— MacroStop Julia from complaining about redifined struct/consts –
@defonce struct MyType
...
end
or
@defonce const pi = 3.14
Lazy.@dotimes
— Macro@dottimes(n, body)
Repeat body
n
times.
Lazy.@forward
— Macro@forward T.x functions...
Define methods for functions
on type T
, which call the relevant function on the field x
.
Example
struct Wrapper
x
end
@forward Wrapper.x Base.sqrt # now sqrt(Wrapper(4.0)) == 2.0
@forward Wrapper.x Base.length, Base.getindex, Base.iterate # several forwarded functions are put in a tuple
@forward Wrapper.x (Base.length, Base.getindex, Base.iterate) # equivalent to above
Lazy.@oncethen
— MacroA do
-while
loop – executes the while
loop once regardless of the condition, then tests the condition before subsequent iterations.
Lazy.@rec
— MacroEnables efficient recursive functions, e.g.
@rec reduce(f::Function, v, xs::List) =
isempty(xs) ? v : reduce(f, f(v, first(xs)), tail(xs))
Without @rec
this function would overflow the stack for lists of 80,000 or more elements.
Caveats:
- No support for trampolining, i.e. only calls to the given function are optimised away.
- Ignores multiple dispatch – it is assumed that the function's name always refers to the given definition.
- Don't rebind the function's name in a let (see above).
- Don't use this with varargs functions.
Use the more flexible, but slower, @bounce
to avoid these issues.
Lazy.@switch
— MacroA switch statement of sorts:
@switch x begin
1; "x equals one!"
2; "x equals two!"
"x equals something else!"
end
However, it's a bit more general than a regular switch in that you can test more than just equality:
@switch isa(x, _) begin
Integer; "x is an integer!"
FloatingPoint; "x is a float!"
"x is something else!"
end
@switch _ begin
a > b; "more than!"
a < b; "less than!"
a == b; "equal!" # Note that this level of enthusiasm is not mandatory.
end
Where _
is replaced by the value for testing in each case. The final expression, if there is one, is used as the default value; if there is no default and nothing matches an error will be thrown.
Lazy.@with
— MacroEnd-less let
block, e.g.
@with (x = 1, y = 2),
x+y
Lazy.tupleassign
— MethodGenerate an expression like (a, b) = (c, d)
.