enumerable

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

痴心易碎 提交于 2019-12-23 10:38:41
问题 #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

Moq First() Last() and GetEnumerator() wierdness

做~自己de王妃 提交于 2019-12-23 07:30:10
问题 I am Moqing my Route Parts from a rps = new List <IRoutePart> ... (3 Route Parts) and Moqing GetEnumerator() for my Route as below route.Setup(ro => ro.GetEnumerator()).Returns(rps.GetEnumerator()); but the Moq fails in the following code with "Sequence contains no elements" on the call to Last() o.Route.Any(rp => rp.IsNonTowLocation && rp != o.Route.First() && rp != o.Route.Last()) Looking at First() Last() in the immediate windows I find the values change if I execute First() Last()

IEnumerable.Cast() vs casting in IEnumerable.Select()

偶尔善良 提交于 2019-12-23 07:27:32
问题 Suppose I have an IEnumerable<int> and I want these to be converted into their ASCII-equivalent characters. For a single integer, it would just be (char)i , so there's always collection.Select(i => (char)i) , but I thought it would be a tad cleaner to use collection.Cast() . Can anyone explain why I get an InvalidCastException when I use collection.Cast<char>() but not with collection.Select(i => (char)i) ? Edit: Interestingly enough, when I call collection.OfType<char>() I get an empty set.

Why doesn't each_slice work?

早过忘川 提交于 2019-12-23 03:17:13
问题 I am trying to use the Enumerable#each_slice. It doesn't work on my computer, stating that method is not found. I am running ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0] API: http://ruby-doc.org/core/classes/Enumerable.html#M003142 Example: (1..10).each_slice(3) {|a| p a} # I get NoMethodError: undefined method `each_slice' for 1..10:Range What am I doing wrong? 回答1: In ruby 1.8.6 you have to require 'enumerator' (which is part of stdlib and has been merged into core in 1.8.7+

Ruby: How do you set an Enumerator's state?

别说谁变了你拦得住时间么 提交于 2019-12-22 12:37:21
问题 I'm doing a base 64 permutation incrementor. I've already written all the working code. But seeing as how Ruby already as Array::permutation which produces an Enumerator; I'd like to use that and take it a step further. Without having to go through every permutation by using 'next', can I set the start point? x = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a + ['+','/'] y = x.permutation(12) y.peek.join => "ABCDEFGHIJKL" y.next y.peek.join => "ABCDEFGHIJKM" . # DO SOMETHING LIKE THIS y

Please explain System.Linq.Enumerable.Where(Func<T, int, bool> predicate)

浪子不回头ぞ 提交于 2019-12-22 08:33:54
问题 I can't make any sense of the MSDN documentation for this overload of the Where method that accepts a predicate that has two arguments where the int, supposedly, represents the index of the source element, whatever that means (I thought an enumerable was a sequence and you couldn't see further than the next item, much less do any indexing on it). Can someone please explain how to use this overload and specifically what that int in the Func is for and how it is used? 回答1: The int parameter

What is the enumerable argument for in Object.create?

泄露秘密 提交于 2019-12-22 05:17:27
问题 In what usages of Object.create do you want to set enumerable to true ? 回答1: A property of an object should be enumerable if you want to be able to have access to it when you iterate through all the objects properties. Example: var obj = {prop1: 'val1', prop2:'val2'}; for (var prop in obj){ console.log(prop, obj[prop]); } In this type of instantiation, enumerable is always true, this will give you an output of: prop1 val1 prop2 val2 If you would have used Object.create() like so: obj = Object

Why does Enumerable#find/#detect return an Array even when called on an Hash?

你说的曾经没有我的故事 提交于 2019-12-22 04:01:19
问题 The documentation for Enumerable#find/#detect says: find(ifnone = nil) { |obj| block } → obj or nil find(ifnone = nil) → an_enumerator Passes each entry in enum to block . Returns the first for which block is not false. If no object matches, calls ifnone and returns its result when it is specified, or returns nil otherwise. However, when it is called on the Hash, the result has changed the type to Array instead of the original Hash. Is it some implementation fault or some historical

How can I make DataTable enumerable?

£可爱£侵袭症+ 提交于 2019-12-21 17:39:21
问题 I cannot use AsEnumerable() on DataTable, I'm using C# 3 but I'm just targeting 2.0 framework (LINQ capability is courtesy of LINQBridge). Is there any way I can make DataTable enumerable without using Select() ? bool isExisting = (bdsAttachments.DataSource as DataTable).Select().Any(xxx => (string)dr["filename"] == filename); Update: I wanted it to make it look like this: bool isExisting = (bdsAttachments.DataSource as DataTable).AsEnumerable().Any(xxx => (string)dr["filename"] == filename);

generic Enumeration to Iterable converter [closed]

蓝咒 提交于 2019-12-21 06:46:38
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . HttpServletRequest is using a lot of java.util.Enumeration. I would like to use them in for-each, so i need to convert them into interable. this is not a problem, but I since I have more than one project needing this I need a library to do this. I would rather not make my own - is there any standard library that