Custom Property interface on my own objects
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I know I can use the Property
interface for things like Graph
but I want to use it for all of my objects. This includes:
SetProperty
PropertyValue
PropertyList
RemoveProperty
Can I define it in some efficient, non-memory-leaky way for any object I want?
customization undocumented properties
add a comment |Â
up vote
3
down vote
favorite
I know I can use the Property
interface for things like Graph
but I want to use it for all of my objects. This includes:
SetProperty
PropertyValue
PropertyList
RemoveProperty
Can I define it in some efficient, non-memory-leaky way for any object I want?
customization undocumented properties
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I know I can use the Property
interface for things like Graph
but I want to use it for all of my objects. This includes:
SetProperty
PropertyValue
PropertyList
RemoveProperty
Can I define it in some efficient, non-memory-leaky way for any object I want?
customization undocumented properties
I know I can use the Property
interface for things like Graph
but I want to use it for all of my objects. This includes:
SetProperty
PropertyValue
PropertyList
RemoveProperty
Can I define it in some efficient, non-memory-leaky way for any object I want?
customization undocumented properties
customization undocumented properties
asked 4 hours ago
b3m2a1
24.9k254147
24.9k254147
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
The answer is a definitive yes, if you're willing to work with the new Language`ExpressionStore
. This goes hand-in-hand with the Language`MutationHandler
stuff to allow us to finally do custom OOP in Mathematica without any odd tricks.
if you just want to play with this scroll down to the example section
Basic Idea
But for the problem at hand, this is actually very simple. We simply make property-store:
$propStore=Language`NewExpressionStore["<PropertyStore>"];
Then we use the get-put-remove interface on this:
containsQ[x_]:=
$propStore@"containsQ"[x];
containsQ[x_, p_]:=
$propStore@"containsQ"[x, p];
(* ::Subsubsection::Closed:: *)
(*get*)
get[x_, p_]:=
$propStore@"get"[x, p];
(* ::Subsubsection::Closed:: *)
(*set*)
set[x_, p_, v_]:=
$propStore@"put"[x, p ,v];
(* ::Subsubsection::Closed:: *)
(*remove*)
remove[x_]:=
$propStore@"remove"[x];
(* ::Subsubsection::Closed:: *)
(*keys*)
keys:=
$propStore@"getKeys";
keys[x_]:=
$propStore@"getKeys"[x];
(* ::Subsubsection::Closed:: *)
(*list*)
list:=
$propStore@"listTable";
And now we can use this naturally as a property set/get mechanism.
Fun Example
I put this all into a little package that imitates the syntax of Property
and friends. Load this from GitHub:
Get["https://github.com/b3m2a1/mathematica-tools/raw/master/Props.m"]
then we'll define a function that caches results in a memory cheap way:
cachedCompute[m_, p_, fn_] :=
Replace[PropVal[m, p],
Missing["PropertyAbsent", p] :>
With[val = fn[m],
SetProp[m, p -> val];
val
]
]
Module[,
(* I do this to avoid $HistoryLength *)
m1 = MemoryInUse;
myMat = RandomReal[, 100, 100];
m1
];
t1 = RepeatedTiming[cachedCompute[myMat, "Inverse", Inverse]][[1]];
t2 = RepeatedTiming[Inverse[myMat]][[1]];
m2 = MemoryInUse;
t2/t1
5.*10^1
m2 - m1
167088
Clear@myMat
MemoryInUse - m1
10480
And we see the caching got us a 50x speed-up but once the original matrix was cleared, the cached memory was free.
Fast properties without memory leaks, thanks to Jason B.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
The answer is a definitive yes, if you're willing to work with the new Language`ExpressionStore
. This goes hand-in-hand with the Language`MutationHandler
stuff to allow us to finally do custom OOP in Mathematica without any odd tricks.
if you just want to play with this scroll down to the example section
Basic Idea
But for the problem at hand, this is actually very simple. We simply make property-store:
$propStore=Language`NewExpressionStore["<PropertyStore>"];
Then we use the get-put-remove interface on this:
containsQ[x_]:=
$propStore@"containsQ"[x];
containsQ[x_, p_]:=
$propStore@"containsQ"[x, p];
(* ::Subsubsection::Closed:: *)
(*get*)
get[x_, p_]:=
$propStore@"get"[x, p];
(* ::Subsubsection::Closed:: *)
(*set*)
set[x_, p_, v_]:=
$propStore@"put"[x, p ,v];
(* ::Subsubsection::Closed:: *)
(*remove*)
remove[x_]:=
$propStore@"remove"[x];
(* ::Subsubsection::Closed:: *)
(*keys*)
keys:=
$propStore@"getKeys";
keys[x_]:=
$propStore@"getKeys"[x];
(* ::Subsubsection::Closed:: *)
(*list*)
list:=
$propStore@"listTable";
And now we can use this naturally as a property set/get mechanism.
Fun Example
I put this all into a little package that imitates the syntax of Property
and friends. Load this from GitHub:
Get["https://github.com/b3m2a1/mathematica-tools/raw/master/Props.m"]
then we'll define a function that caches results in a memory cheap way:
cachedCompute[m_, p_, fn_] :=
Replace[PropVal[m, p],
Missing["PropertyAbsent", p] :>
With[val = fn[m],
SetProp[m, p -> val];
val
]
]
Module[,
(* I do this to avoid $HistoryLength *)
m1 = MemoryInUse;
myMat = RandomReal[, 100, 100];
m1
];
t1 = RepeatedTiming[cachedCompute[myMat, "Inverse", Inverse]][[1]];
t2 = RepeatedTiming[Inverse[myMat]][[1]];
m2 = MemoryInUse;
t2/t1
5.*10^1
m2 - m1
167088
Clear@myMat
MemoryInUse - m1
10480
And we see the caching got us a 50x speed-up but once the original matrix was cleared, the cached memory was free.
Fast properties without memory leaks, thanks to Jason B.
add a comment |Â
up vote
3
down vote
The answer is a definitive yes, if you're willing to work with the new Language`ExpressionStore
. This goes hand-in-hand with the Language`MutationHandler
stuff to allow us to finally do custom OOP in Mathematica without any odd tricks.
if you just want to play with this scroll down to the example section
Basic Idea
But for the problem at hand, this is actually very simple. We simply make property-store:
$propStore=Language`NewExpressionStore["<PropertyStore>"];
Then we use the get-put-remove interface on this:
containsQ[x_]:=
$propStore@"containsQ"[x];
containsQ[x_, p_]:=
$propStore@"containsQ"[x, p];
(* ::Subsubsection::Closed:: *)
(*get*)
get[x_, p_]:=
$propStore@"get"[x, p];
(* ::Subsubsection::Closed:: *)
(*set*)
set[x_, p_, v_]:=
$propStore@"put"[x, p ,v];
(* ::Subsubsection::Closed:: *)
(*remove*)
remove[x_]:=
$propStore@"remove"[x];
(* ::Subsubsection::Closed:: *)
(*keys*)
keys:=
$propStore@"getKeys";
keys[x_]:=
$propStore@"getKeys"[x];
(* ::Subsubsection::Closed:: *)
(*list*)
list:=
$propStore@"listTable";
And now we can use this naturally as a property set/get mechanism.
Fun Example
I put this all into a little package that imitates the syntax of Property
and friends. Load this from GitHub:
Get["https://github.com/b3m2a1/mathematica-tools/raw/master/Props.m"]
then we'll define a function that caches results in a memory cheap way:
cachedCompute[m_, p_, fn_] :=
Replace[PropVal[m, p],
Missing["PropertyAbsent", p] :>
With[val = fn[m],
SetProp[m, p -> val];
val
]
]
Module[,
(* I do this to avoid $HistoryLength *)
m1 = MemoryInUse;
myMat = RandomReal[, 100, 100];
m1
];
t1 = RepeatedTiming[cachedCompute[myMat, "Inverse", Inverse]][[1]];
t2 = RepeatedTiming[Inverse[myMat]][[1]];
m2 = MemoryInUse;
t2/t1
5.*10^1
m2 - m1
167088
Clear@myMat
MemoryInUse - m1
10480
And we see the caching got us a 50x speed-up but once the original matrix was cleared, the cached memory was free.
Fast properties without memory leaks, thanks to Jason B.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
The answer is a definitive yes, if you're willing to work with the new Language`ExpressionStore
. This goes hand-in-hand with the Language`MutationHandler
stuff to allow us to finally do custom OOP in Mathematica without any odd tricks.
if you just want to play with this scroll down to the example section
Basic Idea
But for the problem at hand, this is actually very simple. We simply make property-store:
$propStore=Language`NewExpressionStore["<PropertyStore>"];
Then we use the get-put-remove interface on this:
containsQ[x_]:=
$propStore@"containsQ"[x];
containsQ[x_, p_]:=
$propStore@"containsQ"[x, p];
(* ::Subsubsection::Closed:: *)
(*get*)
get[x_, p_]:=
$propStore@"get"[x, p];
(* ::Subsubsection::Closed:: *)
(*set*)
set[x_, p_, v_]:=
$propStore@"put"[x, p ,v];
(* ::Subsubsection::Closed:: *)
(*remove*)
remove[x_]:=
$propStore@"remove"[x];
(* ::Subsubsection::Closed:: *)
(*keys*)
keys:=
$propStore@"getKeys";
keys[x_]:=
$propStore@"getKeys"[x];
(* ::Subsubsection::Closed:: *)
(*list*)
list:=
$propStore@"listTable";
And now we can use this naturally as a property set/get mechanism.
Fun Example
I put this all into a little package that imitates the syntax of Property
and friends. Load this from GitHub:
Get["https://github.com/b3m2a1/mathematica-tools/raw/master/Props.m"]
then we'll define a function that caches results in a memory cheap way:
cachedCompute[m_, p_, fn_] :=
Replace[PropVal[m, p],
Missing["PropertyAbsent", p] :>
With[val = fn[m],
SetProp[m, p -> val];
val
]
]
Module[,
(* I do this to avoid $HistoryLength *)
m1 = MemoryInUse;
myMat = RandomReal[, 100, 100];
m1
];
t1 = RepeatedTiming[cachedCompute[myMat, "Inverse", Inverse]][[1]];
t2 = RepeatedTiming[Inverse[myMat]][[1]];
m2 = MemoryInUse;
t2/t1
5.*10^1
m2 - m1
167088
Clear@myMat
MemoryInUse - m1
10480
And we see the caching got us a 50x speed-up but once the original matrix was cleared, the cached memory was free.
Fast properties without memory leaks, thanks to Jason B.
The answer is a definitive yes, if you're willing to work with the new Language`ExpressionStore
. This goes hand-in-hand with the Language`MutationHandler
stuff to allow us to finally do custom OOP in Mathematica without any odd tricks.
if you just want to play with this scroll down to the example section
Basic Idea
But for the problem at hand, this is actually very simple. We simply make property-store:
$propStore=Language`NewExpressionStore["<PropertyStore>"];
Then we use the get-put-remove interface on this:
containsQ[x_]:=
$propStore@"containsQ"[x];
containsQ[x_, p_]:=
$propStore@"containsQ"[x, p];
(* ::Subsubsection::Closed:: *)
(*get*)
get[x_, p_]:=
$propStore@"get"[x, p];
(* ::Subsubsection::Closed:: *)
(*set*)
set[x_, p_, v_]:=
$propStore@"put"[x, p ,v];
(* ::Subsubsection::Closed:: *)
(*remove*)
remove[x_]:=
$propStore@"remove"[x];
(* ::Subsubsection::Closed:: *)
(*keys*)
keys:=
$propStore@"getKeys";
keys[x_]:=
$propStore@"getKeys"[x];
(* ::Subsubsection::Closed:: *)
(*list*)
list:=
$propStore@"listTable";
And now we can use this naturally as a property set/get mechanism.
Fun Example
I put this all into a little package that imitates the syntax of Property
and friends. Load this from GitHub:
Get["https://github.com/b3m2a1/mathematica-tools/raw/master/Props.m"]
then we'll define a function that caches results in a memory cheap way:
cachedCompute[m_, p_, fn_] :=
Replace[PropVal[m, p],
Missing["PropertyAbsent", p] :>
With[val = fn[m],
SetProp[m, p -> val];
val
]
]
Module[,
(* I do this to avoid $HistoryLength *)
m1 = MemoryInUse;
myMat = RandomReal[, 100, 100];
m1
];
t1 = RepeatedTiming[cachedCompute[myMat, "Inverse", Inverse]][[1]];
t2 = RepeatedTiming[Inverse[myMat]][[1]];
m2 = MemoryInUse;
t2/t1
5.*10^1
m2 - m1
167088
Clear@myMat
MemoryInUse - m1
10480
And we see the caching got us a 50x speed-up but once the original matrix was cleared, the cached memory was free.
Fast properties without memory leaks, thanks to Jason B.
edited 1 hour ago
J. M. is computer-lessâ¦
94.9k10294454
94.9k10294454
answered 4 hours ago
b3m2a1
24.9k254147
24.9k254147
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184123%2fcustom-property-interface-on-my-own-objects%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password