enumerable

How does Ruby enumerator terminate iteration?

匆匆过客 提交于 2021-01-27 06:28:44
问题 Friends, please I need help with this explanation: In the Ruby code below, what condition termites the loop do? It's supposed to be an infinite loop, but, how does it terminate? # Ruby code fib = Enumerator.new do |y| a = b = 1 loop do y << a a, b = b, a + b end end p fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] Your contributions will be highly appreciated. 回答1: (Source: https://rossta.net/blog/infinite-sequences-in-ruby.html) The way you have implemented the function fib allows it

When is the Enumerator::Yielder#yield method useful?

走远了吗. 提交于 2020-05-25 06:07:24
问题 The question "Meaning of the word yield" mentions the Enumerator::Yielder#yield method. I haven't used it before, and wonder under what circumstances it would be useful. Is it mainly useful when you want to create an infinite list of items, such as the Sieve of Eratosthenes, and when you need to use an external iterator? 回答1: "How to create an infinite enumerable of Times?" talks about constructing and lazy iterators, but my favorite usage is wrapping an existing Enumerable with additional

Why does repeated Enumerable to Observable conversion block

孤街浪徒 提交于 2020-05-09 02:48:22
问题 This is a rather educational, out of curiosity question. Consider the following snippet: var enumerable = Enumerable.Range(0, 5); var observable = enumerable.ToObservable(); var enu = observable.Concat(observable).ToEnumerable(); enu.ToObservable().SubscribeDebug(); Where SubscribeDebug subscribes a simple observer: public class DebugObserver<T> : IObserver<T> { public void OnCompleted() { Debug.WriteLine("Completed"); } public void OnError(Exception error) { Debug.WriteLine("Error"); }

Performance of Enumerable.Range vs for loop

流过昼夜 提交于 2020-03-18 07:00:31
问题 I wondered what the performance overhead is of using Enumerable.Range was against using a foreach loop. For example: var stringArray = Enumerable.Range(0, 4).Select(i => string.Empty).ToArray(); VS. var stringArray = new string[4]; for (int i = 0; i < formatted.Length; i++) { stringArray[i] = string.Empty; } I spotted these question: Why is Enumerable.Range faster than a direct yield loop? Enumerable.Range implementation Thoughts on foreach with Enumerable.Range vs traditional for loop But I

Stopping enumeration in JavaScript when using prototype

牧云@^-^@ 提交于 2020-03-06 02:15:50
问题 I'm currently trying to get my head around using prototype in JavaScript. To experiment with this, I've written a function that effectively works as allowing you to put a where clause onto arrays: Array.prototype.where=(function(){ var tmpArr=[], success; for (var x in this){ var success=true; for (var i in arguments){ if (this[x][arguments[i][0]]!=arguments[i][1]){ success=false; break; } } if (success==true){ tmpArr.push(this[x]); } } return tmpArr; }); An example use would be: arrayName

Does Hash override Enumerable#map()?

时间秒杀一切 提交于 2020-01-23 11:39:48
问题 Given that map() is defined by Enumerable , how can Hash#map yield two variables to its block? Does Hash override Enumerable#map() ? Here's a little example, for fun: ruby-1.9.2-p180 :001 > {"herp" => "derp"}.map{|k,v| k+v} => ["herpderp"] 回答1: It doesn't override map Hash.new.method(:map).owner # => Enumerable It yields two variables which get collected into an array class Nums include Enumerable def each yield 1 yield 1, 2 yield 3, 4, 5 end end Nums.new.to_a # => [1, [1, 2], [3, 4, 5]] 回答2:

Linq statement for an infinite sequence of successive halves

こ雲淡風輕ζ 提交于 2020-01-23 05:47:05
问题 Given a starting number, imagine an infinite sequence of its successive halves. 1, 0.5, 0.25, 0.125, ... (Ignore any numerical instabilities inherent in double .) Can this be done in a single expression without writing any custom extension methods or generator methods? 回答1: I don't know of a single-expression way but I found this clever generator code here: http://csharpindepth.com/articles/Chapter11/StreamingAndIterators.aspx public static IEnumerable<TSource> Generate<TSource>(TSource start

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

假如想象 提交于 2020-01-11 17:06:10
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . What are your favorite code snippets with ruby collections? Preferably they should be discovery for you, be expressive, readable and

Why will ES6 WeakMap's not be enumerable?

南笙酒味 提交于 2020-01-09 07:15:29
问题 Before my re-entry in JavaScript (and related) I've done lots of ActionScript 3 and there they had a Dictionary object that had weak keys just like the upcoming WeakMap; but the AS3 version still was enumerable like a regular generic object while the WeakMap specifically has no .keys() or .values() . The AS3 version allowed us to rig some really interesting and usefull constructs but I feel the JS version is somewhat limited. Why is that? If the Flash VM could do it then what is keeping

Why will ES6 WeakMap's not be enumerable?

笑着哭i 提交于 2020-01-09 07:15:01
问题 Before my re-entry in JavaScript (and related) I've done lots of ActionScript 3 and there they had a Dictionary object that had weak keys just like the upcoming WeakMap; but the AS3 version still was enumerable like a regular generic object while the WeakMap specifically has no .keys() or .values() . The AS3 version allowed us to rig some really interesting and usefull constructs but I feel the JS version is somewhat limited. Why is that? If the Flash VM could do it then what is keeping