Author Topic: Generic Programming  (Read 1954 times)

0 Members and 1 Guest are viewing this topic.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Re: Generic Programming
« Reply #15 on: April 20, 2020, 02:29:34 am »
Hello Ibra!
I hope you are able to keep your head together and your wits about you.
By chimera, do you mean "an imaginary monster compounded of incongruous parts" or "an illusion or fabrication of the mind" or simply "an unrealizable dream"?

I've been using the iterators for traversing vectors, maps, lists, etc ... utilizing the begin() and end() member functions.  Most of the "fun" has been exploring tricky parts with gdb (with added Python interface .gdbinit with STL Views).

It is within the gdb that I am able to make some sense of it.  The iterators are the new incarnation of "pointers" - a phenomenon which appeals to me for they answer the problem of traversing through a sequence.  Using gdb, you can get a little creative (and even go mad in a fun way), just satisfying your curisoity:

Iterators: how to display the item the iterator points at: print *(iter._M_current)
_______________________________________________________________________

Over the past week or so, I was using the <regex> library for text manipulation and am now going over "Numerics," a topic I am especially interested in.   I am trying to learn enough so as to rework exercises from old 1960's and 1980's texts with many novel exercises which I wish to rework using modern C++ (STL and generic programming) ... so I work in phases, a couple weeks concentrating on modern C++ and using the STL, and then a week or so on the Analysis project ...

With -std=c++17, you can access the elements in containers in a rather elegant manner using the STL containers, even when those containers hold elements that are your own homegrown types.

suppose you had a vector of pairs<int, string>:

std::vector<std::pair<int, std::string>> pairs;

After you fill the vector with pairs of {int, string}, you can access as follows:

int count = 0;
for (const auto& [i, s] : pairs) {
    ++count;
    std::cout << "Pair #" << count << ": The integer is " << i << ", and the string is " << s << '\n';
}

Without that, you could just as easily write:

int count = 0;
for (const auto& p : pairs) {
    ++count;
    std::cout << "Pair #" << count << ": The integer is " << p.first << ", and the string is " << p.second << '\n';
}

The vectors are very useful.  I'll be checking out a Matrix library, although I am not sure I will want to rework many of the Linear Algebra related programs I have already written unless there was a huge benefit, that is, as a learning experience ...  Actually, come to think of it, even though it was a few years ago, I did build a few programs for solving linear equations (Ax = b) using STL <vector>.  It builds the matrices from scratch as a vector<vector<Fraction> > so that the results (and the work) were printed in rational form, whcih was cool to work side by side with paper and pencil.

As for Micro$oft and Money-Oriented Programming (or even Spying-Oriented software), yes, I'm sure the merry men are continuing to do as their told to please thier masters. 

The Standard Template Library is worth tinkering with, at least with vectors.

I don't know what even motivates me to forge ahead.  What else to do but peck away ... I don't want to be entertained.  I find that sometimes I can catch these moods while working "in my own little world" where I actaully "lose myself," that is, I momentarily forget who I am ... and just become one with the task, the exploring ... the learning.

« Last Edit: April 20, 2020, 09:54:58 pm by Der Steppenwolf »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Re: Generic Programming
« Reply #16 on: April 20, 2020, 04:52:17 pm »
On second thought, a resounding yes to whether it would be educationally beneficial to rework the Linear Algebra (solving Ax = b) code using the STL-oriented makeshift Matrix found in the Stroustrup text, and then the main code would become trivial, easy for a human being to follow if they are versed in the nomenclature of math textbooks.  All the details would be found in Matrix.h and MatrixIO.h, but they are worth studying since they show how the ostream<< operator is overloaded and what not.

This would replace the homegrown one I made myself, as I would wish to make it a little better as long as I would be going through the "trouble," that is, I would offer the menu to use "real" (floating point approximations) or rational (fractions).  The one I made uses my own Fraction class, the one I've been improving since 1999 after a special semester at the community college where the professor (Patricia Hines - I remember the name clearly) had us design a rather robust Fraction class (user-defined type of data structure).  I had no idea how precious that code would become to my sanity throughout my life, and it easily survived the transmutation into generic programming.  In fact, it has Pythagorean "quasi-religious" significance to me.  It has improved over the decades, but the core concepts of operations on and with fractions were there from the start. 

I agree with Pythagoras that rational numbers seem more beautiful.  Other times, maybe the decimal draction form is better.  It depends.  That machines are not capable of representing so-called "real" numbers may be a symptom of what certain radical mathematicians (Norman Wildberger |---> "Divine Proportions")  find to be rather fishy in Denmark.  (another can of worms)

The bottom line is that I do plan to eventually use a Matrix library rather than the vector<vector<Fraction>> approach.  I will give choice of double or Fraction, thereby utilizing the templates (the "modern" part of modern C++ is writing code using libraries that make use of the way containers and algorithms work together).  I am mostly concerned with beautifying the process, but I do not feel the code I have already created is inferior.  In fact, it will help me understand what parts are hidden in the libraries.  For instance, I will have to learn how to make use of scale() and slice(), not to mention apply().

I will have to revisit classic ideas of vectors and matrices.  So, whenever I take a break from "the math books" I can look forward to this "linear algebra project" as far as programming discipline goes.  In my own little world, this is really good news.  I mean, I appreciate that I have worked patiently through all previous 23 chapters exercises, even though I knew that chapter 24 (Numerics) was really mostly what I was interested in.  I wanted to carefully consider how the creator of C++ viewed certain mechanisms, and it has been worthwhile working through beyond chapter 17, or whatever chapter the universities are reaching in one semester.  They would have to skip huge chunks of the "graphics" chapters and jump right into the less error-prone, far more fundamental material.  I have been patiently resisting the dersire to jump directly to this chapter.

Thanks for asking.
« Last Edit: April 20, 2020, 09:42:32 pm by Der Steppenwolf »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Holden

  • { ∅, { ∅ } }
  • Posts: 5070
  • Hentrichian Philosophical Pessimist
Re: Generic Programming
« Reply #17 on: April 21, 2020, 01:54:34 pm »
Hello Mr.Ibra,
Its nice to see you here.I hope you are safe.

Take care.
La Tristesse Durera Toujours                                  (The Sadness Lasts Forever ...)
-van Gogh.

Ibra

  • Philosopher of the Void
  • Posts: 132
Re: Generic Programming
« Reply #18 on: April 22, 2020, 01:23:24 am »
Thanks Hentrich and Holden,

I am good so far. I hope you are safe too. 

I left my work on January. I worked at night shift for 8 years or more and that deteriorated my health considerably but I didn't mind that, on last December i was ordered by the masters to go into the day shift, I had argument every day with ass-kissing managers  and then i left . well "Hell is the other people" as Sartre? said.

I already missed the Syrian war train, I hope I can ride Corona train to the graveyard. I am already at my wit's end.

 I downloaded "C++ tour 2nd edition" book and I am fiddling with GCC compiler(MinGW-64) as time passing activity, at least we have some control about bits and bytes.

Quote from: Hentrich
After you fill the vector with pairs of {int, string}, you can access as follows:

int count = 0;
for (const auto& [i, s] : pairs) {
    ++count;
    std::cout << "Pair #" << count << ": The integer is " << i << ", and the string is " << s << '\n';
}

Without that, you could just as easily write:

int count = 0;
for (const auto& p : pairs) {
    ++count;
    std::cout << "Pair #" << count << ": The integer is " << p.first << ", and the string is " << p.second << '\n';
}

That is neat trick, reminds me of python unpacking techniques.

take care,
Suffering is the only fruit of human race