I am somewhat new to functional programming. To improve the performance of a package, I would like to re-write a package in a functional style.

I have the following Dataset:

testPoints = Dataset[{ <|"id"->1, "x"->3.75, "y"->3.75, "z"->1.5 |>,
                       <|"id"->2, "x"->-3.75, "y"->3.75, "z"->1.5 |>,
                       <|"id"->3, "x"->-0.95, "y"->-0.95, "z"->1.5 |>,
                       <|"id"->4, "x"->0.95, "y"->-0.95, "z"->1.5 |>,
                       <|"id"->5, "x"->0.95, "y"->0.95,  "z"->1.17 |>,
                       <|"id"->6, "x"->-0.95, "y"->0.95, "z"->1.17 |>,
                       <|"id"->7, "x"->-2.84, "y"->-2.84, "z"->1.17 |>,
                       <|"id"->8, "x"->2.84, "y"->-2.84, "z"->1.17 |>,
                       <|"id"->9, "x"->4.01, "y"->0, "z"->0.83 |>,
                       <|"id"->10, "x"->0, "y"->4.01, "z"->0.83 |>,
                       <|"id"->11, "x"->-2.67, "y"->0, "z"->0.83 |>,
                       <|"id"->12, "x"->0, "y"->-2.67, "z"->0.83 |>,
                       <|"id"->13, "x"->1.89, "y"->1.89, "z"->0.5 |>,
                       <|"id"->14, "x"->-1.89, "y"->1.89, "z"->0.5 |>,
                       <|"id"->15, "x"->-3.75, "y"->-3.75, "z"->0.5 |>,
                       <|"id"->16, "x"->3.75, "y"->-3.75, "z"->0.5 |>}];

I am unclear on how I would specify specific columns in a Dataset in a Map or Apply function.

How would I apply f[x], g[x,y] or h[x,y,z] to the rows in this Dataset?


It depends what you want as result and please don't forget to search this site and look through other posts like this one:

Anyway, let me give you two simple examples:

f = <|#, "x2" -> #x^2|> &;
h = <|"Value" -> #x + #y + #z|> &;

Map[f, testPoints]
Map[h, testPoints]

and you get

Mathematica graphics Mathematica graphics

Since the above code doesn't seem to work consistently in all releases of version 10, please try this alternative as suggested by Gordon Coale

testPoints[All, Append[#, "x2" -> #x^2] &]
  • $\begingroup$ halirutan - Thank you very much - that is very useful. I was trying to use a Map[f, testPoints[All, "x"]] and similar constructs. $\endgroup$ – Doug Kimzey Apr 28 '15 at 14:22
  • $\begingroup$ halirutan - I could not get this to work. I am getting an error message: Keys::invrl: The argument Keys[Association[TypeSystemStruct[{id,TypeSystemAtom[String],y,z},{TypeSystemAtom[Integer],TypeSystemAtom[Real],TypeSystemAtom[Real],TypeSystemAtom[Real]}],x2->TypeSystem`Atom[Real]^2]] is not a valid Association or a list of rules. >> I am using Mathematica 10.0.2. Are you using the same version? $\endgroup$ – Doug Kimzey Apr 28 '15 at 21:43
  • $\begingroup$ Unfortunately, I have only 10.1 here. Can you tell me what this gives on your machine <|#|> &[<|"u" -> x, "v" -> y|>] or this here <|"u" -> x, <|"z" -> 1|>|>? If you cannot get this working, can you please poste a complete example that gives the error to e.g. hastebin.com and give the link here? Or you ask in the Mathematica Chat. $\endgroup$ – halirutan Apr 28 '15 at 23:48
  • 2
    $\begingroup$ @halirutan your format above works in 10.0.1 is broken in 10.0.2 and works again in 10.1. I strongly recommend this thread for some useful dataset queries. A similar example that does work in 10.0.2 is this : testPoints[All, Append[#, "x2" -> #x^2] &] $\endgroup$ – Gordon Coale Apr 29 '15 at 8:35
  • 1
    $\begingroup$ @GordonCoale Yes, it always felt a bit half-baken to me too and this is probably one reason why I haven't used it very often so far. $\endgroup$ – halirutan Apr 29 '15 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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