Search code examples
listiteratorjuliaproductempty-list

How to set product to 0 if its a product over an empty list in Julia?


In my code I use Julia's prod() function to do a product over the elements of a list. However, sometimes that list is empty, in which case I want prod(myList) to just return 0 (or any fixed number like 1). I tried searching online, but the only things I could find were for iterators or stuff like that.

I'm using Julia version 1.5.2.


Solution

  • Would a simple ternary operator work for your case?

    isempty(my_list) ? 0 : prod(my_list)