ds: absurdly necessary data structures and algorithms
ds is a small-ish lua library which fills many of the data structure and method
gaps (needed "batteries") in Lua's standard library. It's only dependency is
lib/metaty which it uses for defining it's records and
lib/fmt which it
uses to define the logging interface.
none: "set but none" vs nil's simply "unset"
In Lua
nil always means "unset". Certain APIs (like JSON) might distinguish
between unset vs null/empty/none. For such APIs
none can be used to mean "set
as none" instead of simply "unset" (which is what
nil means).
none overrides
__metatable='none' so that
getmetatable(none)=='none' and
metaty.ty(none) == 'none'.
WARNING: assert(none) will pass. Use ds.bool to make none falsy.
path
ds.path has some functions for working with paths.
It interacts (but does not set) the
globals CWD and
HOME to get the
"current working directory" and "home directory", respectively.
ds: data structures and algorithms.
Types: B PlainStyler TypoSafe Slc Imm Duration Epoch Set bt BiMap Deq TWriter Error
Functions
- fn setup(args)
Default LUA_SETUP, though vt100 is recommended for most users.
- fn concat(sep, ...) -> string
concatenate the string arguments.
- fn push(t, v) -> index
push the value onto the end of the table, return the index.
- fn name(t) -> string
if t is a table returns t.__name or '?'
- fn inset(t, i, values, rmlen) -> nil
insert values into list at index i.
Uses inset method if available.
rmlen, if provided, will cause t[i:i+rmlen
to be removed first
inset is like an extend but the items are insert at any place in the array.
The rmlen will also remove a certain number of items.
*
fn isPod
*
fn noop()
*
fn nosupport()
*
fn iden(...) -> ...
*
fn retTrue() -> true
*
fn retFalse() -> false
*
fn newTable() -> {}
*
fn eq(a, b) -> a == b
*
fn srcloc(level) -> "/path/to/dir/file.lua:10"
*
fn shortloc(level) -> "dir/file.lua:10"
*
fn srcdir(level) -> "/path/to/dir/"
*
fn coroutineErrorMessage(cor, err) -> string
*
fn isWithin(v, min, max) -> bool
*
fn lt(a, b) -> a < b
*
fn gt(a, b) -> a > b
*
fn lte(a, b) -> a <= b
*
fn bound(v, min, max) -> value within [min,max]
*
fn sort2(a, b) -> (small, large)
*
fn repr(v) -> sfmt('%q', v)
*
fn isEven(a) -> bool
*
fn isOdd(a) -> bool
*
fn decAbs(v) -> number
*
fn concat(sep, ...) -> string
concatenate the string arguments.
*
fn isupper(c) -> string?
return the string if it is only uppercase letters
*
fn islower(c) -> string?
return the string if it is only lowercase letters
*
fn trim(subj, pat, index) -> string
*
fn find(subj, pats, si, plain) -> (ms, me, pi, pat)
find any of a list of patterns. Return the match
start, end as well as
the
index, pat of the pattern matched.
*
fn split(subj, pat--[[%s+]], index--[[1]]) -> (cxt, str) iter
split the subj by pattern.
ctx has two keys:
si (start index) and
ei (end index)
for ctx, line in split(text, '\n') do -- split lines
... do something with line
end
*
fn splitList(...) -> list
*
fn trimEnd(subj, pat, index) -> string
trim the end of the string by removing pat (default='%s')
*
fn squash(s, repl) -> string
Squash a string: convert all whitespace to repl (default=' ').
*
fn usub(s, si, ei, len)
utf8 sub. If len is pre-computed you can pass it in for better performance.
*
fn simplestr(s)
A way to declare simpler mulitline strings which:
- ignores the first/last newline if empty
- removes leading whitespace equal to the first
line (or second line if first line has no indent)
Example:
local s = require'ds'.simplestr
local mystr = s[[
this is
a string.
]]
T.eq('this is\n a string.', mystr)
*
fn bin(uint, width--[[8]], sep4--[['_']]) -> str
Convert integer to binary representation (0's and 1's)
- width will be the number of bits.
- sep4 will be used to separate every 4 bits, set to
nil to disable.
*
fn get(t, k) -> value
t[k