Skip to content

Instantly share code, notes, and snippets.

@adigitoleo
Last active December 14, 2022 09:14
Show Gist options
  • Save adigitoleo/8922d4076e4014a9e2c6e891f007af85 to your computer and use it in GitHub Desktop.
Save adigitoleo/8922d4076e4014a9e2c6e891f007af85 to your computer and use it in GitHub Desktop.
Miscellaneous stuff for Julia
using Pkg
using TerminalPager
using UnicodePlots
# Reduce color space of most REPL components from 16 to 8.
# Line numbers is stacktraces are still unreadable in dark themes...
Base.text_colors[:white] = Base.text_colors[:yellow]
Base.text_colors[:light_cyan] = Base.text_colors[:cyan]
Base.text_colors[:light_red] = Base.text_colors[:red]
Base.text_colors[:light_magenta] = Base.text_colors[:magenta]
Base.text_colors[:light_yellow] = Base.text_colors[:yellow]
Base.text_colors[:light_black] = Base.text_colors[:normal]
Base.text_colors[:light_blue] = Base.text_colors[:blue]
Base.text_colors[:light_green] = Base.text_colors[:green]
# Use truecolors for named colors in UnicodePlots
UnicodePlots.USE_LUT[]=true
"""List available named colors from Base.colors."""
function colornames()
for key in keys(Base.text_colors)
if isa(key, Symbol)
println(key)
end
end
end
"""List modules loaded into m with the `using` keyword."""
function modules(m::Module)
return ccall(:jl_module_usings, Any, (Any,), m)
end
"""Get a dictionary of dependencies of a package and their UUIDs."""
function dependencies(package::AbstractString)
if package == Pkg.project().name
return Pkg.project().dependencies
end
return Pkg.dependencies()[Pkg.project().dependencies[package]].dependencies
end
"""View documentation using `TerminalPager`."""
macro d(input) return :( pager(@doc $input) ) end
"""
maxrepeats(a)
Get the maximum number of times that an element of `a` is repeated in O(n) time.
"""
function maxrepeats(a)
result = 0
repeats = 1
for i = 1:length(a)-1
if a[i] == a[i + 1]
repeats += 1
else
if repeats > result
result = repeats
end
repeats = 1 # Reset repeats so we only need one loop.
end
end
return result + 1 # Count initial occurance as well.
end
"""
wrapdomain(x, upper)
wrapdomain(x, lower, upper)
Wrap `x` to the domain defined by the half-open interval [`lower`, `upper`).
Note that `wrapdomain(x, upper)` is equivalent to `wrapdomain(x, 0, upper)`.
"""
function wrapdomain(x, upper)
return (upper + x % upper) % upper
end
wrapdomain(x, lower, upper) = lower + wrapdomain(x - lower, upper - lower)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment