Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bringing MATLAB Syntax into Kotlin

Bringing MATLAB Syntax into Kotlin

This talk was presented in BlrKotlin's meetup on 22nd Feb, 2020. This talk gives an idea of how powerful Kotlin's Operator Overloading feature is and how best we can use it to do our job.

Chandra Sekhar Nayak

February 22, 2020
Tweet

More Decks by Chandra Sekhar Nayak

Other Decks in Technology

Transcript

  1. MATLAB • Purpose - Specific • Syntax - • Code

    - Scientist or Mathematician Kotlin • Purpose - Generic • Syntax - • Code - Programmer @iChanSek bit.ly/chansecode
  2. MATLAB (Pros) • Purpose - Specific • Syntax - •

    Code - Scientist or Mathematician Kotlin (Cons) • Purpose - Generic • Syntax - • Code - Programmer @iChanSek bit.ly/chansecode
  3. MATLAB (Cons) • Separate Tooling • Learning Curve Kotlin (Pros)

    • Same Tooling • No Learning Curve @iChanSek bit.ly/chansecode
  4. start..step..end @iChanSek bit.ly/chansecode operator fun IntRange.rangeTo(end: Int): IntProgression { return

    if (last > 0) first..end step last else IntProgression.fromClosedRange(first, end, last) } Operator Function
  5. (start..end).T @iChanSek bit.ly/chansecode val IntRange.T: Array<IntArray> get() = arr.T val

    IntRange.arr: IntArray get() = toArray() Extension Property
  6. (start..end).T @iChanSek bit.ly/chansecode val IntRange.T: Array<IntArray> get() = arr.T val

    IntRange.arr: IntArray get() = toArray() private fun IntRange.toArray(): IntArray { val result = IntArray(last - first + 1) forEachIndexed { index, i -> result[index] = i } return result } Extension Property
  7. matrix[rowSt..rowEnd, column] @iChanSek bit.ly/chansecode operator fun Array<IntArray>.get(rows: IntRange, col: Int):

    Array<IntArray> { if (col < 0) throw Exception(“. . .”) if (rows.last < rows.first) return arrayOf() return Array(1 + rows.last - rows.first) { intArrayOf(this[rows][it][col]) } } Operator Function
  8. matrix[rowSt..rowEnd, colSt..colEnd] @iChanSek bit.ly/chansecode operator fun Array<IntArray>.get(rows: IntRange, cols: IntRange):

    Array<IntArray> { if (rows.last < rows.first || cols.last < cols.first) return arrayOf() return Array(1 + rows.last - rows.first) { this[rows][it][cols] } } Operator Function
  9. val result = arr + 10 @iChanSek bit.ly/chansecode operator fun

    IntArray.plus(value: Int): IntArray { val resultArr = IntArray(size) forEachIndexed { index, i -> resultArr[index] = i + value } return resultArr } Operator Function
  10. val result = arr * 10 @iChanSek bit.ly/chansecode operator fun

    IntArray.times(value: Int): IntArray { val resultArr = IntArray(size) forEachIndexed { index, i -> resultArr[index] = i * value } return resultArr } Operator Function