enumerator

Thread safe Enumerator in Ruby

孤街醉人 提交于 2019-12-19 02:32:46
问题 TLDR: Is there a thread-safe version of the Enumerator class in Ruby? What I'm trying to do: I have a method in a Ruby On Rails application that I wanted to run concurrently. The method is supposed to create a zip file containing reports from the site, where each file in the zip is a PDF. The conversion from html to PDF is somewhat slow, thus the desire to multi-thread. How I expected to do it: I wanted to use 5 threads, so I figured I would have a shared Enumerator between the threads. Each

How to iterate over two arrays at once?

邮差的信 提交于 2019-12-17 23:28:31
问题 I have two arrays built while parsing a text file. The first contains the column names, the second contains the values from the current row. I need to iterate over both lists at once to build a map. Right now I have the following: var currentValues = currentRow.Split(separatorChar); var valueEnumerator = currentValues.GetEnumerator(); foreach (String column in columnList) { valueEnumerator.MoveNext(); valueMap.Add(column, (String)valueEnumerator.Current); } This works just fine, but it doesn

Scala: Getting original iteratee after applying enumeratee (example from play documentation doesn't compile)

a 夏天 提交于 2019-12-13 20:43:20
问题 I want to apply an enumeratee to an iteratee and afterwards get the original iteratee back, so I can apply further stuff. There is an example in the play documentation which uses an Iteratee[Int,Int] that just sums up its input (http://www.playframework.org/documentation/2.0.1/Enumeratees). Then they use an Enumeratee[String,Int] that allows strings like "3" and "6" as input. The example is as follows: val sum:Iteratee[Int,Int] = Iteratee.fold[Int,Int](0){ (s,e) => s + e } //create am

When should I separately implement IEnumerator<T>?

风格不统一 提交于 2019-12-12 12:06:07
问题 In the framework classes of collections I have often seen IEnumerator<T> separately implemented as an inner class and an instance of it is returned in the GetEnumerator method. Now suppose I'm writing my own collection classes which will have an inbuilt collection like List<T> or T[] act as the holder internally, say like this: public class SpecialCollection<T> : IEnumerable<T> { List<T> list; public SpecialCollection<T>() { } public IEnumerator<T> GetEnumerator() { return list.GetEnumerator(

Enumerator disposal when not using using, foreach or manually calling Dispose()

 ̄綄美尐妖づ 提交于 2019-12-12 07:13:50
问题 I'm using yield return to iterate over an SqlDataReader 's records: IEnumerable<Reading> GetReadings() { using (var connection = new SqlConnection(_connectionString)) { using (var command = new SqlCommand(_query, connection)) { connection.Open(); using (var reader = command.ExecuteReader()) { while (reader.Read()) { yield return new Reading { End = reader.GetDateTime(0), Value = reader.GetDouble(1) }; } } connection.Close(); } } } I'm then using an adapted version of this accepted answer to

How can I list all the const properties defined in a class

我与影子孤独终老i 提交于 2019-12-11 14:16:04
问题 How can i list all the names (and values) of public (and private / protected) const defined in a class ? public class Layers { public const BACKGROUND:String = "background"; public const PARENT:String = "parent"; public const MAP:String = "map"; public const LINES:String = "lines"; public const POINTS:String = "points"; public const WINDOWS:String = "windows"; ... public function isValidValue(type:String) { // ... // if type is a value of a constant return TRUE // ... } } 回答1: At runtime, you

C# “Generator” Method

拜拜、爱过 提交于 2019-12-11 13:45:45
问题 I come from the world of Python and am trying to create a "generator" method in C#. I'm parsing a file in chunks of a specific buffer size, and only want to read and store the next chunk one at a time and yield it in a foreach loop. Here's what I have so far (simplified proof of concept): class Page { public uint StartOffset { get; set; } private uint currentOffset = 0; public Page(MyClass c, uint pageNumber) { uint StartOffset = pageNumber * c.myPageSize; if (StartOffset < c.myLength)

“break” vs “raise StopIteration” in a Ruby Enumerator

☆樱花仙子☆ 提交于 2019-12-11 04:44:15
问题 If I use Ruby Enumerators to implement a generator and a filter: generator = Enumerator.new do |y| x = 0 loop do y << x x += 1 break if x > CUTOFF end end.lazy filter = Enumerator.new do |y| loop do i = generator.next y << i if i.even? end end does it make any difference whether I break out of the generator's loop using break if x > CUTOFF vs raise StopIteration if x > CUTOFF ? Both seem to work. I imagine break would be more performant, but is raise more idiomatic here? 回答1: In Ruby it's

How to merge 2 Enumerators in one, based on merge rule

旧时模样 提交于 2019-12-10 21:57:24
问题 We have a small Scala project on Playframework. I'm trying to do everything reactive, and stumbled on a problem. I have two Enumerator[A] instances, representing values from a DB ordered by date. I need to return them as a single Enumerator[A] keeping date ordering. I have not found any solution for that in Enumerator[A], so I'm accumulating A's in single collection, and ordering them afterwards. case class A( created: Date, data: String ) val as: Enumerator[A] = findByAOrderedByCreated() val

Swift equivalent of Ruby's “each_cons”

江枫思渺然 提交于 2019-12-10 14:43:59
问题 Ruby Ruby has each_cons that can be used like this class Pair def initialize(left, right) @left = left @right = right end end votes = ["a", "b", "c", "d"] pairs = votes.each_cons(2).map { |vote| Pair.new(*vote) } p pairs # [#<Pair @left="a", @right="b">, #<Pair @left="b", @right="c">, #<Pair @left="c", @right="d">] Swift The same code in swift, but without the each_cons function struct Pair { let left: String let right: String } let votes = ["a", "b", "c", "d"] var pairs = [Pair]() for i in 1