Kotlin / kotlin-numpy

Kotlin bindings for NumPy
Apache License 2.0
316 stars 11 forks source link

Would be nice if KtNDArray implemented Iterable. #16

Open alshan opened 4 years ago

alshan commented 4 years ago

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

devcrocod commented 4 years ago

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.

alshan commented 4 years ago

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.

devcrocod commented 4 years ago

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

alshan commented 4 years ago

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.

devcrocod commented 4 years ago
  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.

  2. 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.

alshan commented 4 years ago

Yes, (1) certainly is not looking good.