Directories
EPHS.Directories — Module
Directories - the NonEmptyDtry and Dtry monads
A nonempty directory of Ts (NonEmptyDtry{T}) is essentially a tree whose leaves hold values of type T. Each value is addressed by the path from the root node to the respective leaf, given by a list of Symbols, see DtryPath.
The monad unit sends a value of type T to a tree consisting only of a leaf holding the value.
The monad multiplication flattens a directory of directories of Ts into a directory of Ts by grafting the trees stored at the leaves directly onto their respective parent nodes.
A (possibly empty) directory of Ts (Dtry{T}) is either empty or it is a nonempty directory of Ts. The Dtry monad is hence obtained by composing the NonEmptyDtry monad with the 'Maybe monad'. The composition of the monads relies on a distributive law. Since subdirectories (subtrees) of a directory cannot be empty, the distributive law filters out any empty directories at the leaves, as they cannot be grafted onto their parent nodes.
This module implements directories as an immutable data structure. Subdirectories are stored in lexicographic order to ensure that two directories with the same set of paths (namespace) and the same associated values are equal.
The implementation supports simple access of subdirectories and values, pretty-printing, iteration, mapping, filtering, merging, etc.
EPHS.Directories.AbstractDtry — Type
The concrete subtypes of AbstractDtry are NonEmptyDtry and Dtry.
EPHS.Directories.Dtry — Type
A Dtry{T} is either empty or it wraps a NonEmptyDtry of Ts.
EPHS.Directories.Dtry — Method
Dtry(value::T) -> Dtry{T}Construct a directory, which contains just a single value (monad unit).
EPHS.Directories.Dtry — Method
Dtry(pairs::Vararg{Pair{Symbol,Dtry{T}}}) -> Dtry{T}Construct a directory of Ts from a number of pairs of names and subdirectories. Empty subdirectories are filtered out.
EPHS.Directories.Dtry — Method
Dtry{T}()Construct an empty directory of Ts.
EPHS.Directories.DtryAccessError — Type
Abstract supertype for exceptions that may be thrown when accessing directories. Concrete subtypes are DtryBranchError (branch with a given name does not exist) and DtryLeafError (given directory is not a leaf).
EPHS.Directories.DtryBranchError — Type
DtryBranchError(dtry::AbstractDtry, name::Symbol)The given directory has no direct subdirectory with the given name.
EPHS.Directories.DtryLeafError — Type
DtryLeafError(dtry::AbstractDtry)The given directory is not a leaf holding a value.
EPHS.Directories.DtryPath — Type
A DtryPath is a linked list of Symbols representing a path in a directory, see also ■.
EPHS.Directories.DtryPath — Method
DtryPath(names::Vararg{Symbol})Construct a DtryPath from a series of names.
Example
julia> DtryPath(:foo, :bar) == ■.foo.bar
trueEPHS.Directories.NonEmptyDtry — Type
A NonEmptyDtry{T} either contains a single value of type T or it has at least one non-empty subdirectory of Ts.
EPHS.Directories.NonEmptyDtry — Method
NonEmptyDtry(value::T) -> NonEmptyDtry{T}Construct a non-empty directory, which contains just a single value (monad unit).
EPHS.Directories.NonEmptyDtry — Method
NonEmptyDtry(pairs::Vararg{Pair{Symbol,NonEmptyDtry{T}}}) -> NonEmptyDtry{T}Construct a non-empty directory of Ts from a number of pairs of names and non-empty subdirectories.
EPHS.Directories.■ — Constant
DtryPath representing the root node of a directory. With that, ■.foo represents the path to a subdirectory named foo.
Base.Iterators.zip — Method
zip(dtry1::NonEmptyDtry{T1}, dtry2::NonEmptyDtry{T2}) -> NonEmptyDtry{Tuple{T1,T2}}
zip(dtry1::Dtry{T1}, dtry2::Dtry{T2}) -> Dtry{Tuple{T1,T2}}Given a directory of T1s and a directory of T2s with the same tree structure, as well as a function f : T1 × T2 -> X, produce a directory of Tuple{T1,T2}s, where the value at path p is given by (dtry1[p], dtry2[p]).
Base.collect — Method
collect(dtry::AbstractDtry{T}) -> Vector{T}Collect values from a directory.
Base.filter — Method
filter(f, dtry::AbstractDtry{T}) -> Dtry{T}Given a function f : T -> Bool and a (nonempty) directory of Ts, produce a new directory of Ts by keeping the entry at path p only if f(dtry[p]) == true.
Base.foreach — Method
foreach(f, dtry::Dtry{T}) -> NothingCall function f on each Pair{DtryPath,T}.
Base.foreach — Method
foreach(f, dtry::NonEmptyDtry{T}) -> NothingCall function f on each Pair{DtryPath,T}.
Base.getindex — Method
getindex(dtry::Dtry{T}, path::DtryPath) -> TIf path refers to a value within dtry, returns the value. Otherwise, throws a DtryAccessError.
Base.getindex — Method
getindex(dtry::Dtry{T}) -> TIf dtry isa leaf, dtry[] returns its value. Otherwise, throws a DtryLeafError
Base.getindex — Method
getindex(dtry::NonEmptyDtry{T}) -> TIf dtry isa leaf, dtry[] returns its value. Otherwise, throws a DtryLeafError
Base.getindex — Method
getindex(dtry::NonEmptyDtry{T}, path::DtryPath) -> TIf path refers to a value within dtry, returns the value. Otherwise, throws a DtryAccessError.
Base.isempty — Method
isempty(dtry::AbstractDtry) -> BoolReturns true if the given directory is empty.
Base.length — Method
length(path::DtryPath) -> IntReturns the length of the DtryPath (linked list of Symbols).
Example
julia> length(■.foo.bar) == 2
trueBase.length — Method
length(dtry::Dtry) -> IntReturns the number of values (leaves) in the given directory.
Base.length — Method
length(dtry::NonEmptyDtry) -> IntReturns the number of values (leaves) in the given directory.
Base.mapreduce — Method
mapreduce(f, op, dtry::Dtry{T}, default)Transforms each value contained in the given directory using the function f : T -> X and reduces the results using the binary operation op : X × X -> X. For an empty directory, returns default.
Base.mapreduce — Method
mapreduce(f, op, dtry::NonEmptyDtry{T})Transforms each value contained in the given directory using the function f : T -> X and reduces the results using the binary operation op : X × X -> X.
Base.merge — Method
merge(dtry1::NonEmptyDtry{T}, dtry2::NonEmptyDtry{T}) -> NonEmptyDtry{T}
merge(dtry1::Dtry{T}, dtry2::Dtry{T}) -> Dtry{T}Merge two directories, given that their namespaces are disjoint, i.e. the union of their namespaces is prefix-free.
EPHS.Directories.filtermap — Method
filtermap(f, dtry::AbstractDtry{T}, X::Type) -> Dtry{X}Make a new directory of Xs from a (nonempty) directory of Ts based on a function f : T -> Union{Some{X},Nothing}. When f returns nothing, the respective entry is filtered out.
EPHS.Directories.filtermapwithpath — Method
filtermapwithpath(f, dtry::AbstractDtry{T}, X::Type) -> Dtry{X}Make a new directory of Xs from a (nonempty) directory of Ts based on a function f : DtryPath × T -> Union{Some{X},Nothing}. When f returns nothing, the respective entry is filtered out.
EPHS.Directories.foreachpath — Method
foreachpath(f, dtry::Dtry) -> NothingCall function f on each path::DtryPath.
EPHS.Directories.foreachpath — Method
foreachpath(f, dtry::NonEmptyDtry) -> NothingCall function f on each path::DtryPath.
EPHS.Directories.foreachvalue — Method
foreachvalue(f, dtry::Dtry{T}) -> NothingCall function f on each value::T.
EPHS.Directories.foreachvalue — Method
foreachvalue(f, dtry::NonEmptyDtry{T}) -> NothingCall function f on each value::T.
EPHS.Directories.haspath — Method
haspath(dtry::Dtry, path::DtryPath) -> BoolReturns true if the given directory contains a leaf/value at the given path.
EPHS.Directories.haspath — Method
haspath(dtry::NonEmptyDtry, path::DtryPath) -> BoolReturns true if the given directory contains a leaf/value at the given path.
EPHS.Directories.hasprefix — Method
hasprefix(dtry::Dtry, prefix::DtryPath) -> BoolReturns true if the given directory has a (complete) path (to a leaf/value), which starts with the given prefix (or incomplete path).
EPHS.Directories.hasprefix — Method
hasprefix(dtry::NonEmptyDtry, prefix::DtryPath) -> BoolReturns true if the given directory has a (complete) path (to a leaf/value), which starts with the given prefix (or incomplete path).
EPHS.Directories.mapwithpath — Method
mapwithpath(f, dtry::Dtry{T}, X::Type) -> Dtry{X}Make a new directory of Xs with the same tree structure as dtry, where the value at path p is given by f((p, dtry[p]))::X.
EPHS.Directories.mapwithpath — Method
mapwithpath(f, dtry::NonEmptyDtry{T}, X::Type) -> NonEmptyDtry{X}Make a new directory of Xs with the same tree structure as dtry, where the value at path p is given by f((p, dtry[p]))::X.
EPHS.Directories.print_dtry — Method
print_dtry(io::IO, dtry::Dtry{T}; prefix::String, print_value=nothing)Write a pretty-printed representation of the given directory to io. As an optional keyword argument, a function print_value(io::IO, value::T; prefix::String) can be used to pretty-print the values. Whenever the output of print_value spans more than one line, prefix is prepended to the extra lines. To print a directory of directories, let print_values=print_dtry.
EPHS.Directories.print_dtry_repr — Method
print_dtry_repr(io::IO, dtry::Dtry{T})Write a round-trippable code representation of the given directory to io.
EPHS.Directories.zipmap — Method
zipmap(f, dtry1::Dtry{T1}, dtry2::Dtry{T2}, X::Type) -> Dtry{X}Given a directory of T1s and a directory of T2s with the same tree structure, as well as a function f : T1 × T2 -> X, produce a new directory of Xs, where the value at path p is given by f(dtry1[p], dtry2[p]).
EPHS.Directories.zipmap — Method
zipmap(f, dtry1::NonEmptyDtry{T1}, dtry2::NonEmptyDtry{T2}, X::Type) -> NonEmptyDtry{X}Given a nonempty directory of T1s and a nonempty directory of T2s with the same tree structure, as well as a function f : T1 × T2 -> X, produce a new nonempty directory of Xs, where the value at path p is given by f(dtry1[p], dtry2[p]).
EPHS.Directories.zipmapwithpath — Method
zipmapwithpath(f, dtry1::Dtry{T1}, dtry2::Dtry{T2}, X::Type) -> Dtry{X}Given a directory of T1s and a directory of T2s with the same tree structure, as well as a function f : DtryPath × T1 × T2 -> X, produce a new directory of Xs, where the value at path p is given by f(p, dtry1[p], dtry2[p]).
EPHS.Directories.zipmapwithpath — Method
zipmapwithpath(f, dtry1::NonEmptyDtry{T1}, dtry2::NonEmptyDtry{T2}, X::Type) -> NonEmptyDtry{X}Given a directory of T1s and a directory of T2s with the same tree structure, as well as a function f : DtryPath × T1 × T2 -> X, produce a new directory of Xs, where the value at path p is given by f(p, dtry1[p], dtry2[p]).
EPHS.MoreBase.flatten — Method
flatten(dtry::NonEmptyDtry{NonEmptyDtry{T}}) -> Dtry{T}Flattens a nonempty directory of nonempty directories (monad multiplication).
EPHS.MoreBase.flatten — Method
flatten(dtry::Dtry{Dtry{T}}) -> Dtry{T}Flattens a directory of directories (monad multiplication).