What is in your startup.jl?

My startup.jl has become a place for tools that are too simple to be behind an import, and probably too opinionated for Base

"""
    @subprocess ex
    @subprocess ex wait=false

Execute `ex` in a subprocess that mimics the current process configuration.
Returns the constructed `Process`.
See [`Base.julia_cmd`](@ref) for the subprocess configuration forwarding that is used.

```julia-repl
julia> println("hello from $(Base.getpid())")
hello from 35640

julia> @subprocess println("hello from $(Base.getpid())")
hello from 43056
Process(`/home/user/julia/julia -Cnative -J/home/user/julia/usr/lib/julia/sys.dylib -g1 -e 'println("hello from $(Base.getpid())")'`, ProcessExited(0))
```
"""
macro subprocess(ex, wait=true)
    quote
        local ex_str = $(esc(sprint(Base.show_unquoted,ex)))
        run(`$(Base.julia_cmd()) -e "$(ex_str)"`, wait = $(wait))
    end
end
# See: https://github.com/IanButterworth/julia/tree/ib/subprocess_macro
"""
    @repeat call
    @repeat n call
    @repeat expr call

Repeat `call` until interrupt, or `n` times, and discard the output.
If an expression is given that returns an Integer, repeat that many times.
If an expression is given that returns a Bool, repeat until false.

```julia
julia> @repeat println("hello")
hello
hello
^ChelloERROR: InterruptException:
...

julia> @repeat 3 println("hello")
hello
hello
hello

julia> @repeat 2 + 1 println("hello")
hello
hello
hello

julia> @repeat rand() > 0.5 println("hello")
hello
hello
```
"""
macro repeat(exs...)
    if length(exs) == 1
        quote
            while true
                $(esc(first(exs)))
            end
        end
    elseif length(exs) == 2
        terms = first(exs)
        ex = last(exs)
        if terms <: Integer
            quote
                for _ = 1:$(esc(terms))
                    $(esc(ex))
                end
            end
        elseif terms isa Expr
            quote
                local terms_eval = $(esc(terms))
                if terms_eval isa Bool
                    if terms_eval
                        $(esc(ex)) # do once given that terms has been evaled once already
                        while $(esc(terms))
                            $(esc(ex))
                        end
                    end
                elseif terms_eval isa Integer
                    for _ in 1:terms_eval
                        $(esc(ex))
                    end
                else
                    throw(ArgumentError("@repeat first argument must return an Integer or a Bool"))
                end
            end
        else
            throw(ArgumentError("@repeat first argument must be an Integer literal, or an expression that returns an Integer or Bool"))
        end
    else
        throw(ArgumentError("Too many arguments passed to @repeat"))
    end
end
# See: https://github.com/JuliaLang/julia/pull/41455
8 Likes