Julia Community 🟣

Discussion on: Creating a simple function for factorial in Julia

Collapse
 
miguelraz profile image
Miguel Raz Guzmán Macedo

I liked this article - I wrote a similar one when I was starting out with Julia too!
And then a kind Teacher Assistant for my course came along (with a sneaky smile) and whispered something about some ternary operator...

myfact(n::Int) = n == 0 ? 1 : n*myfact(n-1)
Enter fullscreen mode Exit fullscreen mode

...Which creeped me out.

Vexed me!

But not as much as when I found out about the world of map, reduce, and folds, and all those nifty iterator methods that can get you out of a pinch:

myfact(n::Int) = prod(big(1):n)
Enter fullscreen mode Exit fullscreen mode