# Get DownValues that do not involve patterns?

Say I have a function with a few definitions that also implements memoization:

f[x_] := f[x] = x^2
f[x_, y_] := f[x, y] = x^2 + y^2


I then apply the function to some data:

f /@ {1, 2, 3}
f @@@ {{1, 2}, {3, 4}}


Now the function will have a bunch of DownValues both for the stored results and for the pattern based definitions. Is there some easy way to retrieve only all the stored result, leaving out the pattern based definitions?

-
Possible duplicates: (7972), (9440), and one on StackOverflow. – Mr.Wizard Feb 14 '13 at 1:09
one way of extracting the inputs/outputs: Cases[DownValues[f], (_[Verbatim[f][value : Except[_Pattern]]] :> result_) :> (value -> result)] (general note: i figure this out by using FullForm. e.g. FullForm[x_] => Pattern[x, Blank[]]) – amr Dec 31 '13 at 23:29

You can use FreeQ and select only those down-values that are free of Pattern:

Select[DownValues@f, FreeQ[#, Pattern] &]
(* {HoldPattern[f[1]] :> 1, HoldPattern[f[2]] :> 4, HoldPattern[f[3]] :> 9,
HoldPattern[f[1, 2]] :> 5, HoldPattern[f[3, 4]] :> 25} *)

-
R.M please see my comment above. Do you think this is a duplicate? It's not exact but I think the earlier answer also answer this one. – Mr.Wizard Feb 14 '13 at 1:11
@Mr.Wizard I don't think it's a duplicate, because Simon's question was about clearing DVs with exactly $n$ arguments. It so happens that your code (with slight modification) would do this, but I don't think it would be obvious to a user who reads it thinking it's a dupe. Simon's (the Aussie one) question on SO though, is an almost dupe — if it had been on this site, I would've probably closed as a duplicate w/ a comment explaining the minor change needed. In any case, if this eventually gets closed, so be it :) – R. M. Feb 14 '13 at 1:27
+1 on Q&A in light of this not being a proper duplicate. – Mr.Wizard Feb 14 '13 at 5:24
Depending on how you interpret the question, you could potentially use Free[#[[1]],Pattern]& to only select the patterns that don't involve pattern-matching, and not sort out those that might return a pattern or use a pattern in it's definition. For instance f[b]=a_:>a – jVincent Feb 14 '13 at 9:30
Nice! This also seems to be the faster solution when the number and size of the DownValues get big. – Mr Alpha Feb 14 '13 at 10:51

An alternative

SetAttributes[getImmediateDownvalues, {HoldFirst, Listable}];
getImmediateDownvalues[sym_Symbol] :=
InternalInheritedBlock[{sym},
Module[{tag},
PrependTo[DownValues[sym], HoldPattern[sym[] /; tag] :> Null];
TakeWhile[
DownValues[
sym], ! MatchQ[#, Verbatim@HoldPattern[sym[] /; tag] :> _] &]]]


So

getImmediateDownvalues[f]
(* {HoldPattern[f[1]] :> 1, HoldPattern[f[2]] :> 4,
HoldPattern[f[3]] :> 9, HoldPattern[f[1, 2]] :> 5,
HoldPattern[f[3, 4]] :> 25} *)

-