[Rcpp-commits] r1636 - pkg/Rcpp/inst/doc/Rcpp-sugar

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jun 19 20:48:48 CEST 2010


Author: romain
Date: 2010-06-19 20:48:47 +0200 (Sat, 19 Jun 2010)
New Revision: 1636

Modified:
   pkg/Rcpp/inst/doc/Rcpp-sugar/Rcpp-sugar.Rnw
Log:
docment sapply and lapply

Modified: pkg/Rcpp/inst/doc/Rcpp-sugar/Rcpp-sugar.Rnw
===================================================================
--- pkg/Rcpp/inst/doc/Rcpp-sugar/Rcpp-sugar.Rnw	2010-06-19 18:07:21 UTC (rev 1635)
+++ pkg/Rcpp/inst/doc/Rcpp-sugar/Rcpp-sugar.Rnw	2010-06-19 18:48:47 UTC (rev 1636)
@@ -336,11 +336,64 @@
 pmin( x*x, 2 )
 @
 
+\subsubsection{ifelse}
 
+Given a logical sugar expression and either :
+\begin{itemize}
+\item two compatible sugar expression (same type, same size)
+\item one sugar expression and one compatible primitive
+\end{itemize}
+\texttt{ifelse} expands to a sugar expression whose i\textsuperscript{th}
+element is the i\textsuperscript{th} element of the first expression 
+if the i\textsuperscript{th} element of the condition expands to \texttt{TRUE}
+or the i\textsuperscript{th} of the second expression if 
+the i\textsuperscript{th} element of the condition expands to \texttt{FALSE}, 
+or the appropriate missing value otherwise.
+
+<<lang=cpp>>=
+IntegerVector x ;
+IntegerVector y ;
+
+ifelse( x < y, x, (x+y)*y )
+ifelse( x > y, x, 2 )
+@
+
 \subsubsection{sapply}
+
+\texttt{sapply} applies a \proglang{C++} function to each element
+of the given expression to create a new expression. The type of the
+resulting expression is deduced by the compiler from the result type of
+the function.
+
+The function can be a free \proglang{C++} function such as the overload 
+generated by the template function below: 
+
+<<lang=cpp>>=
+template <typename T>
+T square( const T& x){
+    return x * x ;
+}
+sapply( seq_len(10), square<int> ) ;
+@
+
+Alternatively, the function can be a functor whose type has a nested type
+called \texttt{result\_type}
+
+<<lang=cpp>>=
+template <typename T>
+struct square : std::unary_function<T,T> {
+    T operator()(const T& x){
+        return x * x ;
+    }
+}
+sapply( seq_len(10), square<int>() ) ;
+@
+
 \subsubsection{lapply}
-\subsubsection{ifelse}
 
+\texttt{lapply} is similar to \texttt{sapply} except that the result is
+allways an list expression (an expression of type \texttt{VECSXP}. 
+
 \section{Performance}
 \label{sec:performance}
 



More information about the Rcpp-commits mailing list