wl-init
Some Wolfram Language (a.k.a. Mathematica) packages that make things easier
-
AllowTrailingComma.m
: Drop every trailing comma inRowBox
. Only boxes interface. For some reason,$PreRead
doesn’t work in text-based interface at all! Also, the package allows some constructs (Module
,With
,Block
,Function
) have empty (Null
) elements in their first argument. In particular, trailing and leading commas are ignored. -
RangeSyntax.m
: Interpret (in boxes)x = a … b as {x, a, b}
andx = a … b … d as {x, a, b, d}
to be used in range specification. -
DifferentialD.m
: PrettyprintDt[x]
asⅆx
. Conversely, interpretⅆx
asDt[x]
. -
ForEach.m
: Another syntax forDo
loop orTable
list comprehension with an iterator in the first argument. -
FormatUsage.m
: APrivate`FormatUsage
function that is used in other packages to, yes, format usage messages. Later, I foundMacros`SetUsage
but it didn’t satisfy me. -
UsageOnly.m
: APrivate`UsageOnly[symb]
auxiliary function that is used in some packages to indicate that the symbol does nothing but holdssymb::usage
message. It may be used in a package that does not introduce valuable eponymous symbol. -
IfThenElse.m
: TheIf
’s syntax makes code be very confusing when used not as ternary operator with several-word branches but as conditional statement with large branches. Else branch starts somewhere below with a tiny comma (one of many).If [test] Then [ifTrue] Else [ifFalse]
is much easier to read. Also,IfQ
is added to surely choose between branches; it is guaranteed to be evaluated to one of the branches, unlikeIf
which stays unevaluated unlesstest
evaluates to boolean. -
QuantityInput.m
: Simple input ofQuantity[5, "cm"]
as5::cm
. -
MethodCall.m
: OO-style syntactic sugar:expr::foo[args…]
evaluates tofoo[expr, args…]
. Some functions likeMap
,Apply
,Nest
, etc. are treated with first two arguments reversed. Method calls may be chained, thusl = {{1, 2, 3}, {4, 5, 6}}; (l ::Append[{7, 8, 9}] ::Apply[Plus, {1}] ::Riffle[", and "] ::Map[ToString] ::StringJoin[])
gives
"6, and 15, and 24"
.
The package may conflict with theQuantityInput`
package, so the code above works only due tol
being not explicitly (in its unevaluated form) a number or a list of numbers. (Also note the parentheses: without them, newlines would terminate expression leading to a syntax error.)expr //$@ foo[args…]
is a version with very low precedence. -
ScopeExit.m
: Write here, defer evaluation until end of block. Useful for assured resources release. Inspired byscope(exit)
in D language. -
SequenceParse.m
: When in boxes interface,Sequence
s may be entered in parentheses separated by comma:()
is emptySequence[]
,(1,)
is unarySequence[1]
,(1,2,3)
isSequence[1,2,3]
.∅∅
interprets as (not evaluates to)Sequence[]
even in text interface. -
SwitchPattern.m
(maybe not the best name, couldn’t think up better): SystemSwitch
function compares its first argument to several pattern forms, and evaluates to the value corresponding to the first match. The problem is, values cannot use variables (named patterns) bound in pattern forms. TheSwitchPattern
function solves this problem. -
HeldPureFunction.m
: Easily construct anonymous functions withHoldAllComplete
attribute. E.g.List @@ (Length[Unevaluated@#]&!) /@ Hold[1+1+1, 2+2]
gives{3, 2}
. -
ThrowGeneral.m
: Two different forms ofThrow
&Catch
— tagged and untagged — is a headache. They are unrelated: taggedCatch
can’t catch untaggedThrow
and vice versa. So, bothCatch[ Throw[expr, tag] ]
andCatch[ Throw[expr] , _]
catch nothing! This package makes a tag be used implicitly: the untagged versions are simply redirected to the tagged ones.Catch[expr]
catches any thrown thing;Throw[expr]
usesGeneral
tag. -
TypeSystemEither.m
:TypeSystem`Either
union type is declared but not working (at least, in 10.0.1). This is a minor fix just to type check viaTypeSystem`ConformsQ
. Complex things withDataset
are not considered. -
IsQ.m
:IsQ[expr, head]
orIsQ[head][expr]
is equivalent toHead[expr] === head
. The same way,IsntQ[expr, head]
orIsntQ[head][expr]
is a negation of that. -
WithNest.m
: The package provide versions ofWith
andModule
—WithNest
andModuleNest
in which each local-variable initialisations may refer to previously defined ones. E.g.WithNest[{a = 1, b = -a, c = 42}, b + c]
gives41
. Braces are optional, i.e. one can writeWithNest[a = 1, b = -a, c = 42, b + c]
.System`Block
already has the property of consecutive initialisers, soBlockNest
is provided just in consistency and for a braceless form. The*Nest
names are for historical reasons; now I doubt they are good enough, but cannot think up better ones.GeneralUtilities`Where
provides the same behavior asWithNest
in a braceless form. -
TheSymbol.m
: Some single-letter system symbols —E
,I
,O
,D
,K
,C
,N
,π
— are protected.TheSymbolE, TheSymbolI, …, TheSymbolPi
are provided that are displayed as a letter and are assignable.TheSymbol[⟨symbol or string⟩]
can be used to be evaluated to one of the above symbols. -
Associate.m
:Association
has an undocumented usage as a replace-value/add-key function. There’s no need to use thisAssociate
since its functionality is exactly inAssiciation
. -
Second.m
: Convenient functionsSecond
,Third
, …,Tenth
— wrappers ofPart
.