# How about to simplify the factorial function name?

**URL:** <https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715>\
**Category:** Internals & Design\
**Created:** [May 8, 2022, 4:28pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715 "2022-05-08T16:28:05Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhiyuanzhai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhiyuanzhai/32/9519_2.png) [@zhiyuanzhai](https://discourse.julialang.org/u/zhiyuanzhai)\
**Post date:** [May 8, 2022, 4:28pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/1 "2022-05-08T16:28:05Z")

</div>

Recently I found it quite complicated to call the function `factorial()`. I feel it too long for such a function that is so frequently used in some situations. Can we make it simpler? Like `!(n)` (I found that the operator `!` does not have a method for `::Integer`), for example.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 8, 2022, 4:41pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/2 "2022-05-08T16:41:29Z")

</div>

> [@zhiyuanzhai](#):
>
> I feel it too long for such a function that is so frequently used in some situations.

I’ve never found the argument about length of functions/commands compelling, in any language. If you get tired of typing it’s probably a sign you need better tooling (for example some more help from your IDE), but bending a language because you don’t want to type to more is never a good idea.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 8, 2022, 4:45pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/3 "2022-05-08T16:45:16Z")

</div>

If you have some factorial-heavy code you can introduce an alias yourself.

```julia
julia> const ! = factorial
factorial (generic function with 7 methods)

julia> !(5)
120

```

For performance reasons it’s important to use `const`.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [May 8, 2022, 4:52pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/4 "2022-05-08T16:52:59Z")

</div>

Does this hide ! as negation? If so, everywhere?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 8, 2022, 5:24pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/5 "2022-05-08T17:24:15Z")

</div>

Just in your code. 🙂

Well, I guess that’s a little heavy-handed. In your own code it’s probably a better idea to commit some type piracy with

```julia
Base.:!(x::Int) = factorial(x)

```

but on the whole it would be advisable to alias it to something truly private.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 8, 2022, 5:25pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/6 "2022-05-08T17:25:21Z")

</div>

And then again it is debatable whether `!(5)` is truly more readable than `factorial(5)`, _especially_ in a language where `!` usually means boolean negation

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 8, 2022, 5:28pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/7 "2022-05-08T17:28:33Z")

</div>

If you have a high factorial density in your code I think it’s an arguable improvement but since it definitely means something different from negation it will never be considered for Base.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 8, 2022, 5:36pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/8 "2022-05-08T17:36:21Z")

</div>

If you really want to commit a crime, with this

```julia
julia> Base.:(*)(n::Int, ::typeof(Base.:(!))) = factorial(n)

julia> 5(!)
120

```

at least you get the order right (although you need too many parentheses) 😜 But again, don’t do it!

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [May 8, 2022, 6:10pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/9 "2022-05-08T18:10:04Z")

</div>

Thanks. It wouldn’t ever happen in my code. I was just curious how much potential damage it would cause.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 8, 2022, 6:29pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/10 "2022-05-08T18:29:49Z")

</div>

> [@GunnarFarneback](#):
>
> ```julia
> julia> !(5)
> 120
> 
> ```

You can just do

```julia
julia> !5
120

```

😃

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 8, 2022, 6:32pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/11 "2022-05-08T18:32:11Z")

</div>

> [@giordano](#):
>
> ```julia
> julia> 5(!)
> 120
> 
> ```

[I think actually `5!` could be made to do this, as in with Unitful.jl, but I’m not sure.]

You can get this with one letter, and readable in a way, i.e. using the correct functional form with the related gamma function:

```julia
julia> Γ(6) # Yes, you still have to type \Gamma<TAB>
120

after defining:

julia> const Γ(x::Integer) = factorial(x-1)
Γ (generic function with 2 methods)

```

The gamma function is there:

> **[GitHub - JuliaMath/SpecialFunctions.jl: Special mathematical functions in Julia](https://github.com/JuliaMath/SpecialFunctions.jl)**
>
> Special mathematical functions in Julia. Contribute to JuliaMath/SpecialFunctions.jl development by creating an account on GitHub.

I’m not going to propose adding `const Γ(x::Number) = gamma(x)` there (which gives Float64), I suppose the Greek letters were considered too valuable to also claim one of them.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 8, 2022, 6:34pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/12 "2022-05-08T18:34:56Z")

</div>

Hm. Maybe:

```julia
julia> struct Factorial end

julia> const _! = Factorial()
Factorial()

julia> Base.:*(n::Integer, ::Factorial) = factorial(n)

julia> 5_!
120

```

?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 8, 2022, 6:47pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/13 "2022-05-08T18:47:04Z")

</div>

> [@Palli](#):
>
> I think actually `5!` could be made to do this

It doesn’t parse:

```julia
julia> 5!
ERROR: syntax: extra token "!" after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [May 8, 2022, 8:13pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/15 "2022-05-08T20:13:44Z")

</div>

I like it.

```julia-repl
julia> struct Factorial end
julia> const ❢ = Factorial()
julia> Base.:*(n::Integer, ::Factorial) = factorial(n)
julia> 5❢
120

```

🙂

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [May 8, 2022, 8:37pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/16 "2022-05-08T20:37:28Z")

</div>

That’s very clever but isn’t `Base.*` supposed to be for a function that behaves like multiplication?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 8, 2022, 9:30pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/17 "2022-05-08T21:30:43Z")

</div>

Not really much danger in abusing it here for nice juxtaposition notation

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 8, 2022, 9:48pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/18 "2022-05-08T21:48:07Z")

</div>

> [@cormullion](#):
>
> `const ❢ = Factorial()`

Or `const ❗ = Factorial()`, which can be typed by `\:exclamation:<tab>`. 😉

Honestly, though, I’m a bit curious about what kind of code makes such heavy use of `factorial` that a shortcut is worth it.

(A common [mistake is calling `factorial` when evaluating series](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/4) formulas. See also [this tutorial](https://nbviewer.org/github/mitmath/18S096/blob/iap2017/pset3/pset3-solutions.ipynb#Taylor-series).)

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [May 8, 2022, 10:11pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/19 "2022-05-08T22:11:31Z")

</div>

Mine’s the ‘fat-torial’… 😀

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 8, 2022, 10:51pm UTC](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/20 "2022-05-08T22:51:49Z")

</div>

> [@stevengj](#):
>
> Honestly, though, I’m a bit curious about what kind of code makes such heavy use of `factorial` that a shortcut is worth it.
> 
> (A common [mistake is calling `factorial` when evaluating series](https://discourse.julialang.org/t/sinc-function-and-sinc-neural-networks-and-function-approximation-for-the-former/46483/4) formulas. See also [this tutorial](https://nbviewer.org/github/mitmath/18S096/blob/iap2017/pset3/pset3-solutions.ipynb#Taylor-series).)

Sometimes you want to optimize for performance and or numerical accuracy. Other times you know that doesn’t matter in your case and you just want to write pretty code
