Re: 'Good' or 'Proper' Mathematica coding habits question

*To*: mathgroup at smc.vnet.net*Subject*: [mg62068] Re: [mg62028] 'Good' or 'Proper' Mathematica coding habits question*From*: Igor Antonio <igora at wolf-ram.com>*Date*: Thu, 10 Nov 2005 02:51:23 -0500 (EST)*Organization*: Wolfram Research, Inc.*References*: <200511090846.DAA17466@smc.vnet.net>*Reply-to*: igora at wolf-ram.com*Sender*: owner-wri-mathgroup at wolfram.com

Matt wrote: > Hello, > Due to the great help I've received from here, I've made limited > progress with Mathematica. As I continue my journey, I am trying to adopt the > philosophy: "When working with Mathematica, shy away from your long-learned > habits of using loops, and instead try a functional or rule-based > approach." To this end, I have coded up a few functions, which I'd > appreciate any feedback on, as to whether I'm breaking an inviolate > rule of Mathematica, or I'm using an egregious memory model, I'm duplicating > some functionality that already exists in Mathematica proper, etc. Please bear > in mind that I am by training and thought process an assembly and C/C++ > developer that worries about details such as whether to use > post-increment or post-increment when dealing with objects due to the > unnecessary construction of temporary objects on the stack. I want to > learn good Mathematica habits from the get-go. > That's a good idea to change your way of thinking when dealing with a functional language from the start. :-) Here's advice about the newsgroup though, please post the InputForm of your code on the newsgroup. This way we can read the code without having to copy and paste it into Mathematica. > Here are the functions with short explanations for each: > > Cell[TextData[{ > "extractNthElementFromEachSublistOfList takes as its first argument \ > a list of lists, e.g. of the form {{", <celldata stuff> I copied and pasted into Mathematica and got a cell expression error. I might have copied and pasted something wrong, but please post the InputForm of it. > Here is a table of 20 pairs of numbers. > values = Table[{Random[Real, {-2, 2}] i, Random[Real, {-2,2}] i}, {i, > 1, 20}]; > Mathematica's Help Browser is a little hard to navigate when you first start, but once you learn how it's organized, you will figure out a way to find what you want. When you want to extract data from a list based on certain conditions, your best bet is to use the functions Select or Cases. (Help Browser > List and Matrices > Element Extraction >). If you can use either one, choose Cases since it works with pattern matching as opposed to element testing. In[2]:= values = Table[{Random[Real, {-2, 2}]*i, Random[Real, {-2, 2}]*i}, {i, 1, 20}]; In[3]:= Cases[values, {a_?Negative, b_?Negative}] Out[3]= {{-0.15028289650316573, -1.9370549678832405}, {-13.809144185126259, -5.175134014000584}, {-26.68175865090242, -18.61024953825357}} > All feedback is appreciated. > > Thanks, > > Matt -- Igor C. Antonio Wolfram Research, Inc. http://www.wolfram.com To email me personally, remove the dash.

**References**:**'Good' or 'Proper' Mathematica coding habits question***From:*"Matt" <anonmous69@netscape.net>