enumerable

Transform a DataTable into Dictionary C#

浪子不回头ぞ 提交于 2019-12-03 08:16:42
问题 I want to know how to transform a DataTable into a Dictionary. I did something like this. using System.Linq; internal Dictionary<string,object> GetDict(DataTable dt) { return dt.AsEnumerable() .ToDictionary<string, object>(row => row.Field<string>(0), row => row.Field<object>(1)); } But I get: System.Data.EnumerableRowCollection does not contains a definition for 'ToDictionary' and the best extension method overload 'System.Linq.Parallel.Enumerable.ToDictionary(System.Linq.ParallelQuery,

Meaning of the word yield

瘦欲@ 提交于 2019-12-03 05:59:05
问题 Currently I'm reading "The Well-Grounded Rubyist" by David A. Black, and I stuck at 10.9 chapter (Enumerators and the next dimension of enumerability). My question is about yield method. What is the meaning of the word yield in Ruby context? My native language is Russian, and Google Translate shows me a bunch of translation variants, that are confusing me. There are some of them: give , bring , surrender ( give up ), produce , agree , comply and many others. UPD: please, pay attention to the

Transform a DataTable into Dictionary C#

半腔热情 提交于 2019-12-02 21:53:58
I want to know how to transform a DataTable into a Dictionary. I did something like this. using System.Linq; internal Dictionary<string,object> GetDict(DataTable dt) { return dt.AsEnumerable() .ToDictionary<string, object>(row => row.Field<string>(0), row => row.Field<object>(1)); } But I get: System.Data.EnumerableRowCollection does not contains a definition for 'ToDictionary' and the best extension method overload 'System.Linq.Parallel.Enumerable.ToDictionary(System.Linq.ParallelQuery, System.Func, System.Collections.Generic.IEqualityComrparer)' has some invalid argumentsch How can I resolve

Cool tricks and expressive snippets with ruby collections/enumerables [closed]

寵の児 提交于 2019-12-02 18:39:21
What are your favorite code snippets with ruby collections? Preferably they should be discovery for you, be expressive, readable and introduce some fun in your coding practice. Pattern-matching in arrays (for local variables and parameters): (a, b), c = [[:a, :b], :c] [a,b,c] => [:a, :b, :c] (a,), = [[:a]] a => :a Assigning from non-arrays to multiple variables: abc, a, b =* "abc".match(/(a)(b)./) => ["abc", "a", "b"] nil1, =* "abc".match(/xyz/) => [] Initialize array elements with the same expression: 5.times.map { 1 } => [1,1,1,1] Array.new(5) { 1 } => [1,1,1,1,1] Initialize array with the

Possible to access the index in a Hash each loop?

余生长醉 提交于 2019-12-02 14:15:54
I'm probably missing something obvious, but is there a way to access the index/count of the iteration inside a hash each loop? hash = {'three' => 'one', 'four' => 'two', 'one' => 'three'} hash.each { |key, value| # any way to know which iteration this is # (without having to create a count variable)? } YOU If you like to know Index of each iteration you could use .each_with_index hash.each_with_index { |(key,value),index| ... } You could iterate over the keys, and get the values out manually: hash.keys.each_with_index do |key, index| value = hash[key] print "key: #{key}, value: #{value}, index

Python `for` does not iterate over enumerate object

一世执手 提交于 2019-12-02 11:56:19
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: ['1', '2'] DEBUG:root:[(0, '1'), (1, '2')] DEBUG:root:['0'] DEBUG:root:[[]] Docs: https://docs.python

Scheduling a IEnumerable periodically with .NET reactive extensions

[亡魂溺海] 提交于 2019-12-02 07:13:20
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. 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 as period: public static class MyEx { public static IObservable<T> ToObservable<T>(this IEnumerable<T>

How can I get a list from a Ruby enumerable?

妖精的绣舞 提交于 2019-12-01 20:00:49
I know of Python's list method that can consume all elements from a generator. Is there something like that available in Ruby? I know of : elements = [] enumerable.each {|i| elements << i} I also know of the inject alternative. Is there some ready available method? Enumerable#to_a If you want to do some transformation on all the elements in your enumerable, the #collect (a.k.a. #map) method would be helpful: elements = enumerable.collect { |item| item.to_s } In this example, elements will contain all the elements that are in enumerable , but with each of them translated to a string. E.g.

C# infinite iteration

别来无恙 提交于 2019-12-01 18:11:32
问题 Is there anything similar in C# to Java's Stream.iterate ? The closest thing I could find was Enumerable.Range but it is much different. The reason I'm asking is that I've been just watching some presentation about good programming principles and there was a thread about declarative vs. imperative code. What caught my attention was a way you can generate a pseudo-infinite set of data. 回答1: There isn't an exact equivalent in the .Net framework but there is one in the MoreLINQ library: foreach

How to create a TypeScript @enumerable(false) decorator for a property

最后都变了- 提交于 2019-12-01 17:21:32
问题 I want to create a decorator in TypeScript in order to be able to make a class property not enumerable. I found an example of @enumerable here: https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators but that only seems to work for methods, not properties: https://www.typescriptlang.org/docs/handbook/decorators.html#property-decorators NOTE  A Property Descriptor is not provided as an argument to a property decorator due to how property decorators are initialized in