range

Is there an equivalent of Pythons range(12) in C#?

孤人 提交于 2019-12-03 11:27:43
问题 This crops up every now and then for me: I have some C# code badly wanting the range() function available in Python. I am aware of using for (int i = 0; i < 12; i++) { // add code here } But this brakes down in functional usages, as when I want to do a Linq Sum() instead of writing the above loop. Is there any builtin? I guess I could always just roll my own with a yield or such, but this would be so handy to just have . 回答1: You're looking for the Enumerable.Range method: var mySequence =

Converting Range or DocumentFragment to string

我只是一个虾纸丫 提交于 2019-12-03 10:51:11
Is there a way to get the html string of a JavaScript Range Object in W3C compliant browsers? For example, let us say the user selects the following: Hello <b>World</b> It is possible to get "Hello World" as a string using the Range.toString() method. (In Firefox, it is also possible using the document 's getSelection method.) But I can't seem to find a way to get the inner HTML. After some searching, I've found that the range can be converted to a DocumentFragment Object. But DocumentFragments have no innerHTML property (at least in Firefox; have not tried Webkit or Opera). Which seems odd to

scala ranges versus lists performance on large collections

自作多情 提交于 2019-12-03 08:23:43
I ran a set of performance benchmarks for 10,000,000 elements, and I've discovered that the results vary greatly with each implementation. Can anybody explain why creating a Range.ByOne, results in performance that is better than a simple array of primitives, but converting that same range to a list results in even worse performance than the worse case scenario? Create 10,000,000 elements, and print out those that are modulos of 1,000,000. JVM size is always set to same min and max: -Xms?m -Xmx?m import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit._ object

Populate a list with a specific range of numbers by using LINQ

落爺英雄遲暮 提交于 2019-12-03 08:17:21
问题 In order to populate a List<int> with a range of numbers from 1 to n I can use: for (i=1; i<=n; i++) { myList.Add(i); } Is there any way to achieve the same result by using LINQ inline expressions? UPDATE Assume I have a method getMonthName(i) that given the integer returns the name of the month. Can I populate the list directly with month names somehow by using Enumerable 回答1: Enumerable.Range(1,12).Select(getMonthName); 回答2: You want to use Enumerable.Range. myList.AddRange(Enumerable.Range

Determine if a number falls within a specified set of ranges

青春壹個敷衍的年華 提交于 2019-12-03 06:59:50
问题 I'm looking for a fluent way of determining if a number falls within a specified set of ranges. My current code looks something like this: int x = 500; // Could be any number if ( ( x > 4199 && x < 6800 ) || ( x > 6999 && x < 8200 ) || ( x > 9999 && x < 10100 ) || ( x > 10999 && x < 11100 ) || ( x > 11999 && x < 12100 ) ) { // More awesome code } Is there a better way of doing this? 回答1: Extension methods? bool Between(this int value, int left, int right) { return value > left && value <

With sql find next available integer within range that is not present in existing integer subset(s)

假装没事ソ 提交于 2019-12-03 05:20:13
Problem statement: given a range x -> y of unsigned integers where x and y are both in the range 0 -> 2 n and n is 0 -> 32 (or 64 in alternate cases) find the minimum available value not equal to x or y that is not in an existing set where existing sets are arbitrary subsets of x -> y I am working with modeling IPv4 and IPv6 subnets in a database. Each subnet is defined by its starting address and ending address (I ensure the integrity of the ranges via business rules). Because IPv6 is too large to store in the bigint datatype we store IP addresses as either binary(4) or binary(16) . The

ruby: what does the asterisk in “p *1..10” mean

吃可爱长大的小学妹 提交于 2019-12-03 04:09:39
问题 the line p *1..10 does exactly the same thing as (1..10).each { |x| puts x } which gives you the following output: $ ruby -e "p *1..10" 1 2 3 4 5 6 7 8 9 10 it's a great shortcut when working with textmate for example, but what does the asterisk do? how does that work? couldn't find anything on the net... 回答1: It's the splat operator. You'll often see it used to split an array into parameters to a function. def my_function(param1, param2, param3) param1 + param2 + param3 end my_values = [2, 3

swift convert Range<Int> to [Int]

半腔热情 提交于 2019-12-03 04:08:06
how to convert Range to Array I tried: let min = 50 let max = 100 let intArray:[Int] = (min...max) get error Range<Int> is not convertible to [Int] I also tried: let intArray:[Int] = [min...max] and let intArray:[Int] = (min...max) as [Int] they don't work either. You need to create an Array<Int> using the Range<Int> rather than cast ing it. let intArray: [Int] = Array(min...max) Put the Range in the init. let intArray = [Int](min...max) do: let intArray = Array(min...max) This should work because Array has an initializer taking a SequenceType and Range conforms to SequenceType . I figured it

Making a list of evenly spaced numbers in a certain range in python

岁酱吖の 提交于 2019-12-03 03:34:04
问题 What is a pythonic way of making list of arbitrary length containing evenly spaced numbers (not just whole integers) between given bounds? For instance: my_func(0,5,10) # ( lower_bound , upper_bound , length ) # [ 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5 ] Note the Range() function only deals with integers. And this: def my_func(low,up,leng): list = [] step = (up - low) / float(leng) for i in range(leng): list.append(low) low = low + step return list seems too complicated. Any ideas? 回答1: Given

Initializer list in a range for loop

耗尽温柔 提交于 2019-12-03 02:17:30
I have objects of different types derived from a single super-type. I wonder if there are any disadvantages in using std::initializer list in a range for loop like this: for(auto object: std::initializer_list<Object *>{object1, object2, object3}) { } Is it completely OK and efficient or would it be better to use an array? To me the std::array solution seems to be more restrictive for the compiler and there is a disadvantage of explicitly stating the size: for(auto object: std::array<Object*, 3>{object1, object2, object3}) { } Is there any other or nicer way of iterating over an explicitly