enumerable

Passing array to function that takes either params object[] or IEnumerable<T>

牧云@^-^@ 提交于 2019-11-30 00:34:22
问题 I want to pass an array of custom objects to a function like String.Join which has the following signatures: public static string Join(string separator, params Object[] values) public static string Join(string separator, IEnumerable<T> values) If I call the function like this: var arr = new MyClass[]{ new MyClass(), new MyClass() }; string text = string.Join("\n", arr); I get a compiler error: The call is ambiguous between the following methods or properties: 'string.Join(string, params

Rails lists have .first and .second – is there a .hundredth or .sixty_nineth ?

和自甴很熟 提交于 2019-11-30 00:13:27
问题 Is there a class or other extension for Rails that allows more than the first few elements in a series (and the last)? These work: [2,45,2,14,53,23,634,346,34,46,643,634,346,34,34].fifth # -> 53 [2,45,2,14,53,23,634,346,34,46,643,634,346,34,34].last # -> 34 so where is? list.sixth list.hundredth 回答1: There was a time when Rails added these, but there was a lot of controversy so most were removed. The only one of this experiment that remains is Array#forty_two . 回答2: You can just use square

Is there a more concise way to call an outside method on a map in Ruby? [duplicate]

瘦欲@ 提交于 2019-11-29 22:56:42
问题 This question already has answers here : How to turn a Ruby method into a block? (3 answers) Closed 5 years ago . Is there a more concise way of doing this? # Given a directory containing subdirectories: foo, bar targets = ['./foo', './bar', './free'] targets.map{ |d| Dir.exists? d } # => [ true, true, false ] I'd love to be able to do something similar to proc calls... it feels cleaner: # targets.map( Dir.exists? ) 回答1: Yes, possible this way, but not good for performance (see post: Is the

Implementing List Enumerator OfType<T> in Delphi

左心房为你撑大大i 提交于 2019-11-29 21:36:32
I am using Delphi XE to implement an enumerator that allows filtering the elements of the list by type. I have quickly assembled a test unit as follows: unit uTestList; interface uses Generics.Collections; type TListItemBase = class(TObject) end; { TListItemBase } TListItemChild1 = class(TListItemBase) end; TListItemChild2 = class(TListItemBase) end; TTestList<T : TListItemBase> = class; TOfTypeEnumerator<T, TFilter> = class(TInterfacedObject, IEnumerator<TFilter>) private FTestList : TList<T>; FIndex : Integer; protected constructor Create(Owner : TList<T>); overload; function GetCurrent :

How to get Alternate elements using Enumerable in C#

£可爱£侵袭症+ 提交于 2019-11-29 17:06:16
This is a continuation of my question: How to get reverse of a series of elements using Enumarable in C#? Now I need alternate elements only. Here is my solution using for loop: int Max = 10; int limit = 5; Dictionary<String , String> MyDict = new Dictionary<string,string>(); int j = 0; for (int i = 0; i <Max; i++) { if (i >= limit) MyDict.Add((i+1).ToString(), "None"); else MyDict.Add((i+1).ToString(), j.ToString()); j+=2; } The output is like { "1" "0"} { "2" "2"} { "3" "4"} { "4" "6"} { "5" "8"} { "6" "None"} { "7" "None"} { "8" "None"} { "9" "None"} { "10" "None"} How to do this using

Ruby array of hash. group_by and modify in one line

强颜欢笑 提交于 2019-11-29 11:44:35
问题 I have an array of hashes, something like [ {:type=>"Meat", :name=>"one"}, {:type=>"Meat", :name=>"two"}, {:type=>"Fruit", :name=>"four"} ] and I want to convert it to this { "Meat" => ["one", "two"], "Fruit" => ["Four"]} I tried group_by but then i got this { "Meat" => [{:type=>"Meat", :name=>"one"}, {:type=>"Meat", :name=>"two"}], "Fruit" => [{:type=>"Fruit", :name=>"four"}] } and then I can't modify it to leave just the name and not the full hash. I need to do this in one line because is

Help understanding yield and enumerators in Ruby

北城以北 提交于 2019-11-29 10:22:17
问题 I would appreciate it if someone could help me understand the difference between using a Yielder in an Enumerator vs. just invoking yield in an Enumerator. The "Well-grounded Rubyist" suggests that one doesn't "yield from the block" but doesn't explain precisely what's going on. Thanks 回答1: The Enumerator::Yielder#yield method and the Enumerator::Yielder::<< method are exactly the same . In fact, they are aliases. So, which one of those two you use, is 100% personal preference, just like

Do LINQ's Enumerable Methods Maintain Relative Order of Elements?

丶灬走出姿态 提交于 2019-11-29 03:58:04
Say I have List<Foo> foos where the current order of elements is important. If I then apply a LINQ Enumerable method such as GroupBy , Where or Select , can I rely on the resulting IEnumerable<Foo> to iterate in the same relative order as the original list? Yes, for Enumerable methods (LINQ to Objects, which applies to List<T> you mentioned), you can rely on the order of elements returned by Select , Where , or GroupBy . This is not the case for things that are inherently unordered like ToDictionary or Distinct . From Enumerable.GroupBy documentation: The IGrouping<TKey, TElement> objects are

Apply method to each elements in array/enumerable

大兔子大兔子 提交于 2019-11-28 20:06:19
This is my array: array = [:one,:two,:three] I want to apply to_s method to all of my array elements to get array = ['one','two','three'] . How can I do this (converting each element of the enumerable to something else)? This will work: array.map!(&:to_s) You can use map or map! respectively, the first will return a new list, the second will modify the list in-place: >> array = [:one,:two,:three] => [:one, :two, :three] >> array.map{ |x| x.to_s } => ["one", "two", "three"] It's worth noting that if you have an array of objects you want to pass individually into a method with a different caller

Implementing List Enumerator OfType<T> in Delphi

佐手、 提交于 2019-11-28 17:33:29
问题 I am using Delphi XE to implement an enumerator that allows filtering the elements of the list by type. I have quickly assembled a test unit as follows: unit uTestList; interface uses Generics.Collections; type TListItemBase = class(TObject) end; { TListItemBase } TListItemChild1 = class(TListItemBase) end; TListItemChild2 = class(TListItemBase) end; TTestList<T : TListItemBase> = class; TOfTypeEnumerator<T, TFilter> = class(TInterfacedObject, IEnumerator<TFilter>) private FTestList : TList<T