The Joys Of Programming

To get roughly equally distributed intervals you need to keep your running total as a floating point. Something like this (off the top of my head, I don't have a C# compiler on hand to test it, and to keep it simple I haven't included the special case for fewer days than buckets):

IEnumerable<Tuple<int, int>> GetRanges(int start, int end, uint n = 10) { double interval = (double)(end - start + 1) / n; double head = start; int tail = start; for (int i = 1; i < n; ++i) { head += interval; int width = (int)Math.floor(head - tail); yield return Tuple.Create(tail, tail + width - 1); tail += width; } yield return Tuple.Create(tail, end); }

The loop count isn't a fencepost error, it does n-1 loops intentionally. The last yield is special-cased to allow for rounding errors; if we just did n loops, the last one would sometimes only go to end-1 instead of end.

Hope I got that right...

If you've got to handle numbers of days that don't divide neatly in to 10s then you should convert your time intervals in to hours. Divide those by 10. Then convert back to a date format which includes hours for plotting (in fact you should do that for all time intervals).

Here's what I wound up with, which turned out very similar to CaptainCrowbar's suggestion.

IEnumerable<Tuple<int, int>> GetRanges(int start, int end, uint n = 10) { double width = (double)(end - start + 1) / n; if (width < 1.0) { // end + 1 to be inclusive of the end of the range for (int i = start; i <= end; i++) { yield return Tuple.Create(i, i); } yield break; } double current = start; while (current < end) { double next = current + width; if (current != start) { yield return Tuple.Create((int)current+1, Math.Min(end, (int)next)); } else { yield return Tuple.Create((int)current, (int)next); } current = next; } }

Which gave me:

[41274, 41306], // length = 33 [41307, 41339], // length = 33 [41340, 41371], // length = 32 [41372, 41404], // length = 33 [41405, 41436], // length = 32 [41437, 41469], // length = 33 [41470, 41501], // length = 32 [41502, 41534], // length = 33 [41535, 41566], // length = 32 [41567, 41598] // length = 32

It handles lower ranges better, too. [0, 18], for example, had a severe rounding error due to the int cast (1.9 -> 1), causing a bunch of 1's followed by a 9, like this:

[0, 1], // length = 2 [2, 2], // length = 1 [3, 3], // length = 1 [4, 4], // length = 1 [5, 5], // length = 1 [6, 6], // length = 1 [7, 7], // length = 1 [8, 8], // length = 1 [9, 9], // length = 1 [10, 18] // length = 9

With the improved code, it's:

[0, 1], // length = 2 [2, 3], // length = 2 [4, 5], // length = 2 [6, 7], // length = 2 [8, 9], // length = 2 [10, 11], // length = 2 [12, 13], // length = 2 [14, 15], // length = 2 [16, 17], // length = 2 [18, 18] // length = 1

Math: Not Even Once

Hypatian wrote:

Worth looking at for solving that sort of problem, although it's probably overkill if you don't have to do a lot of it: Bresenham's line algorithm.

Given that computer graphics was my favorite course in college, I have no idea why I didn't think of that sooner.

Worth looking at for solving that sort of problem, although it's probably overkill if you don't have to do a lot of it: Bresenham's line algorithm. (If someone doesn't see why a pixel-based line-drawing algorithm is relevant: Each strip of horizontal pixels is one of your relatively-equally-sized segments.)

Well, the connection I mentioned in my edit doesn't really pop into your head until you've either thought about it a bunch and had an "aha!" moment, or heard it from someone else, or both. Once you see it, it's a pretty cool solution—but the connection between bins and lines of pixels is not a real obvious one.

Objective-C++ continues to impress. I think that it's found a home as my favorite UI/tooling language, even though it's sort of Frankensteinian. I feel like it's what C++/CLI could be if C++/CLI didn't decide it had to infest all my otherwise-cross-platform C++ files in order to work effectively. My game and its data objects are all C++ and I'm using them within Cocoa just like I would if this was a C++ application--they work as retvals, arguments, class members, whatever. Everything works just fine and I keep my guarantees of constness and I don't have to modify any of my C++ code to work with Cocoa.

I mean, I still don't like Objective-C on an aesthetic level, but being able to just throw messages at objects that may or may not exit, let alone be able to act on them, is pretty awesome for quickly writing fault-tolerant UIs. I wouldn't do anything data-critical within it, but it's a really inspired choice for UI work.

Thanksgiving sale is on. 'turkey2013' coupon code for 50% off ebooks/screencasts/audio books.

http://pragprog.com/promotions

Well, there goes my wallet then.

trueheart78 wrote:

Thanksgiving sale is on. 'turkey2013' coupon code for 50% off ebooks/screencasts/audio books.

http://pragprog.com/promotions

Someone go buy
http://pragprog.com/book/elixir/prog...
so that I may live vicariously! No time to learn Elixir as I've decided to actually finish my project of adding const to Mono so I'll have something semi-impressive on my github in case I need a job (layoffs are coming soon where I work).

I've never bought an ebook version of a technical book. Anyone have any impressions on the format as compared to a standard book?

RolandofGilead wrote:

Elixir enthusiasm

My new job has kept me busy enough that I haven't been addressing my side projects, but I definitely want to pick up Hullabaloo again before long. I write a lot of Coffeescript at work and it makes me miss Elixir.

To go back to Bonus' item from last week - whats wrong with not generating a full spark-line? Why not show the user that "this is all the data that we have" for those cases where you don't meet the necessary number of segments?

billt721 wrote:

I've never bought an ebook version of a technical book. Anyone have any impressions on the format as compared to a standard book?

PragProg is pretty awesome - they provide epub, mobi & PDF formats. They'll push to Dropbox, as well as kindle, and you get all updates/errata automatically.

McIrishJihad wrote:

To go back to Bonus' item from last week - whats wrong with not generating a full spark-line? Why not show the user that "this is all the data that we have" for those cases where you don't meet the necessary number of segments?

Nothing at all. That's actually exactly how it behaves in my solution if you pass in, for example (0, 7, 10).

I thought I had a problem with buying games & never playing them. Then I looked at my digital book pile from PragProg - it’s getting scary.

My cart alone is enough to make me woozy, but they have so many good titles.

Well it looks like it's time to finally grab Pragmatic Programmer and check that off the "things I should do" list. Thanks for mentioning their sale!

shoptroll wrote:

Well it looks like it's time to finally grab Pragmatic Programmer and check that off the "things I should do" list. Thanks for mentioning their sale!

I've got the hard copy - bought it years ago. It's good, it's still relevant, and I found it more satisfying than Code Complete.

Ok, bought the following (after looking at what I have to read that I haven't yet, [i]I was okay with it):

Node.js the Right Way (eBook)
Seven Concurrency Models in Seven Weeks (eBook)
Seven Databases in Seven Weeks (eBook)
Seven Languages in Seven Weeks (eBook)
Seven Web Frameworks in Seven Weeks (eBook)
The Dream Team Nightmare (eBook)

Seven Languages in Seven Weeks is dreamy.

Stuff I am looking at getting:
Programming Elixir
Node.js the Right Way
Functional Programming in Java

Maybe Programming Erlang 2nd ed. (I have the first edition). I am interested in what is new in Erlang but really don't have the time to stay up to date on my own.

I bought the new Clojure books:

Web Development with Clojure
Functional Programming Patterns in Scala and Clojure

Picked up this sale:

For me:
1 × Async JavaScript (eBook) (download)
1 × Node.js the Right Way (eBook) (download)
1 × Seven Concurrency Models in Seven Weeks (eBook) (download)

For the kids
1 × Practical Programming (2nd edition) (eBook) (download)
1 × 3D Game Programming for Kids (eBook) (download)

I picked up

Node.js
Pragmatic Unit Testing (C#/NUnit)
Pragmatic Version Control (Subversion)
Raspberry Pi
Web Development Recipes

Seeing that I'm doing services work for a CMS company in the .Net/C# space, and we use SVN, hopefully this means more automation and less repetitive grunt work for my team.

On Cyber Monday, Apress's ebooks will all be $15.

Worth noting due to the recent release of the book Expert JavaScript, which I'm hearing from some very good people is one of the best JS books in a while, and dives into the updates in ECMAScript 6.

I bought the Seven Concurrency Models book and while it's still in beta I'm not super thrilled with it. It's not bad, but it's very, very basic (and I was hoping for something basic-to-moderate).

tboon wrote:

Functional Programming in Java

This should be a one-pager:

Don't.

And then links to Scala, Clojure, JRuby, Jython and Groovy.

Mixolyde wrote:
tboon wrote:

Functional Programming in Java

This should be a one-pager:

Don't.

And then links to Scala, Clojure, JRuby, Jython and Groovy.

It's talking about Java 8 stuff. Lambdas, Streams, etc. Java 8 looks to be much better in that regard, which, granted, is not a very high bar to exceed. About 1 cm tall last I measured. And that is using Guava.

But, since 8 is coming out next year, my current plan is for work to upgrade to 8 next summer, and there is no way my minions would or could swap over to Scala (much less Clojure!), I gotta learn this stuff.

So nyah-nyah!

tboon wrote:

and there is no way my minions would or could swap over to Scala (much less Clojure!)

Maybe there'll be a Cyber Monday sale on new minions.

*Legion* wrote:
tboon wrote:

and there is no way my minions would or could swap over to Scala (much less Clojure!)

Maybe there'll be a Cyber Monday sale on new minions. :)

God I hope so.

tboon wrote:
*Legion* wrote:
tboon wrote:

and there is no way my minions would or could swap over to Scala (much less Clojure!)

Maybe there'll be a Cyber Monday sale on new minions. :)

God I hope so.

I think you have to build more Overlords first.

RolandofGilead wrote:
trueheart78 wrote:

Thanksgiving sale is on. 'turkey2013' coupon code for 50% off ebooks/screencasts/audio books.

http://pragprog.com/promotions

Someone go buy
http://pragprog.com/book/elixir/prog...
so that I may live vicariously! No time to learn Elixir as I've decided to actually finish my project of adding const to Mono so I'll have something semi-impressive on my github in case I need a job (layoffs are coming soon where I work).

Done. New hammer, lots of nails.