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.
- Host: GitHub
- URL: https://github.com/yahyatinani/y
- Owner: yahyatinani
- License: epl-1.0
- Created: 2020-08-28T08:45:11.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-06-29T07:39:55.000Z (over 2 years ago)
- Last Synced: 2024-05-22T11:16:03.844Z (almost 2 years ago)
- Topics: data-structures, immutable-collections, kmp, kotlin, kotlin-multiplatform, lists, maps, vectors
- Language: Kotlin
- Homepage:
- Size: 1.6 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README

========
[](https://github.com/yahyatinani/y/actions/workflows/main.yml)  

[](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
```