Kotlin multiplatform bounded open/closed generic intervals.

Overview

Kotlin Multiplatform Bounded Open/Closed Generic Intervals

Publish snapshots Sonatype Nexus (Snapshots) Maven Central

Represent closed, open, or half-open, bounded intervals in Kotlin and perform common operations on them. Values covered by the interval can be of a different type than distances between those values.

For example, IntInterval has Int values and UInt distances:

val interval: IntInterval = interval( 0, 10, isEndIncluded = false )
val areIncluded = 0 in interval && 5 in interval // true
val areExcluded = 10 !in interval && 15 !in interval // true
val size: UInt = interval.size // 10

This protects against overflows (e.g. if size > Int.MAX_VALUE) but also offers better semantics. For example, you can create intervals for kotlinx datetime Instant values which are a Duration apart.

Interval Types

This library includes a generic base class Interval<T, TSize> which can be used to create intervals for any type. To achieve this, it directs type operations to IntervalTypeOperations which the constructor takes as a parameter.

The following interval types are included by default:

Type Values (T) Distances (TSize)
ByteInterval Byte UByte
ShortInterval Short UShort
IntInterval Int UInt
LongInterval Long ULong
FloatInterval Float Double
DoubleInterval Double Double
UByteInterval UByte UByte
UShortInterval UShort UShort
UIntInterval UInt UInt
ULongInterval ULong ULong
CharInterval Char UShort
Comments
  • Concrete interval type tests only indirectly rely on same `TypeOperations` than tested types

    Concrete interval type tests only indirectly rely on same `TypeOperations` than tested types

    To remove redundancy in setting up unit tests for each concrete Interval type (e.g. IntInterval implementing Interval<Int, UInt>), BasicTypeIntervalsTest.kt uses an inline function to retrieve the type operations for the type parameters (using getBasicTypeOperationsFor()).

    This manually needs to be kept in sync if the operations ever change.

    Is there a way to directly retrieve the matching TypeOperations from the concrete types? Maybe through a common factory interface on the companion objects?

    enhancement 
    opened by Whathecode 1
  • Double can't safely represent all sizes for Double intervals

    Double can't safely represent all sizes for Double intervals

    With the addition of TSize as a type parameter for Interval, overflows for almost all types are prevented by relying on TSize operations to prevent T values from overflowing.

    However, as commented when closing this issue:

    DoubleInterval can still overflow since there is no suitable type that can be specified to cover Double.MAX_VALUE - Double.MIN_VALUE. This is probably okay as an edge case, but this should probably throw an exception when accessing size rather than it being ignored.

    Currently, the IntervalTest.size_can_be_greater_than_max_value for DoubleIntervalTest succeeds since Double.Infinity is returned for both the expected and calculated value. This should not succeed for Double and probably give a runtime exception instead.

    bug 
    opened by Whathecode 1
  • Overflow for interval sizes larger than max of T

    Overflow for interval sizes larger than max of T

    Types with negative numbers can represent a - larger interval size than can be represented by the type itself

    Once generic size parameters are introduced for intervals (#1), this can likely be fixed by using the unsigned matching type as the size parameter.

    bug 
    opened by Whathecode 1
  • Add generic size parameter to `Interval`

    Add generic size parameter to `Interval`

    DateTime intervals will need this, since the size between two dates is not a date itself.

    Similarly, the size between two chars is probably better expressed as a byte

    enhancement 
    opened by Whathecode 1
  • Add `IntervalUnion` and `MutableIntervalUnion`

    Add `IntervalUnion` and `MutableIntervalUnion`

    An initial contract definition for IntervalUnion and preliminary concrete MutableIntervalUnion which lives up to the contract by restricting which intervals can be added.

    To help verify the IntervalUnion contract, I included the Interval.intersects(), Interval.reverse(), and Interval.nonReversed() methods.

    opened by Whathecode 0
  • Add reverse function

    Add reverse function

    A common operation is to reverse intervals: swapping start and isStartIncluded with end and isEndIncluded.

    Currently, I added an inner fun to assertIntersects in IntervalTest as part of ongoing IntervalUnion work. #22

    enhancement 
    opened by Whathecode 0
  • Add interval unions

    Add interval unions

    Common operations on intervals are to:

    • add/remove a value or interval
    • get complementary interval

    These may result not in a single interval, but in what can be called an interval union. This can be stored as a sorted list of non-overlapping intervals. Operations which may result in interval unions should return IntervalUnion, others can continue to return a normal Interval.

    enhancement 
    opened by Whathecode 0
  • Add separate `kotlinx.interval.datetime` library

    Add separate `kotlinx.interval.datetime` library

    Closes #12

    Turned the main kotlinx.interval into a submodule as part of a multi-module project.

    To be able to reuse the test base classes from other modules, I moved them to a new submodule kotlinx.interval.test. Rather than pulling in this dependency in each kotlinx.interval library, a test dependency to this library should likely be included as part of the interval.library-conventions gradle plugin (#14).

    Next up is adding kotlinx.interval.datetime which defines Interval<Instant, Duration>.

    opened by Whathecode 0
  • Include datetime intervals

    Include datetime intervals

    Include Interval<Instant, Duration>.

    Since this depends on the kotlinx datetime library, implement it as a separate module: kotlinx.interval.datetime.

    enhancement 
    opened by Whathecode 0
  • Release 1.0.0-alpha.1

    Release 1.0.0-alpha.1

    A first functional, but still unstable, version.

    Open/closed intervals with generic T and TSize and size property. Default intervals are included for all basic Kotlin types, e.g. IntInterval, DoubleInterval, etc.

    opened by Whathecode 0
  • Test `TypeOperations` in isolation of `Interval`

    Test `TypeOperations` in isolation of `Interval`

    If these implementations can be assumed tested in separate unit tests, they can probably be used to simplify the implementation of IntervalTest.

    E.g. use subtract to calculate a size, rather than passing it, and zero could be included in TypeOperators.

    enhancement 
    opened by Whathecode 0
  • Add normalize function

    Add normalize function

    end does not always need to lie after start. When start lies after end, an interval is called "reversed".

    When operating and comparing intervals, however, it is useful to normalize them. Part of this is to make sure start always lies before end.

    Can isStartInlcuded and isEndIncluded also be normalized for discrete types? E.g. for integers ]0, 1] can be normalized to [1, 1].

    Currently, I added an inner normalize function to intersects solely dealing with start/end reversal as part of ongoing IntervalUnion work: #22

    enhancement question 
    opened by Whathecode 1
  • Clean up buildSrc

    Clean up buildSrc

    With the introduction of kotlinx-interval-datetime as a submodule, buildSrc is used to apply a conventions plugin.

    But, this can be cleaned up a bit:

    • [ ] Central definition of versions. It is spread out between buildSrc/build.gradle.kts and build.gradle.kts at the moment, since the Nexus Gradle Plugin has to be applied to the main project.
    • [ ] Double loading of publish.properties in buildSrc/build.gradle.kts and the interval.library-conventions.gradle.kts plugin.
    • [ ] Add a test dependency on kotlinx.interval.test. But, this will require kotlinx.interval.test not to apply the convention plugin itself, perhaps only a subset defined in a new separate convention plugin.
    enhancement good first issue 
    opened by Whathecode 0
Releases(v1.0.0-alpha.4)
  • v1.0.0-alpha.4(Oct 9, 2022)

    • Added an initial contract definition for IntervalUnion (a collection of intervals) and preliminary concrete MutableIntervalUnion which lives up to the contract by restricting which intervals can be added.
    • Added Interval.reverse and nonReversed.
    • Added Interval.intersects to check whether one interval intersects with another.
    Source code(tar.gz)
    Source code(zip)
  • v1.0.0-alpha.3(Aug 2, 2022)

  • v1.0.0-alpha.2(May 24, 2022)

    • Added Interval.contains (in) operator to check whether values lie in an interval.
    • Added interval constructor functions so that all interval types can be constructed using the same keyword.
    Source code(tar.gz)
    Source code(zip)
  • v1.0.0-alpha.1(May 22, 2022)

    A first functional, but still unstable, release.

    The base type representing open/closed intervals with generic T and TSize has been introduced. The only operations at the moment is a size property. Default intervals are included for all basic Kotlin types, e.g. IntInterval, DoubleInterval, etc.

    Source code(tar.gz)
    Source code(zip)
Owner
Steven Jeuris
Steven Jeuris
Generic AST parsing library for kotlin multiplatform

kotlinx.ast kotlinx.ast is a generic AST (Abstract Syntax Tree) parsing library, Kotlin is currently the only supported language. The library is desig

null 235 Dec 29, 2022
KMM RSS Reader: an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile.

KMM RSS Reader This is an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile. It's a simple RSS reader, and you ca

Kotlin 1.4k Jan 4, 2023
Open as default - A flutter plugin that allows setting up your flutter app to open files as default

open_as_default A flutter plugin that allows setting up your flutter app to open

LuisDeLaValier 3 Nov 15, 2022
Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in Kotlin with Jetpack Compose and a backed in Kotlin hosted on AppEngine.

Conferences4Hall Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in K

Gérard Paligot 98 Dec 15, 2022
Opinionated Redux-like implementation backed by Kotlin Coroutines and Kotlin Multiplatform Mobile

CoRed CoRed is Redux-like implementation that maintains the benefits of Redux's core idea without the boilerplate. No more action types, action creato

Kittinun Vantasin 28 Dec 10, 2022
An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile.

An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile. 项目架构主要分为原生系统层、Android/iOS业务SDK层、KMM SDK层、KMM业务逻辑SDK层、iOS sdkfra

libill 4 Nov 20, 2022
A Bluetooth kotlin multiplatform "Cross-Platform" library for iOS and Android

Blue-Falcon A Bluetooth "Cross Platform" Kotlin Multiplatform library for iOS, Android, MacOS, Raspberry Pi and Javascript. Bluetooth in general has t

Andrew Reed 220 Dec 28, 2022
Mobile client for official Nextcloud News App written as Kotlin Multiplatform Project

Newsout Android and iOS mobile client for Nextcloud news App. The Android client is already available to download in the Play Store. F-Droid and Apple

Simon Schubert 118 Oct 3, 2022
Kotlin Multiplatform Application to show Crypto Coins

This is the codebase of Crypto currency Tracking Kotlin Multiplatform App. Components Shared Components Ktor (Network Client) SQL Delight (Local DB) A

Aman Bansal 10 Oct 31, 2022
Dependency Injection library for Kotlin Multiplatform, support iOS and Android

Multiplatform-DI library for Kotlin Multiplatform Lightweight dependency injection framework for Kotlin Multiplatform application Dependency injection

Anna Zharkova 32 Nov 10, 2022
Kotlin multiplatform library template.

template-kmp-library Kotlin multiplatform library template. Has a baseline setup for a multiplatform library supporting all kotlin targets except andr

Martynas Petuška 51 Nov 21, 2022
BuildConfig for Kotlin Multiplatform Project

BuildKonfig BuildConfig for Kotlin Multiplatform Project. It currently supports embedding values from gradle file. Table Of Contents Motivation Usage

Yasuhiro SHIMIZU 331 Jan 4, 2023
Kotlin multiplatform benchmarking toolkit

NOTE: Starting from version 0.3.0 of the library: The library runtime is published to Maven Central and no longer published to Bintray. The Gradle plu

Kotlin 310 Jan 2, 2023
Gradle plugin for simplify Kotlin Multiplatform mobile configurations

Mobile Multiplatform gradle plugin This is a Gradle plugin for simple setup of Kotlin Multiplatform mobile Gradle modules. Setup buildSrc/build.gradle

IceRock Development 78 Sep 27, 2022
KaMP Kit by Touchlab is a collection of code and tools designed to get your mobile team started quickly with Kotlin Multiplatform.

KaMP Kit Welcome to the KaMP Kit! About Goal The goal of the KaMP Kit is to facilitate your evaluation of Kotlin Multiplatform (aka KMP). It is a coll

Touchlab 1.7k Jan 3, 2023
Kotlin Multiplatform Mobile App Template

KMMT : Kotlin Multiplatform Mobile Template Kotlin Multiplatform Mobile Development Simplified KMMT is a KMM based project template designed to simpli

Jitty Andiyan 207 Jan 4, 2023
GraphQL based Jetpack Compose and SwiftUI Kotlin Multiplatform sample

GraphQL based Jetpack Compose and SwiftUI Kotlin Multiplatform sample

John O'Reilly 151 Jan 3, 2023
Playground for learning Kotlin Multiplatform Mobile

This is a playground for learning KMP (KMM Plugin for android studio). Requirements Android Studio Canary 8 Architecture Thanks https://twitter.com/jo

Mitch Tabian 111 Dec 27, 2022
Kotlin Multiplatform project that gets network data from Food2Fork.ca

Food2Fork Recipe App This is the codebase for a Kotlin Multiplatform Mobile course. [Watch the course](https://codingwithmitch.com/courses/kotlin-mult

Mitch Tabian 317 Dec 30, 2022