Giter Site home page Giter Site logo

kotlin-bucket's Introduction

Kotlin Bucket

Random stuff in Kotlin language

Not null check for multiple arguments.

In this implementation, one can use existing classes Pair and Triple.

fun <A, B> Pair<A?, B?>.takeIfNotNull(): Pair<A, B>? {
  val a = first ?: return null
  val b = second ?: return null
  return Pair(a, b)
}

fun <A, B, C> Triple<A?, B?, C?>.takeIfNotNull(): Triple<A, B, C>? {
  val a = first ?: return null
  val b = second ?: return null
  val c = third ?: return null
  return Triple(a, b, c)
}
fun main() {
  val a: Int? = 32
  val b: Long? = 42
  (a to b).takeIfNotNull()?.let { (a, b) ->
    println("a: $a, b: $b")
  }
}

For shortcut, one could use:

inline fun <A, B, R> Pair<A?, B?>.runIfNotNull(block: (A, B)->R): R? {
  return takeIfNotNull()?.let { (a, b) -> block(a, b) }
}

inline fun <A, B, C, R> Triple<A?, B?, C?>.runIfNotNull(block: (A, B, C)->R): R? {
  return takeIfNotNull()?.let { (a, b, c) -> block(a, b, c) }
}
fun main() {
  val a: Int? = 32
  val b: Long? = 42
  (a to b).runIfNotNull { a, b ->
    println("a: $a, b: $b")
  }
}

Pair, Triple, Quadruple creation

In standard library only Pair has pleasant way of constructing it. Here is what one can do:

Optional: define Quadruple, Quintuple etc...

data class Quadruple<A, B, C, D> (val first: A, val second: B, val third: C, val fourth: D): Serializable {
  override fun toString(): String = "($first, $second, $third, $fourth)"
}
infix fun <A, B> A.nd(second: B): Pair<A, B> = this to second
infix fun <A, B, C> Pair<A, B>.rd(third: C) = Triple(first, second, third)
infix fun <A, B, C, D> Triple<A, B, C>.th(fourth: D) = Quadruple(first, second, third, fourth)
fun main() {
  val pair = 1 nd 2
  val triple = 3 nd 4 rd 5
  val anotherTriple = pair rd 6
  val quadruple = 7 nd 8 rd 9 th 10
}

kotlin-bucket's People

Contributors

domazey avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.