Skip to content

Instantly share code, notes, and snippets.

@naixx
Last active November 7, 2022 15:32
Show Gist options
  • Select an option

  • Save naixx/9d94c1498c4d45ffda3a to your computer and use it in GitHub Desktop.

Select an option

Save naixx/9d94c1498c4d45ffda3a to your computer and use it in GitHub Desktop.

Revisions

  1. naixx revised this gist Jan 16, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ternary.kt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    class Ternary<T>(val expr: Boolean, val then: T) {
    public fun div(elze: T): T = if (expr) then else elze
    class Ternary<T>(private val expr: Boolean, private val then: T) {
    infix operator fun div(elze: T): T = if (expr) then else elze
    }

    public fun <T> Boolean.mod(a: T): Ternary<T> = Ternary(this, a)
    infix operator fun <T> Boolean.rem(a: T): Ternary<T> = Ternary(this, a)
  2. naixx revised this gist Oct 27, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Readme.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ Kotlin is very expressive language, but like Scala it lacks of ternary operator.
    ```kotlin
    val result = if (myExpression) 1 else 2
    ```
    Compared to classical Java or C++
    Compared to a classical Java or C++ variant
    ```java
    int result = myExpression ? 1 : 2
    ```
  3. naixx revised this gist Oct 27, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Readme.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ Kotlin is very expressive language, but like Scala it lacks of ternary operator.
    ```kotlin
    val result = if (myExpression) 1 else 2
    ```
    Copared to classical Java or C++
    Compared to classical Java or C++
    ```java
    int result = myExpression ? 1 : 2
    ```
  4. naixx revised this gist Nov 28, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions Readme.md
    Original file line number Diff line number Diff line change
    @@ -12,4 +12,9 @@ Unlike Scala, Kotlin allows only fixed names for operators, so we cant fully rep
    ```kotlin
    val result = myExpression % 1 / 2
    ```
    If you want to use complex boolean expression, you can wrap it in braces
    ```kotlin
    val result = (a == null && b > 5) % 1 / 2
    ```

    The impcat I see here is temporaty object creation, probably `inline` can't help.
  5. naixx revised this gist Nov 27, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Readme.md
    Original file line number Diff line number Diff line change
    @@ -12,3 +12,4 @@ Unlike Scala, Kotlin allows only fixed names for operators, so we cant fully rep
    ```kotlin
    val result = myExpression % 1 / 2
    ```
    The impcat I see here is temporaty object creation, probably `inline` can't help.
  6. naixx revised this gist Nov 27, 2014. 2 changed files with 5 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions elvis.kt
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@

    class Elvis<T>(val expr: Boolean, val then: T) {
    public fun div(elze: T): T = if (expr) then else elze
    }

    public fun <T> Boolean.mod(a: T): Elvis<T> = Elvis(this, a)
    5 changes: 5 additions & 0 deletions ternary.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    class Ternary<T>(val expr: Boolean, val then: T) {
    public fun div(elze: T): T = if (expr) then else elze
    }

    public fun <T> Boolean.mod(a: T): Ternary<T> = Ternary(this, a)
  7. naixx revised this gist Nov 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Readme.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    * Motivation *
    # Motivation

    Kotlin is very expressive language, but like Scala it lacks of ternary operator. Currently language provided alternative `if` looks a bit verbose.
    ```kotlin
  8. naixx revised this gist Nov 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Readme.md
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,6 @@ Copared to classical Java or C++
    int result = myExpression ? 1 : 2
    ```
    Unlike Scala, Kotlin allows only fixed names for operators, so we cant fully reproduce classic syntax, but we can have something similar
    '''kotlin
    ```kotlin
    val result = myExpression % 1 / 2
    ```
  9. naixx created this gist Nov 27, 2014.
    14 changes: 14 additions & 0 deletions Readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    * Motivation *

    Kotlin is very expressive language, but like Scala it lacks of ternary operator. Currently language provided alternative `if` looks a bit verbose.
    ```kotlin
    val result = if (myExpression) 1 else 2
    ```
    Copared to classical Java or C++
    ```java
    int result = myExpression ? 1 : 2
    ```
    Unlike Scala, Kotlin allows only fixed names for operators, so we cant fully reproduce classic syntax, but we can have something similar
    '''kotlin
    val result = myExpression % 1 / 2
    ```
    6 changes: 6 additions & 0 deletions elvis.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@

    class Elvis<T>(val expr: Boolean, val then: T) {
    public fun div(elze: T): T = if (expr) then else elze
    }

    public fun <T> Boolean.mod(a: T): Elvis<T> = Elvis(this, a)