: ) wonderful world ( :

the metasyntactic variable

Archive for August 2006

without comments

Written by grault

August 30, 2006 - 7:37 pm at August 30, 2006 - 7:37 pm

without comments

Written by grault

August 30, 2006 - 7:31 pm at August 30, 2006 - 7:31 pm

without comments

On 08/11/06 (mm/dd/yy :) the wheel was reinvented:

int start = 3;
std::list<int> path = listiniter(start);
std::list<int> dirs = listiniter<int>(4, 5);

std::transform
(dirs.begin(),
 dirs.end(),
 path.begin(),
 std::back_inserter(path),
 std::plus<int>());

std::list<int>::iterator i = path.begin();
assert(*i==3); ++i;
assert(*i==7); ++i;
assert(*i==12); ++i;
assert(i==path.end());

Written by grault

August 17, 2006 - 10:40 am at August 17, 2006 - 10:40 am

without comments

Boost rocks.

#define PUSHER(z, n, data) data .push_back(x##n);

#define LISTINITER(z, n, data)                \
    template<class T>                         \
    inline std::list<T>                       \
    listiniter(BOOST_PP_ENUM_PARAMS(n, T x))  \
    {                                         \
       std::list<T> ret;                      \
       BOOST_PP_REPEAT(n, PUSHER, ret)        \
       return ret;                            \
    }

BOOST_PP_REPEAT(31, LISTINITER, _)

Written by grault

August 17, 2006 - 10:01 am at August 17, 2006 - 10:01 am

without comments

Though it can be implemented in c++ lightly.

#include <list>
#include <functional>

...

template<class T, class C, class F&gt
std::list<T&gt
path_accum(std::list<T&gt start,
           C comb,
           std::list<F&gt list)
{
  while(list.size()>0)
  {
    start.push_back(comb(start.back(),list.front()));
    list.pop_front();
  }

  return start;
}

...

std::list<int&gt start;
start.push_back(3);

std::list<int&gt list;
list.push_back(4);
list.push_back(5);

std::list<int&gt ret = path_accum(start,std::plus<int&gt,list);
std::list<int&gt::const_iterator i=ret.begin();
assert(*i==3); ++i;
assert(*i==7); ++i;
assert(*i==12); ++i;
assert(i==ret.end());

Written by grault

August 11, 2006 - 4:01 pm at August 11, 2006 - 4:01 pm

without comments

One can use previous 'accum to compute paths, when there are move-vectors in list. In this case you can easily inject the lambda used before into the main algorithm as shown below.

(defun path-accum (start comb list)
  (if (null list)
      start
    (path-accum (append start
                        (list (funcall comb
                                       (car (last start))
                                       (car list))))
                comb
                (cdr list))))

(path-accum '(3) '+ '(4 5))

=> (3 7 12)

Written by grault

August 11, 2006 - 3:36 pm at August 11, 2006 - 3:36 pm

without comments

Nice.

(defun accum (start comb list)
  (if (null list)
      start
    (accum (funcall comb start (car list))
           comb
           (cdr list))))

(accum '(3)
       (lambda (start new)
         (append start
                 (list (+ new
                          (car (last start))))))
       '(4 5))

=> (3 7 12)

Written by grault

August 11, 2006 - 3:24 pm at August 11, 2006 - 3:24 pm