Slice chunking in Go

You don’t need to make new slices, just append slices of logs to the divided slice. http://play.golang.org/p/vyihJZlDVy var divided [][]string chunkSize := (len(logs) + numCPU – 1) / numCPU for i := 0; i < len(logs); i += chunkSize { end := i + chunkSize if end > len(logs) { end = len(logs) } divided … Read more

Is there an elegant way to process a stream in chunks?

Elegance is in the eye of the beholder. If you don’t mind using a stateful function in groupingBy, you can do this: AtomicInteger counter = new AtomicInteger(); stream.collect(groupingBy(x->counter.getAndIncrement()/chunkSize)) .values() .forEach(database::flushChunk); This doesn’t win any performance or memory usage points over your original solution because it will still materialize the entire stream before doing anything. If … Read more

What is the best way to chop a string into chunks of a given length in Ruby?

Use String#scan: >> ‘abcdefghijklmnopqrstuvwxyz’.scan(/.{4}/) => [“abcd”, “efgh”, “ijkl”, “mnop”, “qrst”, “uvwx”] >> ‘abcdefghijklmnopqrstuvwxyz’.scan(/.{1,4}/) => [“abcd”, “efgh”, “ijkl”, “mnop”, “qrst”, “uvwx”, “yz”] >> ‘abcdefghijklmnopqrstuvwxyz’.scan(/.{1,3}/) => [“abc”, “def”, “ghi”, “jkl”, “mno”, “pqr”, “stu”, “vwx”, “yz”]

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)