Difference between revisions of "Functional C++"

From Tuxamito
Jump to: navigation, search
Line 24: Line 24:
  
 
== map ==
 
== map ==
 +
 +
<syntaxhighlight lang="cpp">
 +
template<class InputIterator, class Function>
 +
  Function for_each(InputIterator first, InputIterator last, Function fn)
 +
{
 +
  while (first!=last) {
 +
    fn (*first);
 +
    ++first;
 +
  }
 +
  return move(fn);
 +
}
 +
</syntaxhighlight>
 +
 +
 +
<syntaxhighlight lang="cpp">
 +
template <class InputIterator, class OutputIterator, class UnaryOperator>
 +
  OutputIterator transform (InputIterator first1, InputIterator last1,
 +
                            OutputIterator result, UnaryOperator op)
 +
{
 +
  while (first1 != last1) {
 +
    *result = op(*first1);  // or: *result=binary_op(*first1,*first2++);
 +
    ++result; ++first1;
 +
  }
 +
  return result;
 +
}
 +
</syntaxhighlight>
  
 
== foldl ==
 
== foldl ==
 +
 +
<syntaxhighlight lang="cpp">
 +
template <class InputIterator, class T>
 +
  T accumulate (InputIterator first, InputIterator last, T init)
 +
{
 +
  while (first!=last) {
 +
    init = init + *first;  // or: init=binary_op(init,*first) for the binary_op version
 +
    ++first;
 +
  }
 +
  return init;
 +
}
 +
</syntaxhighlight>
  
 
== foldr ==
 
== foldr ==
 +
 +
user rbegin and rend
 +
 +
<syntaxhighlight lang="cpp">
 +
template <class InputIterator, class T>
 +
  T accumulate (InputIterator first, InputIterator last, T init)
 +
{
 +
  while (first!=last) {
 +
    init = init + *first;  // or: init=binary_op(init,*first) for the binary_op version
 +
    ++first;
 +
  }
 +
  return init;
 +
}
 +
</syntaxhighlight>
  
 
== filter ==
 
== filter ==
 +
 +
<syntaxhighlight lang="cpp">
 +
template <class InputIterator, class OutputIterator, class UnaryPredicate>
 +
  OutputIterator copy_if (InputIterator first, InputIterator last,
 +
                          OutputIterator result, UnaryPredicate pred)
 +
{
 +
  while (first!=last) {
 +
    if (pred(*first)) {
 +
      *result = *first;
 +
      ++result;
 +
    }
 +
    ++first;
 +
  }
 +
  return result;
 +
}
 +
</syntaxhighlight>
  
 
== replicate ==
 
== replicate ==
Line 42: Line 110:
 
   }
 
   }
 
   return first;
 
   return first;
 +
}
 +
</syntaxhighlight>
 +
 +
 +
<syntaxhighlight lang="cpp">
 +
template <class ForwardIterator, class T>
 +
  void fill (ForwardIterator first, ForwardIterator last, const T& val)
 +
{
 +
  while (first != last) {
 +
    *first = val;
 +
    ++first;
 +
  }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 09:58, 11 December 2014


Function Equivalence
Haskell C++
map for_each
foldl accumulate
foldr accumulate
filter copy_if
replicate fill_n

map

template<class InputIterator, class Function>
  Function for_each(InputIterator first, InputIterator last, Function fn)
{
  while (first!=last) {
    fn (*first);
    ++first;
  }
  return move(fn);
}


template <class InputIterator, class OutputIterator, class UnaryOperator>
  OutputIterator transform (InputIterator first1, InputIterator last1,
                            OutputIterator result, UnaryOperator op)
{
  while (first1 != last1) {
    *result = op(*first1);  // or: *result=binary_op(*first1,*first2++);
    ++result; ++first1;
  }
  return result;
}

foldl

template <class InputIterator, class T>
   T accumulate (InputIterator first, InputIterator last, T init)
{
  while (first!=last) {
    init = init + *first;  // or: init=binary_op(init,*first) for the binary_op version
    ++first;
  }
  return init;
}

foldr

user rbegin and rend

template <class InputIterator, class T>
   T accumulate (InputIterator first, InputIterator last, T init)
{
  while (first!=last) {
    init = init + *first;  // or: init=binary_op(init,*first) for the binary_op version
    ++first;
  }
  return init;
}

filter

template <class InputIterator, class OutputIterator, class UnaryPredicate>
  OutputIterator copy_if (InputIterator first, InputIterator last,
                          OutputIterator result, UnaryPredicate pred)
{
  while (first!=last) {
    if (pred(*first)) {
      *result = *first;
      ++result;
    }
    ++first;
  }
  return result;
}

replicate

template <class OutputIterator, class Size, class T>
  OutputIterator fill_n (OutputIterator first, Size n, const T& val)
{
  while (n>0) {
    *first = val;
    ++first; --n;
  }
  return first;
}


template <class ForwardIterator, class T>
  void fill (ForwardIterator first, ForwardIterator last, const T& val)
{
  while (first != last) {
    *first = val;
    ++first;
  }
}