{"action":"opened","issue":{"url":"https://api.github.com/repos/RcppCore/Rcpp/issues/426","labels_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/426/labels{/name}","comments_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/426/comments","events_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/426/events","html_url":"https://github.com/RcppCore/Rcpp/issues/426","id":127322915,"number":426,"title":"Rcpp::algorithm proposal","user":{"login":"dcdillon","id":2372155,"avatar_url":"https://avatars.githubusercontent.com/u/2372155?v=3","gravatar_id":"","url":"https://api.github.com/users/dcdillon","html_url":"https://github.com/dcdillon","followers_url":"https://api.github.com/users/dcdillon/followers","following_url":"https://api.github.com/users/dcdillon/following{/other_user}","gists_url":"https://api.github.com/users/dcdillon/gists{/gist_id}","starred_url":"https://api.github.com/users/dcdillon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dcdillon/subscriptions","organizations_url":"https://api.github.com/users/dcdillon/orgs","repos_url":"https://api.github.com/users/dcdillon/repos","events_url":"https://api.github.com/users/dcdillon/events{/privacy}","received_events_url":"https://api.github.com/users/dcdillon/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-01-18T22:25:30Z","updated_at":"2016-01-18T22:25:30Z","closed_at":null,"body":"A thought about how we could give the end user some more flexibility with respect to sugar (and additionally reimplement sugar with the more flexible functions).  This is a proposal for an addition and not for changing any existing functionality.  It goes something like this...If we add an include file that looks something like this:\r\n\r\n```c++\r\nnamespace Rcpp\r\n{\r\nnamespace algorithm\r\n{\r\n    template< typename InputIterator >\r\n    typename std::iterator_traits< InputIterator >::value_type sum(InputIterator begin, InputIterator end)\r\n    {\r\n        typename std::iterator_traits< InputIterator >::value_type start = *begin++;\r\n\r\n        while (begin != end)\r\n        {\r\n                start += *begin++;\r\n        }\r\n\r\n        return start;\r\n    }\r\n\r\n    template< typename InputIterator >\r\n    typename std::iterator_traits< InputIterator >::value_type prod(InputIterator begin, InputIterator end)\r\n    {\r\n        typename std::iterator_traits< InputIterator >::value_type start = *begin++;\r\n\r\n        while (begin != end)\r\n        {\r\n                start *= *begin++;\r\n        }\r\n\r\n        return start;\r\n    }\r\n\r\n    template< typename InputIterator, typename OutputIterator >\r\n    void log(InputIterator begin, InputIterator end, OutputIterator out)\r\n\r\n    {\r\n        while (begin != end)\r\n        {\r\n                *out = std::log(*begin++);\r\n                ++out;\r\n        }\r\n    }\r\n}\r\n}\r\n```\r\n\r\nThen we can write functions that look like this:\r\n\r\n```R\r\n#include <Rcpp.h>\r\n\r\n// [[Rcpp::export]]\r\ndouble mySum(Rcpp::NumericVector v, int begin, int end)\r\n{\r\n        return Rcpp::algorithm::sum(v.begin() + (begin - 1), v.begin() + end);\r\n}\r\n\r\n// [[Rcpp::export]]\r\ndouble myProd(Rcpp::NumericVector v, int begin, int end)\r\n{\r\n        return Rcpp::algorithm::prod(v.begin() + (begin - 1), v.begin() + end);\r\n}\r\n\r\n// [[Rcpp::export]]\r\nRcpp::NumericVector myLog(Rcpp::NumericVector v)\r\n{\r\n        Rcpp::NumericVector x = Rcpp::clone(v);\r\n        Rcpp::algorithm::log(v.begin(), v.end(), x.begin());\r\n        return x;\r\n}\r\n```\r\n\r\nWhich produce output like this:\r\n\r\n```\r\n> library(Rcpp)\r\n> sourceCpp(\"test.cpp\")\r\n> mySum(1:4, 1, 3)\r\n[1] 6\r\n> myProd(1:4, 2, 4)\r\n[1] 24\r\n> myLog(1:4)\r\n[1] 0.0000000 0.6931472 1.0986123 1.3862944\r\n> \r\n```\r\n\r\nCurrently, most sugar functions only work on stuff that IS a `Rcpp::Vector`.  This excludes, for instance, `Rcpp::Matrix::Row`.  Creating range based algorithms would simply give more flexibility to the end user without removing any of the current functionality."}}