- Way of writing "reduce" - 3 Updates
- "Year 2038 problem is still alive and well" by CookiePLMonster - 3 Updates
| Malcolm McLean <malcolm.arthur.mclean@gmail.com>: Mar 04 11:27AM -0800 "reduce" takes a collection of objects, and applies a binary operation to pairs that returns an objet of the same type, until only one object remains. Here's my attempt. #include <iostream> #include <vector> template<class Iterator, typename func_t> typename Iterator::value_type reduce(Iterator start, Iterator end, func_t op ) { typename Iterator::value_type answer = *start; start++; while (start != end) { answer = op(answer, *start); start++; } return answer; } int main() { const std::vector<double> array = { 1, 2, 3, 4}; double v = reduce(array.begin(), array.end(), [](double a, double b){return a < b ? a : b;}); std::cout << "Result " << v << "\n"; return 0; } Can this be improved?? |
| Bonita Montero <Bonita.Montero@gmail.com>: Mar 04 09:31PM +0100 Am 04.03.2022 um 20:27 schrieb Malcolm McLean: > template<class Iterator, typename func_t> > typename Iterator::value_type reduce(Iterator start, Iterator end, func_t op ) typename std::iterator_traits<Iterator>::value_type > { > typename Iterator::value_type answer = *start; typename std::iterator_traits<Iterator>::value_type So this works also with pointers. |
| Cholo Lennon <chololennon@hotmail.com>: Mar 04 07:11PM -0300 On 3/4/22 4:27 PM, Malcolm McLean wrote: > return 0; > } > Can this be improved?? Maybe adding the initial value. More information about the (old?) C++ implementation: https://en.cppreference.com/w/cpp/algorithm/accumulate Or the new one https://en.cppreference.com/w/cpp/algorithm/reduce Regards -- Cholo Lennon Bs.As. ARG |
| Kaz Kylheku <480-992-1380@kylheku.com>: Mar 04 07:22AM > https://cookieplmonster.github.io/2022/02/17/year-2038-problem/ > "Year 2038 problem? Wasn't that supposed to be solved once and for all > years ago? Not quite." We should adopt a cyclic three digit year for some contexts. The main problem with two digit years is that a century is too small of a time span, which causes ambiguities, such as that someone born in `19 could be a toddler, or 103 years old. A three-digit year will fix most such things. 919 versus 019. Only if you have contracts that span most of a millennium and such do you have issues. For most human activities, it's sufficient, easily understood, and free of issues. It never overflows; it doesn't accumulate more and more precision with advancing time, always requiring the same amount of storage. Arithmetic is easy. If you have two dates, d1 and d0, if d1 - d0 <= 500y, then d0 is considered in the past. For instance, year 853 is considered in the past relative to year 159. -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal |
| Juha Nieminen <nospam@thanks.invalid>: Mar 04 09:56AM > problem with two digit years is that a century is too small of a time > span, which causes ambiguities, such as that someone born in `19 could > be a toddler, or 103 years old. It's indeed curious how common it is to express dates, such as dates of birth, with a two-digit year. Some countries have, for example, date-of-birth based personal IDs, and the date is almost invariably of the form 010203 where the last two digits are the year. Given that such IDs were most commonly adopted in the earlier half of the 1900's it might have made sense back then, but once the millenium drew closer it became more and more of a problem to distinguish between newly born people and people who had their 100th birthday. For example in Finland the format of personal IDs is DDMMYY-xxxx where the xxxx is a four-character unique code consisting of letters and numbers. To solve the millenium problem, what they decided to do was that for people born in 2000 and after that - would be a + instead, which feels like quite a kludge. I think it would have been better to just have them have IDs of the form DDMMYYYY-xxxx. Would have been much clearer and nicer-looking. |
| Muttley@dastardlyhq.com: Mar 04 11:29AM On Fri, 4 Mar 2022 09:56:03 -0000 (UTC) >of birth, with a two-digit year. Some countries have, for example, >date-of-birth based personal IDs, and the date is almost invariably >of the form 010203 where the last two digits are the year. I think the issue is that in most non IT cases the century doesn't matter. People just know that if a young man gives his year of birth as 99 or 01 its not going to be 1899 or 1901. Unfortunately this indifference to the century carried over to programmers who then only used 2 digits in their code. This *might* have just been valid back in the 50s when every byte really did matter but nobody after the 60s really had any excuse to do this. At least the unix 4 byte date had a reasonable excuse that back in the early 70s when unix was designed even 32 bit machines were stuff of the future for anything except high end mainframes like the System 360 et al. |
| You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment