Some upcoming C++23 features that will make it even more convenient to use ranges: to, zip, slide, stride, repeat, adjacent, pairwise, cartesian_product.all: though type conversions from containers to ranges are usually implicit, it takes a container and takes everything in that container.elements: if we have a range of tuple-like elements, this takes the $$$n$$$-th members of each of these tuples (where $$$N$$$ is a compile-time constant). queue.clear() It removes all the values from the deque (clears/empties the deque) leaving it with a length of 0.Similarly, values returns a range of the values. keys, values: keys takes a range whose elements are pair-like and returns a range of their first elements (so if we have (key, value) pairs, it will return keys), similar to.counted: This takes $$$n$$$ elements of a range starting from an iterator.drop: This takes everything but the first $$$n$$$ elements of a range (unless you run out of elements by then).take: This takes the first $$$n$$$ elements of a range (unless you run out of elements by then).reverse: This works similarly to Python's reversed(). iota: This works similarly to Python's range() but without the stride.C++23 has a feature R::to(range) that will construct a container out of a range, and you could even do range | R::to(). Std::cin.tie(nullptr)->sync_with_stdio(false) įor (int i = 0 i a(r.begin(), r.end()). Have you ever found yourself using a vector like a stack just because you have to also access the last few positions, or writing a sliding window code using a deque (where the size might be variable, but you want to access an element at some offset from the end)?Ĭhances are that you have written code like this: while (stk.size() >= 3 & stk + stk > stk) įor (const auto word : std::views::split(words, delim)) Here's how you can write code more smartly and succinctly in C++(20): Negative indexing with end People often say C++ is much more cumbersome to write than any sane language should be, but I think that is mainly due to lack of knowledge about how to write modern C++ code. > from operator import itemgetter > import itertools > with open ( 'contactlenses.csv', 'r' ) as infile. Map and then to itertools.chain? Probably not the best idea! ButĪ list comprehension inside another list comprehension that is then passed to For example, I think listĬomprehensions can be very readable and rather easy to debug and maintain. Short and smart your code is is outweighing how readable and maintainable it Use your best judgment and if it feels like how But IĬan definitely see how they can be taken too far, resulting in hard to debug, My say on it is that as farĪs I can see, none of the items below are inherently harder to debug. Techniques below leading to harder to debug code. There wasĪlso a very interesting discussion about the possibility of some of the _iterable, and dictionary comprehensions. Updated the list below to include many of the improvements suggested, and addedĪ few new items based on suggestions that made me have one of those "Cool! Iĭidn't know you could do that!" moments. With many readers suggesting great alternatives and improvements. Reddit, Hacker News, and in the comments below, March 8th, 2014 update: This article generated a lot of good discussion on Improvements or additions and submit pull requests. Readers to make improvements or additions through pull requests. Great suggestion of turning this article into a GitHub repository to allow March 14th, 2014 update: Roy Keyes made a Is since that's the original title the article was shared as, making it more Number 30 in the title is no longer accurate. The list is very roughly ordered by difficulty, with the easier and moreĬommonly known language features and tricks appearing first.Īpril 9th, 2014 update: As you can see the article has been growing withĬurrently 38 items in it, mostly thanks to comments from readers. Should be clear enough to allow you to use Google for more information on it. If something still doesn't make sense after looking at the examples, the title Some of them might still appear cryptic depending on your familiarity level. While I tried my best to make the examples clear, Python, or just brand new to programming, then you might find quite a few ofĮach trick or language feature is demonstrated only through examples, If you are a C, C++ or Java programmer who is learning If you are an experienced Python programmer, chancesĪre you already know most of these, though you might still find a few that youĭidn't know about. This post is part of that list,Īfter some cleaning up. Understood it and then added it to the list. "Cool! I didn't know you could do that!" I experimented with it until I Stack Overflow, in open source software, etc.) that made me think deque (iterable, maxlen) ¶ Returns a new deque object initialized left-to-right (using append()) with data from iterable. Any time I saw a piece of code (in an example, on Since I started learning Python, I decided to maintain an often
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |