An open API service indexing awesome lists of open source software.

https://github.com/czyzby/kotlin-times

:octocat: Kotlin reinvented.
https://github.com/czyzby/kotlin-times

joke kotlin

Last synced: 3 months ago
JSON representation

:octocat: Kotlin reinvented.

Awesome Lists containing this project

README

          

```
____________________________________________________
| ____ __. __ .__ .__ |
| | |/ _|_____/ |_| | |__| ____ 07/2016 |
| | / / _ \ __\ | | |/ \ |
| | | \( (_} ) | | |_| | | \ |
| |____|__ \____/|__| |____/__|___| / |
| \/ \/ |
| __ .__ |
| _/ |_|__| _____ ____ ______ |
| \ __\ |/ \_/ __ \ / ___/ |
| | | | | Y Y \ ___/ \___ \ |
| |__| |__|__|_| /\___ >____ ) |
| \/ \/ \/ |
| |
| - - BREAKING NEWS! - - |
| A software engineering masterpiece released! |
| ___________________________ |
| | fun thisReallyWorks() { | "Kotlin literally |
| | 50 times { | becomes another |
| | println(""" | language", said |
| | | some random Kotlin |
| | This would print | developer. After |
| | | many long years of |
| | oooooooo .oooo. | careful coding, |
| | dP"````"P d8P'`Y8b | kotlin-times says |
| | d88888b. 888 888 | goodbye to the lab |
| | `Y88b 888 888 | and finally sees |
| | ]88 888 888 | the light of day. |
| | o. .88P `88b d88' | "Pure awesomeness", |
| | '8bd88P" `Y8bd8P' | said the other guy. |
| | | - - - /
| | times! | More info and /
| | """) | poor quality /
| | } | screen captures /
| | } | on page 42. /
| |_________________________| /\/
\ /
\ /\ In other news: Java still verbose/
\/ \/\ /\ /\/\ /\/\ /\/\ /\ /
\ / \/ \/ \/ \ / \/
\/ \/
```

[![Maven Central](https://img.shields.io/maven-central/v/com.github.czyzby/kotlin-times.svg)](http://mvnrepository.com/artifact/com.github.czyzby/kotlin-times)
![Just kidding](https://img.shields.io/badge/coverage-101%25-red.svg) [![Kotlin](https://img.shields.io/badge/kotlin-1.0.3-orange.svg)](http://kotlinlang.org/)

You might have thought that Kotlin cannot get any better - but *now* you have found *this* library. Thanks to
`kotlin-times` your Kotlin applications will never be the same. Enter the future of programming.

# Kotlin Times

This library, carefully crafted by our Kotlin experts, extends `Byte`, `Short`, `Int` and `Long` types with `infix`
methods sharing `times` identifier. It effectively allows its users to set up simple and readable loops with a pleasant
Kotlin*-ish* syntax:
```Kotlin
50 times {
println("Will be printed 50 times!")
}
```

You can access current iteration index as well! It starts at `0` and ends at `n-1`.
```Kotlin
10 times { index ->
println("This is the $index run.")
}
```

As usual in case of single-parameter lambdas, current iteration index can be also accessed with `it`.
```Kotlin
10 times {
println("This is the $it run.")
}
```

If you prefer even less verbose operators, `times` happens to be the valid name of the `*` operator, which - combined
with a number - is a pretty readable and obvious way of saying that you want to repeat a loop block for a certain amount
of times. Lucky us.
```Kotlin
10 * {
println("Equivalent to '10 times {...}'")
}
```

Thanks to the fact that these are all `inline` functions, there is little-to-none runtime overhead for their usage -
they compile to pretty much the same byte code as regular Kotlin loops (or Java ones, for that matter). Needless to say,
these should be as fast as any other simple loop created with the official API. In fact, methods similar to these
obviously *should* be a part of the official API, available out of the box in the standard library. Duh.

## Is it stable?

Definitely. We are proud to announce that **all** of the extension methods are fully tested on every possible number in
each value range. 100% test coverage comes at a price: the full test suite can run for hours due to ~~integers and longs
being big~~ their admirable complexity.

## Why would you even do that?

Joking aside, I kind of missed the good old classic Java `for(int i=0; i
// Do something.
}

(0 until n).forEach { i ->
// Do something.
}
```
This might be my personal favorite, but it still seems more verbose than necessary.

```Kotlin
inline operator fun IntRange.invoke(action: (Int) -> Unit) {
for (i in this) action(i)
}

// Given the operator extension, you can do this:
(0..n-1) { i ->
// Do something.
}
```
I pity maintainers of your code if you do this, but I have got to admit it looks unsettlingly tempting.

```Kotlin
repeat(n) { i ->
// Do something.
}
```
*...Um, wait, is this library actually necessary?*

## How do I include this beauty in my project?

Easily - its already on Maven Central.

### Maven
```XML

com.github.czyzby
kotlin-times
1.0

```

### Gradle
```Groovy
compile 'com.github.czyzby:kotlin-times:1.0'
```

```Groovy
compile group: 'com.github.czyzby', name: 'kotlin-times', version: '1.0'
```

### Working with the sources

Since this library consists of a [single Kotlin file](src/main/kotlin/kt/times/times.kt), you can just copy the
extension methods to your application and modify them however you like. You can also clone this repository and use
[Gradle](http://gradle.org/) to set up this project, if you really want to.

The code is dedicated to public domain, so feel free to do anything. Just so you know.

#### Variations

If you're feeling brave, try refactoring `times` to something like `x` for this awkwardly short syntax without
overloaded operators: `8 x { println("Will work just fine.") }`. Life is a sandbox.

## The future

Our experts are currently working on extension methods for other numeric types. Hopefully, if the project is not aborted
by then, the official `2.0` version will be released in 2042.

## Contribution

If you do not think that `kotlin-times` is perfect in every way for some reason, you can create an issue or open a pull
request. Thanks in advance.