enumerable

How to get reverse of a series of elements using Enumarable in C#?

点点圈 提交于 2020-01-06 03:20:07
问题 I have two Integer variables like int Max = 10; int limit = 5; and a Dictionary Dictionary<String , String> MyDict = new Dictionary<string,string>(); I need to fill the dictionary with Max elements and if the index is greater than or equal to limit then the value should be none. Here is my sample code which will clear my question int j = 1; for (int i = Max-1; i >= 0; i--) { if (i >= limit) MyDict.Add((j++).ToString(), "None"); else MyDict.Add((j++).ToString(), i.ToString()); } so the result

Why is Range#sum defined in Enumerable module?

半世苍凉 提交于 2020-01-03 12:31:14
问题 In Ruby 2.4 and for Integer Ranges, Range(Enumerable)#sum is optimized to return a result directly, without iterating over all elements. I don't understand why the corresponding code is defined in enum.c for the Enumerable module and not in range.c for Range class. Why should Enumerable know about classes that include it (e.g. Range , Hash , ...) and check for their type instead of letting those classes overwrite Enumerable#sum ? Seen in enum.c : return int_range_sum(beg, end, excl, memo.v);

Ruby Enumerables — what are they exactly?

只谈情不闲聊 提交于 2020-01-03 02:04:36
问题 Can someone explain in the most basic, laymans terms what a Ruby Enumerable is? I'm very new to coding and just starting to work with arrays and hashes. I read the word "Enumerables" everywhere but I don't understand what they are. 回答1: Can someone explain in the most basic, laymans terms what a Ruby Enumerable is? It's a module that defines a bunch of methods, and when another class includes that module, those methods are available in that class. So if someone uses a method like each_with

Ruby: Is there something like Enumerable#drop that returns an enumerator instead of an array?

孤街浪徒 提交于 2020-01-01 09:24:16
问题 I have some big fixed-width files and I need to drop the header line. Keeping track of an iterator doesn't seem very idiomatic. # This is what I do now. File.open(filename).each_line.with_index do |line, idx| if idx > 0 ... end end # This is what I want to do but I don't need drop(1) to slurp # the file into an array. File.open(filename).drop(1).each_line do { |line| ... } What's the Ruby idiom for this? 回答1: If you need it more than once, you could write an extension to Enumerator . class

ruby methods that either yield or return Enumerator

笑着哭i 提交于 2019-12-31 08:30:09
问题 in recent versions of Ruby, many methods in Enumerable return an Enumerator when they are called without a block: [1,2,3,4].map #=> #<Enumerator: [1, 2, 3, 4]:map> [1,2,3,4].map { |x| x*2 } #=> [2, 4, 6, 8] I want do do the same thing in my own methods like so: class Array def double(&block) # ??? end end arr = [1,2,3,4] puts "with block: yielding directly" arr.double { |x| p x } puts "without block: returning Enumerator" enum = arr.double enum.each { |x| p x } 回答1: The core libraries insert

ruby methods that either yield or return Enumerator

不问归期 提交于 2019-12-31 08:29:30
问题 in recent versions of Ruby, many methods in Enumerable return an Enumerator when they are called without a block: [1,2,3,4].map #=> #<Enumerator: [1, 2, 3, 4]:map> [1,2,3,4].map { |x| x*2 } #=> [2, 4, 6, 8] I want do do the same thing in my own methods like so: class Array def double(&block) # ??? end end arr = [1,2,3,4] puts "with block: yielding directly" arr.double { |x| p x } puts "without block: returning Enumerator" enum = arr.double enum.each { |x| p x } 回答1: The core libraries insert

Python `for` does not iterate over enumerate object

梦想的初衷 提交于 2019-12-31 05:26:30
问题 Why does this not iterate? import logging logging.basicConfig(level=logging.DEBUG) x = [] y = [[] for n in range(0, 1)] linedata = ["0","1","2"] x.append( linedata[0] ) d = linedata[1:] logging.debug( "d: {}".format(d) ) e = enumerate(d) logging.debug( list(e) ) for k, v in e: logging.debug( "k:{} v:{}".format( k, v ) ) y[int(k)].append( v ) #for d in [(0,1)]: #logging.debug( "k:{} v:{}".format( d[0], d[1] ) ) #y[d[0]].append( d[1] ) logging.debug( x ) logging.debug( y ) Output: DEBUG:root:d:

Scheduling a IEnumerable periodically with .NET reactive extensions

*爱你&永不变心* 提交于 2019-12-31 03:51:32
问题 Say for example I have an enumerable dim e = Enumerable.Range(0, 1024) I'd like to be able to do dim o = e.ToObservable(Timespan.FromSeconds(1)) So that the observable would generate values every second until the enumerable is exhausted. I can't figure a simple way to do this. 回答1: I have also looked for the solution and after reading the intro to rx made my self one: There is an Observable.Generate() overload which I have used to make my own ToObservable() extension method, taking TimeSpan

Dynamic LINQ, Select function, works on Enumerable, but not Queryable

一笑奈何 提交于 2019-12-23 20:15:59
问题 I have been fiddling with dynamic LINQ for some time now, but I have yet to learn its secrets. I have an expression that I want to parse that looks like this: "document.LineItems.Select(i => i.Credit).Sum();" During parsing of this I reach a point where I need to call a Select function on LineItems Collection. I am using factory method of Expression.Call: Expression.Call( typeof(Queryable), "Select", new Type[] { typeof(LineItem), typeof(decimal?) }, expr, Expression.Lambda(expression, new

Why would #each_with_object and #inject switch the order of block parameters?

有些话、适合烂在心里 提交于 2019-12-23 10:39:32
问题 #each_with_object and #inject can both be used to build a hash. For example: matrix = [['foo', 'bar'], ['cat', 'dog']] some_hash = matrix.inject({}) do |memo, arr| memo[arr[0]] = arr memo # no implicit conversion of String into Integer (TypeError) if commented out end p some_hash # {"foo"=>["foo", "bar"], "cat"=>["cat", "dog"]} another_hash = matrix.each_with_object({}) do |arr, memo| memo[arr[0]] = arr end p another_hash # {"foo"=>["foo", "bar"], "cat"=>["cat", "dog"]} One of the key