Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If I understand dataflow's example correctly you don't need the Select at the end:

  var x = Enumerable.Range(1,50)
          .Where((num, index) => num % 4 == 1 && index % 3 == 0)
          .Skip(2)
          .ToArray();
That computes the same thing as their Python snippet: [25,37,49]. Of course, what this is actually computing is whether the number is congruent to 1 modulo 4 and 3 so it was a weird example, but here's how you'd really want to write it (since a number congruent to 1 modulo 4 and 3 is the same as being congruent to 1 module 12):

  var x = Enumerable.Range(1,50)
          .Where(num => num % 12 == 1)
          .Skip(2)
          .ToArray();
Rewriting that Python example to be a bit clearer for a proper one-to-one comparison:

  y = [t for t in range(1, 50, 4) if t % 3 == 1][2:]
That enumerate wrapper was unnecessary. I don't recall a way, in LINQ, to generate only every 4th number in a range, but I also haven't used C# in a few years so my memory is rusty on LINQ anyways.


You're right, the maths simplifies it a lot. I rushed out a one-liner without much analysis, and eventually come to the same conclusion.

There's no Range method that takes (start, stop, step) but it's trivial enough to write one, it's a single for loop and yield return statement.

We can even trigger the python users by doing it in one line ;)

    public static class CustomEnumerable { public static IEnumerable<Int32> Range(int start, int stop, int step) {for (int i = start; i < stop; i+=step) yield return i;}}
Try writing your function definitions on one line in python!


> LINQ, to generate only every 4th number in a range

Maybe something like this?

    Enumerable.Range(0,49).Select(x => 4*x + 1)


Yeah, that would work, throw it before the Where clause and change 49. Range here doesn't specify a stopping point, but a count of generated values (this makes it not quite the same as Python's range). So you'd want:

  Enumerable.Range(0,13).Select(x => 4 * x + 1).Where((e, i) => i % 3 == 0).Skip(2)
And that's equivalent to the original, short of writing a MyRange that combines the first Range and Select. Still an awful lot of work for generating 3 numbers.


> num % 12

> That enumerate wrapper was unnecessary.

I'm surprised you didn't go all the way and just write

  x = [25, 37, 49]
and tell me the rest of the code was unnecessary!


I mean, was it necessary? Your original Python expression was pretty obfuscated for such a simple calculation.


Are you actually suggesting I didn't realize I could've written x = [25, 37, 49], or what?

Surely the point of the example wasn't "find the optimal way to calculate that particular list of numbers"?


No, I'm suggesting that your original example was a great example of obfuscated Python. Even supposing that you wanted to alter the total number of values generated and the number of initial values to skip, you're doing unnecessary work and made it more convoluted than necessary:

  def some_example(to_skip=2, total_count=3):
    return [n * 12 + 1 for n in range(to_skip, to_skip+total_count)]
There you go. Change the variable names that I spent < 1 second coming up with and that does exactly the same thing without the enumeration or discarding values. In a thread on how computer speed is wasted on unnecessary computation, it seems silly that you're arguing in favor of unnecessary work and obfuscated code.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: