New answers tagged

2

Just like you I am also learning algorithms mostly in python, that I am also using to learn haskell. I am fighting myself not to change variable names to long but readable ones! Here it use not using any folding or anything, just recursion. bubbleSort :: (Ord a) => [a] -> [a] bubbleSort [x] = [x] bubbleSort (x:y:xs) | x < y = x : bubbleSort (y:...


1

I tested with the cases of standard integral types (expecting true,) standard floating point types (expecting false,) user-defined types convertible to some standard integral type (expecting false.) All tests passed. You should have tested with all the other kinds of types in C++. Reference types, pointer types, enum types, nullptr_t, void, pointer-to-...


3

Your header guard is: #ifndef ANY_HPP_INCLUDED The style I most commonly see in modern code is #ifndef INCLUDED_ANY_HPP (or, just leave off the _HPP part). The reason is that technically, C and C++ reserve all uppercase names matching E[A-Z].* to the implementation, for macros like EINVAL and EPERM. Of course in practice your implementation won't have a ...


2

I see one place where you could use a C++ comment: /* Test Any Implementation */ Because this comment only spans one line, I generally prefer to use a C++ comment instead: // Test Any Implementation


3

I see some of the issues include copying the data and passing by reference or const reference. Might I suggest following the STL's method of using iterators? Your any function is essentially std::find_if using a container instead of iterators. Rewriting it to use std::find_if reveals the differences: template<typename List, typename Predicate> bool ...


2

Errorsatz already mentioned the Functor type, but I'd like to argue for Functor&&. As for testing, this is a case where you literally want to test edge cases. I.e. where the first or only the last element has the desired property. Two additional special cases to test would be a single element, matching or not matching. (Zero-one-many rule of thumb; ...


15

I think you did a pretty good job as a beginner. In addition to what Errorsatz said: Consider making element a const reference to prevent unnecessary copying: const auto& element : elements You missed #include <string> in the test file. std::endl flushes the buffer and causes performance degradation; use \n instead unless you need the flushing ...


30

The algorithm looks correct. Regarding the function signature, I'd make three changes: You're not modifying elements, so take it by constant reference rather than reference. You don't need to specify a pointer to Function, Function is already a template parameter, and non-pointers could be valid (ex: a class with a () operator). "callback" isn't a ...


4

Is it worth parallelising? When you are parallelizing code you have to ask yourself if it's worth doing that. Parallel code doesn't magically give a speedup, because there are various things that can actually slow you down when using parallelism, for example: Spawning and waiting for threads itself costs some time. Threads might be fighting for resources, ...


Top 50 recent answers are included