News for hackers. Stuff that matters.
Re:Ask HN: What is the most beautiful piece of code you've e... by Jarqwiz on Monday October 28, 2019 @04:04AM (#21375084)
For me it's a function that calculates factorial using iterators in Rust:

  fn factorial(i: u64) -> u64 {
      (1..=i).product()
  }
In almost every other language this code would look messy or use some terrible recursion.

For example in C it would look something like this:

  long factorial(int n)
  {
      int c;
      long result = 1;

      for (c = 1; c <= n; c++)
          result = result * c;
 
      return result;
  }
Or with recursion:

  long factorial(int n)
  {
      if (n == 0)
          return 1;
      else
          return(n * factorial(n-1));
  }
In any case, I thin Rust looks better in every way with its cleaner syntax.
Re:Ask HN: What is the most beautiful piece of code you've e... by cakoose on Monday October 28, 2019 @04:26AM (#21375176)
> In almost every other language this code would look messy or use some terrible recursion.

Nah, lots of other languages can do this.

Python:

    def factorial(i):
        return reduce(operator.mul, range(2, i+1), 1)
Ruby:

    def factorial(i)
        (2..i).reduce(1, :*)
    end
Haskell:

    factorial n = foldl (*) 1 [2..n]
Re:Ask HN: What is the most beautiful piece of code you've e... by estomagordo on Monday October 28, 2019 @05:14AM (#21375385)
For Python, we don't need to supply the optional initializer 1. And reduce resides in the functools namespace (much like mul is in operator). I assume we're not talking about that peculiar dialect of Python that is no longer supported a few months from now.
Re:Ask HN: What is the most beautiful piece of code you've e... by messe on Monday October 28, 2019 @07:03AM (#21375866)
Not only that but in python 3.8, it can be written:

    def factorial(n):
        return math.prod(range(1,n+1))
Re:Ask HN: What is the most beautiful piece of code you've e... by estomagordo on Monday October 28, 2019 @07:53AM (#21376075)
Nice!
Re:Ask HN: What is the most beautiful piece of code you've e... by cakoose on Monday October 28, 2019 @05:07PM (#21381681)
> For Python, we don't need to supply the optional initializer 1.

Without the initializer, factorial(0) doesn't work.

Re:Ask HN: What is the most beautiful piece of code you've e... by rnhmjoj on Monday October 28, 2019 @04:35AM (#21375203)
In haskell you can also use the `product` function:

    factorial n = product [1..n]
Re:Ask HN: What is the most beautiful piece of code you've e... by enriquto on Monday October 28, 2019 @05:33AM (#21375475)
the first C version looks the best to me, it is extremely clear and concise. For a slight improvement, declare the variable c inside the loop.

Yet, since 20! is the last factorial representable in a u64, there is not much a point for these functions, and you should definitely check for n<21. It would be more elegant to store a lookup table with the 21 possible results.

In practice you would want the logarithm of the factorial, that is computed by the "lgamma" function from the C standard math.h. Is such a thing available in rust?

Edit: if you use doubles (which is more reasonable for that use case), you can also do that:

     double factorial(double n) { return tgamma(1+n); }
Re:Ask HN: What is the most beautiful piece of code you've e... by emmelaich on Monday October 28, 2019 @04:43AM (#21375235)
That Rust is very nice.

But a more idiomatic C version would be

   long factorial(int n)
   {
     long result = 1;
     for (int c = 1; c <= n; c++)
       result *= c;
     return result;
   }
Re:Ask HN: What is the most beautiful piece of code you've e... by sanatgersappa on Monday October 28, 2019 @05:28AM (#21375453)
Clojure:

(defn factorial [n] (reduce *' (range 1 (inc n))))

Re:Ask HN: What is the most beautiful piece of code you've e... by ansis- on Monday October 28, 2019 @06:02AM (#21375595)
C can be quite elegant:

  long factorial(int n) {
          return n == 0 ? 1 : n * factorial(n - 1);
  }