Int 확장 함수를 사용해 팩토리얼을 구현해보자.
fun Int.factorial(): Int = (1..this).product()
fun Iterable<Int>.product(): Int = fold(1) { acc, i -> acc * i}
이때 팩토리얼을 수학 기호처럼 표현하고 싶다면 ! 연산자를 오버로딩하여 만들어 낼 수 있다.
operator fun Int.not() = factorial()
print(!6) // 7200(6!)
위 구현 방식엔 문제점이 있다.
print(6.not()) // 7200