idiomatic

What's the idiomatic python equivalent of get() for lists?

无人久伴 提交于 2019-12-10 15:55:32
问题 Calling get(key) on a dictionary will return None by default if the key isn't present in a dictionary. What is the idiomatic equivalent for a list, such that if a list is of at least size of the passed in index the element is returned, otherwise None is returned? To rephrase, what's a more idiomatic/compact version of this function: def get(l, i): if i < len(l): return l[i] else: return None 回答1: Your implementation is Look Before You Leap-style. It's pythonic to execute the code and catch

Idiomatic way to detect sequences of x times same object in an Array in Smalltalk?

最后都变了- 提交于 2019-12-10 15:16:49
问题 What's the idiomatic way to detect sequences of x times the same object (or an object with a specific matching parameter) in an OrderedCollection or Array? E.g. does the Array contain 10 times the number 5 in a row? 回答1: Getting the sequences of repeating objects is as simple as: ({ 1. 1. 2. 2. 2. 5. 5. 3. 9. 9. 9. 9. } as: RunArray) runs => #(2 3 2 1 4) If you want to test if there is a run satisfying specific constraints, you can do something like the following: meetsConstraint := false. ({

How to count non-null elements in an iterable?

可紊 提交于 2019-12-09 08:25:26
问题 I'm looking for a better/more Pythonic solution for the following snippet count = sum(1 for e in iterable if e) 回答1: len(filter(None, iterable)) Using None as the predicate for filter just says to use the truthiness of the items. (maybe clearer would be len(filter(bool, iterable)) ) 回答2: Honestly, I can't think of a better way to do it than what you've got. Well, I guess people could argue about "better," but I think you're unlikely to find anything shorter, simpler, and clearer. 回答3: Most

is an “optionalized” pipe operator idiomatic F#

不打扰是莪最后的温柔 提交于 2019-12-08 21:08:36
问题 I like to use the pipe operator '|>' a lot. However, when mixing functions that return 'simple' values with functions that return 'Option-Typed-values', things become a bit messy e.g.: // foo: int -> int*int // bar: int*int -> bool let f (x: string) = x |> int |> foo |> bar works, but it might throw a 'System.FormatException:...' Now assume I want to fix that by making the function 'int' give an optional result: let intOption x = match System.Int32.TryParse x with | (true, x) -> Some x |

Clojure-idiomatic way to initialize a Java object

一笑奈何 提交于 2019-12-08 20:10:51
问题 I am trying to find a Clojure-idiomatic way to initialize a Java object. I have the following code: (let [url-connection (let [url-conn (java.net.HttpURLConnection.)] (doto url-conn (.setDoInput true) ; more initialization on url-conn ) url-conn)] ; use the url-connection ) but it seems very awkward. What is a better way to create the HttpURLConnection object and initialize it before using it later in the code? UPDATE : It seems that (doto ...) may come in handy here: (let [url-connection

Iterating over each element of an array, except the first one

自古美人都是妖i 提交于 2019-12-07 10:52:05
问题 What is the idiomatic Ruby way to write this code? Given an array, I would like to iterate through each element of that array, but skip the first one. I want to do this without allocating a new array. Here are two ways I've come up with, but neither feels particularly elegant. This works but seems way too verbose: arr.each_with_index do |elem, i| next if i.zero? # skip the first ... end This works but allocates a new array: arr[1..-1].each { ... } Edit/clarification: I'd like to avoid

When is `new Error()` better than `Error()`?

╄→尐↘猪︶ㄣ 提交于 2019-12-06 17:38:41
问题 The ES5 language spec clearly states that Error(foo) does the same thing as new Error(foo) . But I notice that in the wild, the longer new Error(foo) form is much more common. Is there some reason for this? Is there any situation where using new Error(foo) is preferable to using Error(foo) ? 回答1: Is there some reason for this? It's simply the habit of always calling constructors with new . Consistency rules! It's a good practice to do even when they work without new , and recommended by

dplyr version of grouping a dataframe then creating regression model on each group

送分小仙女□ 提交于 2019-12-06 08:50:44
问题 Can anyone suggest a dplyr answer to the following question? Split data.frame by country, and create linear regression model on each subset For completeness, the question and answer from the link are included below. Question For reference, here's Josh's question: I have a data.frame of data from the World Bank which looks something like this; country date BirthRate US. 4 Aruba 2011 10.584 25354.8 5 Aruba 2010 10.804 24289.1 6 Aruba 2009 11.060 24639.9 7 Aruba 2008 11.346 27549.3 8 Aruba 2007

In C++ why can't we compare iterators using > and <? [duplicate]

折月煮酒 提交于 2019-12-05 20:09:34
问题 This question already has answers here : Why is “!=” used with iterators instead of “<”? (2 answers) Closed 3 years ago . I have been asked this question which I do not really know why. If you have a pointer int * x; You can compare pointers with > and < because it stands for the memory location something like 0x0000 0x0004 0x0008 , etc. I know iterators and pointers are different, but they act in a very similar way. For example: vector<int> myVector; for(int i = 1; i < 101; i++) { myVector

With std::byte standardized, when do we use a void* and when a byte*?

て烟熏妆下的殇ゞ 提交于 2019-12-05 18:37:30
问题 C++17 will include std::byte, a type for one atomically-addressable unit of memory, having 8 bits on typical computers. Before this standardization, there is already a bit of dilemma when pointing into "raw" memory - between using char* / unsigned char* on one hand or void * on the other. Now, one of reasons for prefering void * is removed - std::byte does not have the same connotations as a char ; it's about raw memory, not characters. So, my question is: What is a good rule of thumb, for