lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: reading material?


From: Douglas A Linhardt
Subject: Re: reading material?
Date: Mon, 22 Mar 2004 12:51:12 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0 (CK-LucentTPES)

Agreed.  We're off target.  And I'm not trying to start a flame war.  I really
don't want to start an argument.  I just want to I promise not to post any more
to this thread (unless, of course I change my mind ;) ).

> 
> We're going off-topic here, but I'm pretty sure that you know as well
> as I that "Hi" and "there" are (char const*). The STL is better than
> nothing, but to me a library does not qualify as built-in.
> 

Yes, "Hi" and "there" are char const*, but in my example, the strings were x and
y.
        string x = "Hi ";
        string y = "there.";
        cout << x + y << endl;          // concatenation

The string objects have all the expected member functions, like length,
substring access, the == operator, the < operator, etc.

And the STL is fully standardized and an integral part of the new C++
definition.  It's kind of like saying the base C language doesn't support output
because printf is supported in the standard library.  If you think this, then at
least you're consistent, and we know where our differences are.

> First-class means first-class citizens, that is, objects of that type
> can be used in all language constructs just like other objects.  For
> example, a function that returns a function in Scheme is natural,
> 
>  (define (iterate func k)
>    "Produce the function x -> FUNC(FUNC .. (x) .. ) "
>    (if (> k 0)
>        (lambda (x) (func ((iterate func (1- k)) x)))
>        (lambda (x) x)))
> 
>  (define to-6th-power (iterate sqr 3))
> 
> If you're suitably masochistic, then you could come up with a C++
> solution, but I doubt it would be natural or elegant. Besides, I don't
> appreciate masochism when it comes to programming.

Actually, this will calculate x ^ (2 ^ 3), so the procedure should be named
to-eighth-power, but be that as it may...

The C++ solution is both natural and elegant.  Two example solutions follow, the
first not using templates, the second using templates.  By the way, my favorite
reference for using the Standard Template library, as well as things like these
functor classes, is Nicolai M. Josuttis, "The C++ Standard Library", 1999.


Example 1:
==================================================

#include <iostream>
using namespace std;

class Function
{
public:
        virtual int operator() (int x) = 0;
};

class Square : public Function
{
public:
        virtual int operator() (int x) { return x * x; }
};

class Iterate
{
public:
        Iterate (Function& f, unsigned int i) : func(f), iters(i) { }

        int operator() (int operand)
        {
                for (int i = 0; i < iters; ++i)
                {
                        operand = func(operand);
                }
                return operand;
        }

private:
        Function& func;
        int iters;
};

int main()
{
        Square s;
        Iterate tripleSquared(s, 3);
        cout << tripleSquared(2) << endl;
        return 0;
}

==========================================================
Example 2:
==========================================================

#include <iostream>
using namespace std;

class Square
{
public:
        virtual int operator() (int x) { return x * x; }
};

template <typename FUNCTOR, typename TYPE>
class Iterate
{
public:
        Iterate (unsigned int i) : iters(i) { }

        TYPE operator() (TYPE operand)
        {
                FUNCTOR func;
                for (int i = 0; i < iters; ++i)
                {
                        operand = func(operand);
                }
                return operand;
        }
private:
        int iters;
};

int main()
{
        Iterate<Square, int> tripleSquared(3);
        cout << tripleSquared(2) << endl;
        return 0;
}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]