Kakoune-kotlin.kak - Kotlin language support

Due date: August 18, 2019

Package

  • kotlin.kak

Module

  • kotlin.keywords
  • kdoc.keywords
  • kakoune.faces
  • kakoune.commands
  • kakoune.keys

Problem Domain

  • Minimise the use of strings with a higher level of abstraction
  • Correct indentation on keywords and tokens
  • Loosely couple modules

Requirements

Authorisation

  • Me
  • Myself, and
  • I

Scope

Limitations


Outside the scope [Future project]

keywords

inline fun <reified T : Enum<T>> enumKeywordAlternationGroup(decapitalize: Boolean): AlternationGroup {
    return enumValues<T>()
            .joinToString(prefix = "(", postfix = ")", separator = "|")
            { it.toString() }.also { when { decapitalize -> it.decapitalize() } }
}

fun keywordAlternationGroup(size: HowManyKeywords): AlternationGroup {
    require(size > 0) { "$size > 0 is false" }

    if (size < 2) return "(%s)"

    val sb = StringBuilder()

    for (k in 0 until size) {
        val isFirst = { k == 0 }
        val isLast = { k == size - 1 }

        when {
            isFirst() -> sb.append("(%s|")
            isLast()  -> sb.append("%s)")
            else      -> sb.append("%s|")
        }
    }
    return sb.toString()
}

fun String.wordBoundaryFrontAndBack() = "\\b$this\\b"

Note:
[1] Still in the design phase hoping to move into TDD next week time permitting.

@duncan kt should be a different project no?

1 Like

yep working through it will have it all sorted on projects completion. thanks alex.

Waiting for my brain to catch up to my ambition.

Reading list is below working through chapter 5 The joy of Kotlin. The book provides a nice bridge between Kotlin and Haskell. Will likely upgrade Kakoune to the latest release version around Xmas break. Back and forth between Kotlin and Haskell and enjoying the challenge, thanks for the push @SolitudeSF.

Dmitrij Žemerov, Isakova, S, Andrej Breslav & Manning Publications 2017, Kotlin in action, Manning, Shelter Island, Ny. https://www.manning.com/books/kotlin-in-action

Samuel, S & Bocutiu, S 2017, Programming Kotlin : familiarize yourself with all of Kotlin’s features with this in-depth guide, Packt Publishing, Birmingham, Uk. https://www.packtpub.com/application-development/learn-kotlin-programming-second-edition

Pierre Yves Saumont 2019, The joy of Kotlin, Manning Publications Co, Shelter Island, Ny. https://www.manning.com/books/the-joy-of-kotlin

Hutton, G. and Cambridge University Press (2018). Programming in Haskell. Cambridge: Cambridge University Press. https://www.cambridge.org/core/books/programming-in-haskell/8FED82E807EF12D390DE0D16FDE217E4

Bird, R 2014, Thinking Functionally with Haskell, Cambridge University Press. https://www.cambridge.org/core/books/thinking-functionally-with-haskell/79F91D976F0C7229082325B41824EBBC

/**
 * foldl :: (b -> a -> b) -> b -> [a] -> b
 * foldl _ v [] = v
 * foldl f v (x:xs) = foldl f (f v x) xs
 *
 * foldl (#) v [x,y,z] = ((v # x) # y) # z
 *             [x,y,z] = (([] : x) : y) : z
 */
tailrec fun <A, B> foldl(
    f: (B) -> (A) -> B,
    v: B,
    xs: List<A>
): B = if (xs.isEmpty()) v
       else foldl(f, f(v) (xs.head()), xs.tail())

/**
 * foldr :: (a -> b -> b) -> b -> [a] -> b
 * foldr _ v [] = v
 * foldr f v (x:xs) = f x (foldr f v xs)
 *
 * foldr (#) v [x,y,z] = x # (y # (z # v))
 *             [x,y,z] = x : (y : (z : []))
 */
fun <A, B> foldr(
    f: (A) -> (B) -> B,
    v: B,
    xs: List<A>
): B = if (xs.isEmpty()) v
       else f(xs.head()) (foldr(f, v, xs.tail()))