I continue to find functional programming somewhat confusing.
I have lists of depths with unit, with differing units drawn from columns of a database. I wish to simply convert these lists of depths so that all elements of the list are, with each element given in meters. Ignoring non-essential aspects, I simplify the problem below.
As an example input, I use the following list:
result[All,13]] = {"50 ft", "8 ft", "16 m", "35 ft", "70 ft", "6 m", "60 ft", "90 ft", "8 ft"}
Although I naively thought that ft and m are interpretable by Quantity[result[[All,13]]], I get an "Unable to interpret unit specification ..."
Consequently, I substitute the explicit values for units and then have tried to execute a slightly overloaded function to do this:
result[[All, 13]] = StringReplace[result[[All, 13]], {"fm" -> "Fathoms", "ft" -> "Feet", "m" -> "Meters"}]
depthfun[n_ /; n == ""] := Module[{y}, y = n;];
depthfun[n_ /; n != ""] :=
Module[{x, y1, y2, z}, x = StringSplit[n]; y1 = x[[1]];
y2 = x[[2]] // InputForm;
If[y2 == "Feet", z = UnitConvert[Quantity[y1, y2], "Meters"],
If[y2 == "Fathoms", z = UnitConvert[Quantity[y1, y2], "Meters"],
z = Quantity[y1, y2]]]];
which I then attempt to Map across the list:
Map[Apply[depthfun[#] &], result[[All, 13]]]
Unfortunately, this doesn't work and simply leaves the original list unchanged. I don't understand why. Can anyone help me figure out why my function fails to perform the unit conversions? No doubt, there is a more appropriate/succinct way to do this, so alternative approaches would be instructive.
Thank you.