enumerator

Ruby: reuse value in a block without assigning it to variable (write object method on the fly)

∥☆過路亽.° 提交于 2019-12-25 11:25:11
问题 There are several situations where I'd like to apply a block to a certain value and use the value inside this block, to use the enumerator coding style to every element. If such method would be called decompose , it would look like: result = [3, 4, 7, 8].decompose{ |array| array[2] + array[3] } # result = 15 # OR result = {:key1 => 'value', :key2 => true}.decompose{ |hash| hash[:key1] if hash[:key2] } # result = 'value' # OR [min, max] = [3, 4, 7, 8].decompose{ |array| [array.min, array.max]

Is it possible to get a callback when data fed into a Play Concurrent.unicast Enumerator's channel has been processed?

故事扮演 提交于 2019-12-24 11:55:02
问题 I'm proxying between a TCP connection and a WebSocket, and I want to apply back-pressure to the TCP socket if the WebSocket is sending data to the browser slowly. When I receive data from the TCP socket I do: channel.push(data.toArray) I'd like to get an acknowledgement of when that data has been sent. How do I get that? 回答1: I've done this: val resumer = Enumeratee.map[Array[Byte]] { in => connection ! Tcp.ResumeReading in } val enumeratorWithAck = enumerator &> resumer It seems to work, but

How is Enumerator created with for in construction destroyed?

不想你离开。 提交于 2019-12-24 05:29:23
问题 I have a collection derived from TCollection, implementing GetEnumerator so I can use it in a construction like for lElem in lCollection do The enumerator is derived from TObject, exactly like the standard enumerators supplied by Delphi and therefor doesn't have an owner. The Delphi help mentions that if the enumerator supports IDisposable is it will be disposed of, but that is for .NET only of course. What I was wondering is, how and when and by who the enumerator instance is freed? 回答1: For

Disposing of arguments for an iterator block

天大地大妈咪最大 提交于 2019-12-23 18:31:08
问题 Allright, here it goes a good piece of bad code: public class Log : CachingProxyList<Event> { public static Log FromFile(String fullPath) { using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs)) { return new Log(sr); } } } public Log(StreamReader stream) : base(Parser.Parse(Parser.Tokenize(stream))) { /* Here goes some "magic", the whole reason for this * class to exist, but not really relevant to the issue */ } } And

Clone an Enumerator in Ruby?

谁说我不能喝 提交于 2019-12-23 09:31:13
问题 I have a tree that I'm trying to traverse. As I traverse it, I keep a stack of enumerators in which each enumerator is used to enumerate over a tree's children. I'd like to be able to duplicate this stack of enumerators and hand it to another object so it may traverse the tree starting in the place indicated by the state of the stack. When I attempt to call #dup on Enumerator, I get an error. Is it possible to duplicate an Enumerator? If not, how could I accomplish the same thing? (I've

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

Where does a generic List<> implement Reset?

一个人想着一个人 提交于 2019-12-22 12:14:49
问题 when I go to the definition of List<> I can see it has a public struct Enumerator that implements the interfaces IEnumerator<T> , IDisposable and IEnumerator. IEnumerator should force the implementation of Reset - besides Current and MoveNext. Yet only Current and MoveNext are implemented. How can that be? Where do I find the Reset() of List<>? var list = new List<int>(); list.Add(23); list.Add(44); var Enumerator = list.GetEnumerator(); while (Enumerator.MoveNext()) { Console.WriteLine

Is there a way to iterate through HttpServletRequest.getAttributeNames() more than once?

拈花ヽ惹草 提交于 2019-12-22 05:41:49
问题 I'm trying to log the contents of the HttpServletRequest attributes collection. I need to do this when the servlet first starts, and again right before the servlet is finished. I'm doing this in an attempt to understand a crufty and ill-maintained servlet. Because I need to have as little impact as possible, servlet filters are not an option. So here's the problem. When the servlet starts, I'll iterate through the enumeration returned by HttpServletRequest.getAttributeNames(). However, when I

Best way to convert a non-generic collection to generic collection

和自甴很熟 提交于 2019-12-21 07:36:28
问题 What is the best way to convert a non-generic collection to a generic collection? Is there a way to LINQ it? I have the following code. public class NonGenericCollection:CollectionBase { public void Add(TestClass a) { List.Add(a); } } public class ConvertTest { public static List<TestClass> ConvertToGenericClass( NonGenericCollection collection) { // Ask for help here. } } Thanks! 回答1: Since you can guarantee they're all TestClass instances, use the LINQ Cast<T> method: public static List

Count iteration on the Enumerable cycle

别说谁变了你拦得住时间么 提交于 2019-12-20 01:43:41
问题 I would like to use an enumerator like [1,2,3].cycle and count how many times I've gone through the iterations. [1,2,3].cycle.count creates an infinite loop and doesn't bring the iteration count. I'm playing a card game, and it cycles through the players. It's easy in the game to say: @round = 0 if @turn == 1 @round += 1 end and it works. But I would like to know how to change count or add iter only for enumerators with cycle into something like this: module Enumerable def cycle super def