Skip to main content
23-Emerald V
June 13, 2012
Question

I'm dithering - which seq should I use?

  • June 13, 2012
  • 2 replies
  • 16913 views

I'm rewriting a number of functions whilst sorting out my toolbox of utility functions and I've hit a slight quandry with regard to one of the most regularly used ones, seq.

seq(n) was originally written to generate simple integer sequences, primarily for use as vector indices and as test data. Consequently, I thought about its input argument, n, in terms of the number of elements and mentally regarded it as an integer with no thought for other interpretations (apart from arrays and range definitions, which seq passed to my other primary tool, vec).

However, I have several variants of seq (often with additional arguments) that allow me to generate more complex sequences, including string sequences. The downside is that it necessitates a change in world view to looking at n as the upper limit of a sequence (and implied lower limit for negative numbers). This will involve a further rewrite of several functions to accomodate the effect this has upon zero-based indices (positive n will now give a vector of n+1 elements). I also get a different response for 0 and negative numbers, which previously generated errors (on the not entirely unreasonable basis that you shouldn't have a vector with zero or a negative amount elements) but now return valid sequences (if, in the case of 0, you can call a one-element array a sequence).

My goal in tool generation is to keep the tools small and simple with as few arguments as possible, and preferably with short meaningful names.

The question is does this new form of seq look worth trouble? It's larger than I like tools to be (but I do have some larger ones), is fairly complex in its treatment of its argument and, although I'm happyish with most of the interpretations, they may not be so for others or there may be better ones.

All comments gratefully received.

Thank you.

Stuart

2 replies

19-Tanzanite
June 13, 2012

To be honest, I have never found a use for seq. I prefer to use vec, or one of the many variations thereof. vec(0..4) may require a very small amount of additional typing, but it's much clearer what is being asked for than seq(4) (especially to someone that's not familiar with Mathcad). I might find a use for a more flexible seq function though.

12-Amethyst
June 14, 2012

I have created similar functions for my own libraries, but found limited use for extremely generic versions. The simple versions cover 99+% of the need, so I have come to keep the def's on the simple side. Also, with my fading, old memory, it's easier to remember what simple functions do. One of my main decisions in creating a function is its name; how to trade off my laziness vs. utility - i.e, really short, cryptic names vs. longer, descriptive names (LSMFT vs. Least_Squares_Modifed_Fourier_Transform).

In the end, however many comments you may get (worth what you pay for them), if you're the user, then go for what makes best sense for you.

Lou

P.S. I remember someone from the old collab with a similar name. Your grandfather perhaps? He may remember that LSMFT was an acronym used in an old cigarette marketing campaign.

23-Emerald V
June 15, 2012

Lou Poulo wrote:

I have created similar functions for my own libraries, but found limited use for extremely generic versions. The simple versions cover 99+% of the need, so I have come to keep the def's on the simple side. Also, with my fading, old memory, it's easier to remember what simple functions do. One of my main decisions in creating a function is its name; how to trade off my laziness vs. utility - i.e, really short, cryptic names vs. longer, descriptive names (LSMFT vs. Least_Squares_Modifed_Fourier_Transform).

In the end, however many comments you may get (worth what you pay for them), if you're the user, then go for what makes best sense for you.

Hey, ho. <sigh> That's at the core of my quandry. I like simple routines and there is a benefit to having tools do one job very well; I'm quite fond of my vec function because it's short, recursive and turns just about anything into a vector. I also keep a copy of Tom Gutman's ravel around, but I haven't had the need to use it for quite some time ... years, in fact, gosh tempus really does fugit; I have another function, flatten, that I use when I want to control the how deep I go with the conversion on a nested array.

seq(n) started out with a simple need - as a shorthand for vec(0..n-1) to generate a sequence of n successive integers and let me convert scalars into vectors as well; it also has, in my mind, a more mnemonic name for its purpose.

However, I also keep hitting the need for more complex sequences and, this must the Scot in me coming out, I hate to waste the opportunities that other values of n might present in meeting these needs. Overloading argument interpretations is somewhat prevalent in other applications, such as Mathematica, and in Mathcad is the most effective way of getting round the lack of optional arguments. So, carrying on this grand tradition, with usage, the name 'seq' began to become more suggestive of the sequence 0..n or -n..n or "a".."g" and that's how it evolved. Simultaneously, the auxiliary function idx, which took a vector argument and generated its corresponding index vector, evolved to deal with 2-dimensional arrays and also now generates 0..n-1 for a scalar input, so it kind of takes over the original ecological niche that seq was designed to fill.

I'm leaning towards the new definitions, but ....

I suppose the real worry nagging at the back of my mind is the infamous 'requirement creep' syndrome that tries to make an all-singing, all-dancing, all-weather weapons platform out of what was originally a daylight, VFR trainer.

P.S. I remember someone from the old collab with a similar name. Your grandfather perhaps?

Not unless I invent a time machine and play around with a few paradoxes.

I think it was probably me, especially if you remember the dog-fights over the skies of Drummondville against the Giraud Mk I.

P.S. .. He may remember that LSMFT was an acronym used in an old cigarette marketing campaign.

Being Brits, I'm not sure either of my grandfathers would have been au fait with Lucky Strikes ... Woodbines or Players is more like it.

Stuart

12-Amethyst
June 15, 2012

I remember the Mk I; kept things from getting dull.

As far as how generic to write routines, I tend to keep the initial versions relatively simple, and expand when a need arises. Once I need to make revisions, I tend to add a bit more generality at that time than I actually need, trying to head off future revisions. As you might guess, this is not always successful.

My advice on this topic: worry less and enjoy drinking more (and not tea).

The attached was always one of my favorites (available in many versions) on the topic of mission creep.

Lou

the swing.jpg