kotlin-numpy icon indicating copy to clipboard operation
kotlin-numpy copied to clipboard

Would be nice if KtNDArray implemented Iterable.

Open alshan opened this issue 5 years ago • 6 comments

Now when passing 1d KtNDArray as a data series to lets-plot user have to explicitly call toList().

alshan avatar Jun 28 '20 23:06 alshan

The problem with the implementation of the KtNdArray Iterable interface is that Iterable methods are for the most part extensions functions and can't be override.

devcrocod avatar Jun 30 '20 13:06 devcrocod

Not sure why it is necessary to override methods like toList. KtNDArray will provide an iterator (ListIterator maybe) and all the rest will work out of the box IMO. Alternatively KtNDArray can implement List interface.

alshan avatar Jun 30 '20 15:06 alshan

Iterable has functions plus, minus, max, min, etc. Some of these features are required for ndarray.

devcrocod avatar Jun 30 '20 17:06 devcrocod

IMHO this is not an issue. When user works with KtNDArray all expressions have type KtNDArray and all extensions work as expected.

A code which doesn't know about kotlin numpy works with std Kotlin collections (all expressions have Iterable, List etc. types) and again all extensions work as expected.

I don't see how user would unintentionally mix those two environments and get confused.

alshan avatar Jul 01 '20 00:07 alshan

  1. For example: Iterable.plus returns a List and KtNDArray.plus returns a new KtNdArray. If KtNDArray will implement the Iterable interface, then in the following code val a = array(arrayOf(1, 2)) + array(arrayOf(3, 4)) the method that returns List will be used first. Explicit import and type will be required for to use KtNDArray.plus:
import org.jetbrains.numkt.core.KtNDArray
import org.jetbrains.numkt.math.plus
val a: KtNDArray<Int> = array(arrayOf(1, 2)) + array(arrayOf(3, 4))

Which complicates the use of the library.

  1. To implement Iterable, it is necessary that a primitive type (scalars) be returned during iteration. Currently, iterating returns an NdArray view. This is not the final version, I need to think about a better API iterator. It is necessary to give the user the ability to iterate over slices and scalars.

devcrocod avatar Jul 04 '20 22:07 devcrocod

Yes, (1) certainly is not looking good.

alshan avatar Jul 06 '20 15:07 alshan