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

https://github.com/yahyatinani/y

'y' is a library that aims to provide useful data structures and utility functions.
https://github.com/yahyatinani/y

data-structures immutable-collections kmp kotlin kotlin-multiplatform lists maps vectors

Last synced: 3 months ago
JSON representation

'y' is a library that aims to provide useful data structures and utility functions.

Awesome Lists containing this project

README

          

![Y](docs/art/logo-with-text.png)
========

[![build](https://github.com/yahyatinani/y/actions/workflows/main.yml/badge.svg)](https://github.com/yahyatinani/y/actions/workflows/main.yml) ![Sonatype Nexus (Releases)](https://img.shields.io/nexus/r/io.github.yahyatinani.y/y-core?server=https%3A%2F%2Fs01.oss.sonatype.org%2F&label=latest%20release&color=blue) ![GitHub](https://img.shields.io/github/license/yahyatinani/y)
![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/io.github.yahyatinani.y/y-core?server=https%3A%2F%2Fs01.oss.sonatype.org%2F&label=latest%20snapshot)
[![codecov](https://codecov.io/gh/yahyatinani/y/branch/main/graph/badge.svg?token=O7NV8M4TTP)](https://codecov.io/gh/yahyatinani/y)

`y` is a Kotlin multiplatform library that aims to port and provide effective
concepts inspired by other languages or libraries.

## Modules:

### y-core:

This module provides core data structures and utility functions.

- ##### Data structures:

- List:

```kotlin
val list1 = l(1,2,3) // (1 2 3)
val empty = l() // ()

// From Kotlin's List to PersistentList
val list2 = listOf(1, 2, 3).toPlist() // (1 2 3)
```

To add an item to a list, use `conj(e)`, it puts the item at the front
of the list and returns a new list.

```kotlin
l(1,2,3).conj(4) // (4 1 2 3)
```

To get the size of a list:

```kotlin
l(1,2,3).count // 3
```

- Vector:

```kotlin
val vec1 = v(1, 2, 3) // [1 2 3]
val empty = v() // []

// From Kotlin's List to PersistentVector
val vec2 = listOf(1, 2, 3).toPvector() // [1 2 3]
```

To add an item to a vector, use `conj(e)`, it puts the item at the end
of the vector and returns a new vector.

```kotlin
v(1, 2, 3).conj(4) // [1 2 3 4]
```

To get the size of a vector:

```kotlin
v(1, 2, 3).count // 3
```

To get an item by index, you can use `get(index)` operator
or `nth(index)`/`nth(index, def)`

```kotlin
v(1, 2, 3)[1] // 2
v(1, 2, 3).nth(2) // 3
v(1, 2, 3).nth(5, -1) // -1
```

To get the items in reverse order as a sequence, use `reverse()`

```kotlin
v(1, 2, 3).reverse() // (3 2 1)
```

- PersistentArrayMap:

This map should be only used when you have very small maps and want to
maintain key order, since it is just an array of `[key,val..key,val]`.
Subsequent `assoc-ing` will eventually return a `PersistentHashMap`.

```kotlin
val map1 = m("1" to 1, "2" to 2) // {"1" 1, "2" 2}

//From Kotlin's Map to PersistentArrayMap
val map2 = mapOf("1" to 1, "2" to 2).toPArrayMap() // {"1" 1, "2" 2}
```

- PersistentHashMap:

This map requires keys that correctly support hashCode and equals.

```kotlin
val map1 = hashMap("1" to 1, "2" to 2) // {"1" 1, "2" 2}

//From Kotlin's Map to PersistentHashMap
val map2 = mapOf("1" to 1, "2" to 2).toPhashMap() // {"1" 1, "2" 2}
```

- PersistentHashSet:

```kotlin
val set1 = hs(1, 2, 2, 3, 3) // #{1 2 3}

//From Kotlin's Set to PersistentHashSet
val set2 = setOf(1, 2, 2, 3 ,3).toPhashSet() // #{1 2 3}
```

- Sequence:

All collections support a member function `seq()` that return a
sequence of type `ISeq` that can walk the entire collection. A
sequance provides three key member functions:

- `first()` : return the first element in the sequence.
- `rest()` : returns all of the rest elements of the sequence that
came after first element, as a sequence.
- `cons(element)` : always adds to the front of the sequence and
returns a sequence.

- ###### Keywords (WIP, only on JVM for now):

Keywords are identifiers that provide very fast equality tests, and they
have a string name. If you call toString() on a keyword it returns the
name prefixed by a ':' which is not part of the name.

```kotlin
// To create a Keyword, use the utility function `k(name)`:
val keyword = k("a") // :a

// invoke(map, default = null) operator
val map = hashMap("a" to 1)
val key = k("a")
val default = k("none")
key(map, default) // :none
```