2

I want to create a function that creates and assigns properties to a symbol. In theory this would behave similarly to the NonzeroValues property of SparseArrays.

Here's what I currently have, and where I'd like to go.

func[x_] := Module[{data},
  (*data=Import data from source based on x*)
  HoldPattern[func[x, "Length"]] = Length@data;
  HoldPattern[func[x, "Dimensions"]] = Dimensions@data;
 ]


func[15,"Length"]

225

However, I'd like this to work more like this:

var=func[15];

var["Length"]

225

It would be ideal not to have to calculate this every time a property is retrieved. For efficiency sake, importing the data every time a property is retrieved isn't feasible.

How so I define these subvalues at the first function call and store them for fast retrieval later?

kale
  • 10,922
  • 1
  • 32
  • 69

0 Answers0