Factorial in the library?


#1

Trying to do combinatorics, factorial is really rather essential. Yes it can be calculated using reduce. However given there is a sum function in the library shouldn’t there be a product function.


#2

I believe product is required far less often than sum, that’s why we haven’t included it in the standard library.

Could you illustrate the use cases of product function?


#3

The really obvious example is factorial, which is an important function for all work on combinatorics. Anyone working on any data science application that uses combinatorics will have an implementation of factorial – preferably as a postfix exclamation mark operator.

I’ll see if I can hunt out others.

Obviously this is easy to implement with reduce of fold, it is just the hassle, especially given many languages have a product function in the standard library. It is about surprise that it isn’t there in Kotlin more than anything.


#4

Functions like product() could probably go into a kotlinx.math module. There are a lot of functions that could also go into this module, e. g. cartesianProduct() (I use this often) and extension functions for mathematical functions from java.lang.Math [1] (this was requested a few times) along with functions like cot() (which is not in Math).

[1] https://gist.github.com/MichaelRocks/f8f66230707bcd88a239


#5

I agree that the functions are too specific to be included to Stdlib. Kotlinx.math is better place


#6

This works for me. I will have a look at the kotlinx workflow.