8

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?

Mahdi
  • 1,619
  • 10
  • 23
Doug Kimzey
  • 2,279
  • 1
  • 14
  • 21

1 Answers1

11

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] &]
halirutan
  • 112,764
  • 7
  • 263
  • 474
  • halirutan - Thank you very much - that is very useful. I was trying to use a Map[f, testPoints[All, "x"]] and similar constructs. – Doug Kimzey Apr 28 '15 at 14:22
  • 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?

    – Doug Kimzey Apr 28 '15 at 21:43
  • 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. http://hastebin.com/ and give the link here? Or you ask in the [chat]. – halirutan Apr 28 '15 at 23:48
  • 2
    @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] &] – Gordon Coale Apr 29 '15 at 08:35
  • @GordonCoale I have included all your information in my answer. Thanks for trying! – halirutan Apr 29 '15 at 08:41
  • Quoting directly from the help pages testPoints[All, {"id" -> e, "x" -> f, "y" -> g, "z" -> h}] will work on 10.0.2 also its worth restarting your kernel when playing with dataset, occasionally it seems flaky and gives errors that a restart fixes. – Gordon Coale Apr 29 '15 at 08:48
  • 1
    @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. – halirutan Apr 29 '15 at 08:50
  • Thanks very much! I tried this in 10.1 and it works without issue. I have submitted the example to Wolfram as a bug report (but they are most likely already aware of this). – Doug Kimzey Apr 29 '15 at 13:30