Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

2 Answers

up vote 6 down vote accepted

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} *)
share|improve this answer
    
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
1  
@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 :) –  rm -rf 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
2  
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
add comment

An alternative

SetAttributes[getImmediateDownvalues, {HoldFirst, Listable}];
getImmediateDownvalues[sym_Symbol] := 
 Internal`InheritedBlock[{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} *)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.