• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Kotlin/kotlinx.collections.immutable: Immutable persistent collections for Kotli ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

Kotlin/kotlinx.collections.immutable

开源软件地址(OpenSource Url):

https://github.com/Kotlin/kotlinx.collections.immutable

开源编程语言(OpenSource Language):

Kotlin 100.0%

开源软件介绍(OpenSource Introduction):

Immutable Collections Library for Kotlin

Kotlin Alpha JetBrains official project GitHub license Build status Maven Central IR

Immutable collection interfaces and implementation prototypes for Kotlin.

This is a multiplatform library providing implementations for jvm, js (both Legacy and IR), mingwX64, linuxX64 and Apple Kotlin/Native targets.

For further details see the proposal.

What's in this library

Interfaces and implementations

This library provides interfaces for immutable and persistent collections.

Immutable collection interfaces

Interface Bases
ImmutableCollection Collection
ImmutableList ImmutableCollection, List
ImmutableSet ImmutableCollection, Set
ImmutableMap Map

Persistent collection interfaces

Interface Bases
PersistentCollection ImmutableCollection
PersistentList PersistentCollection, ImmutableList
PersistentSet PersistentCollection, ImmutableSet
PersistentMap ImmutableMap

Persistent collection builder interfaces

Interface Bases
PersistentCollection.Builder MutableCollection
PersistentList.Builder PersistentCollection.Builder, MutableList
PersistentSet.Builder PersistentCollection.Builder, MutableSet
PersistentMap.Builder MutableMap

To instantiate an empty persistent collection or a collection with the specified elements use the functions persistentListOf, persistentSetOf, and persistentMapOf.

The default implementations of PersistentSet and PersistentMap, which are returned by persistentSetOf and persistentMapOf, preserve the element insertion order during iteration. This comes at expense of maintaining more complex data structures. If the order of elements doesn't matter, the more efficient implementations returned by the functions persistentHashSetOf and persistentHashMapOf can be used.

Operations

toImmutableList/Set/Map

Converts a read-only or mutable collection to an immutable one. If the receiver is already immutable and has the required type, returns it as is.

fun Iterable<T>.toImmutableList(): ImmutableList<T>
fun Iterable<T>.toImmutableSet(): ImmutableSet<T>

toPersistentList/Set/Map

Converts a read-only or mutable collection to a persistent one. If the receiver is already persistent and has the required type, returns it as is. If the receiver is a builder of the required persistent collection type, calls build on it and returns the result.

fun Iterable<T>.toPersistentList(): PersistentList<T>
fun Iterable<T>.toPersistentSet(): PersistentSet<T>

+ and - operators

plus and minus operators on persistent collections exploit their immutability and delegate the implementation to the collections themselves. The operation is performed with persistence in mind: the returned immutable collection may share storage with the original collection.

val newList = persistentListOf("a", "b") + "c"
// newList is also a PersistentList

Note: you need to import these operators from kotlinx.collections.immutable package in order for them to take the precedence over the ones from the standard library.

import kotlinx.collections.immutable.*

Mutate

mutate extension function simplifies quite common pattern of persistent collection modification: get a builder, apply some mutating operations on it, transform it back to a persistent collection:

collection.builder().apply { some_actions_on(this) }.build()

With mutate it transforms to:

collection.mutate { some_actions_on(it) }

Using in your projects

Note that the library is experimental and the API is subject to change.

The library is published to Maven Central repository.

The library depends on the Kotlin Standard Library of the version at least 1.6.0.

Gradle

Add the Maven Central repository:

repositories {
    mavenCentral()
}

Add the library to dependencies of the platform source set, e.g.:

kotlin {
    sourceSets {
        commonMain {
             dependencies {
                 implementation("org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.5")
             }
        }
    }
}

Maven

The Maven Central repository is available for dependency lookup by default. Add dependencies (you can also add other modules that you need):

<dependency>
    <groupId>org.jetbrains.kotlinx</groupId>
    <artifactId>kotlinx-collections-immutable-jvm</artifactId>
    <version>0.3.5</version>
</dependency>

Building from source

You can build and install artifacts to maven local with:

gradlew build install



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap