Skip to content

Instantly share code, notes, and snippets.

ClearAll[symbolRules];
symbolRules[syms_List]:=
Join @@ Map[
Function[s, symbolRules @ Unevaluated @ s, HoldFirst],
Unevaluated @ syms
]
symbolRules[sym_String]:=
ToExpression[sym, InputForm, Function[s,symbolRules @ Unevaluated @ s, HoldFirst]];
symbolRules[s_Symbol]:= Join[OwnValues[s],DownValues[s], UpValues[s], SubValues[s]];
symbolRules[]:= symbolRules @ Names["Global`*"];
@lshifr
lshifr / NewOO.m
Last active July 4, 2016 14:55
A new Python - inspired implementation of an object-oriented extension for Mathematica language, which enables creation of garbage-collectable objects
BeginPackage["OO`"]
new
EndPackage[]
BeginPackage["OO`Core`", "RuleBasedFunction`"]
TypeQ;
ObjectQ;
@lshifr
lshifr / RuleBasedFunction.m
Created December 22, 2015 09:09
An implementation of a function constructor that takes (possibly overloaded) function definitions using standard pattern-based syntax (where function name should be denoted as Fn), and returns a Function-based definition that is garbage-collectable
BeginPackage["RuleBasedFunction`"]
RuleBasedFunction::usage = "RuleBasedFunction[defs, attrs] constructs a pure function based on definitions given by rules";
Fn::usage = "Fn is a symbol used to denote a function in definitions used in RuleBasedFunction";
Begin["`Private`"]
SetAttributes[CleanUp, HoldAll]
CleanUp[expr_, cleanup_] :=
@lshifr
lshifr / FrozenCode.m
Created May 19, 2016 14:59
A non-standard evaluator for Mathematica code, allowing to evaluate the code in the "frozen" mode
BeginPackage["FrozenCode`"]
FrozenCodeEvaluate::usage = "FrozenCodeEvaluate[Hold[code], {heads}] evaluates the code
in the 'frozen' mode, where only specific heads are allowed to evaluate";
Begin["`Private`"]
ClearAll[symbolToHideQ]
SetAttributes[symbolToHideQ, HoldFirst];
symbolToHideQ[s_Symbol, expandedSymbs_] :=! MemberQ[expandedSymbs, Unevaluated[s]];
@lshifr
lshifr / SmartLet.m
Created November 7, 2014 20:14
Smart Let in Mathematica
ClearAll[Let, let, symbolOrListQ, inSetDelayed];
SetAttributes[{Let, let, symbolOrListQ}, HoldAll];
symbolOrListQ[_Symbol] = True;
symbolOrListQ[{___?symbolOrListQ}] = True;
symbolOrListQ[_] = False;
Let::lvset = "Local variable specification `1` is not valid.";
@lshifr
lshifr / ReferenceTracker.m
Created April 29, 2016 23:01
A Mathematica package to track which symbols may reference a given symbol
BeginPackage["ReferenceTracker`"]
TrackReferences;
Begin["`Private`"]
Clear[$globalProperties];
$globalProperties = {
OwnValues,
DownValues,
@lshifr
lshifr / Defines.m
Last active March 18, 2016 15:43
Static code analyzer for Mathematica code
ClearAll[shead];
SetAttributes[shead, HoldAllComplete];
shead[f_Symbol[___]] := HoldComplete[f];
shead[f_[___]] := shead[f];
shead[f_ /; AtomQ[Unevaluated[f]]] := Head[f];
(* TODO:This is a bit too simplisitc,since some pattern symbols might \
be localized by inner rules inside expr,and should not be \
counted.We might miss some extra dependencies this way *)
@lshifr
lshifr / HTMLParser.m
Last active March 9, 2016 21:24
A very simplistic breadth - first HTML parser in Wolfram Mathematica
BeginPackage["HTMLParser`"]
ParseHTML::usage = "ParseHTML[s] parses a string of HTML code";
HTMLContainer::usage = "HTMLContainer[tag] is an inert container for the contents of html tag";
Begin["`Private`"]
listSplit[x_List,lengthlist_List,headlist_List]:=
@lshifr
lshifr / SEFormatter.m
Last active December 18, 2015 15:59
Code formatter palette for mathematica.stackexchange.com
(* Mathematica Package *)
(* :Title: SEFormatter *)
(* :Author: Leonid B. Shifrin *)
(* :Summary: Code formatting palette for mathematica.stackexchange.com
(but can be useful also for other formatting purposes) *)
(* :Context: SEFormatter` *)
@lshifr
lshifr / lincomp.c
Last active December 14, 2015 20:19
LinearComplexity
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
int random_in_range (unsigned int min, unsigned int max)
{
int range = max - min,
remainder = RAND_MAX % range,
bucket = RAND_MAX / range;