https://github.com/shahroz16/tilde
A functional tool-belt for Kotlin Language similar to Lo-Dash or Underscore.js in Javascript and Dollar in Swift
https://github.com/shahroz16/tilde
belt dollar functional-programming java-8 kotlin lodash underscore
Last synced: 7 months ago
JSON representation
A functional tool-belt for Kotlin Language similar to Lo-Dash or Underscore.js in Javascript and Dollar in Swift
- Host: GitHub
- URL: https://github.com/shahroz16/tilde
- Owner: Shahroz16
- License: mit
- Created: 2017-05-18T11:56:35.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-10T10:22:05.000Z (about 8 years ago)
- Last Synced: 2025-01-23T05:41:28.810Z (9 months ago)
- Topics: belt, dollar, functional-programming, java-8, kotlin, lodash, underscore
- Language: Kotlin
- Size: 156 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tilde
----
Tilde is a functional tool-belt for Android. It not just only provides functional methods for Kotlin but also some wrapper for Java so you can play around with the library in Java too.
Tilde is inspired from [Dollar][dollar] in Swift which is similar to [Lodash][lodash] and [Underscore.js][underscore-js] in Javascript.
It also provides Java 6 and 7 users with almost all the useful methods in Java 8.> **NOTE: This library is under development and documentation is incomplete**
# Quick Jump
---
- [Setup](#setup)
- [Usage](#usage)
- [Contributing](#contributing)
- [License](#license)# Setup
---
## Gradle Setup
In your **build.gradle** file, add the following dependency
```groovy
compile 'com.tilde:tilde:1.0.0'
```
## Maven Setup
Add the following dependency
```xmlcom.tilde
tilde
1.0.0
pom```
# Usage
----
# Assign - `T?.assign`
Consider a map where we have mapped characters to list of their occurences. e.g. in kotlin we have
```kotlin
val map = `mapOf('a' to arrayListOf("Apple", "Avocado"), 'm' to arrayListOf("Mango"), 't' to arrayListOf("Tomato"))`
```
And we have a new value `Banana`. Our code would be like
```kotlin
val fruit = "Banana"
val key = fruit.elementAt(0) // key is b here
var occurences = map.get(key)
if (occurences == null) {
occurences = ArrayList()
map.put(key, occurences)
}
occurences.add(fruit)
```
But **Tilde** makes our life simple
```kotlin
val fruit = "Banana"
val key = fruit.elementAt(0) // key is b here
val occurences = map.get(key).assign {
// The block will only execute if map.get(key) returns null
val list = ArrayList()
map.put(key, list)
return@assign list
}
occurences.add(fruit)
```
# At - `Iterable.at`
Consider we have a list
```kotlin
val list = arrayListOf("User first", "User second", "User third", "User fourth", "User fifth")
```
And we want elements at 0th, 4th and 3rd index. We can get it simply by
```kotlin
val selected = list.at(0, 4, 3)
// Selected here will be arrayListOf("User first", "User fifth", "User fourth")
```
# Chunks - `Iterable.chunks`
Consider we have list of students roll number
```kotlin
val list = arrayListOf("1001", "1002", "1003", "1004", "1005", "1006", "1007", "1008", "1009", "1010", "1011", "1012", "1013", "1014")
```
And we want to create groups in sequence having maximum of 3 students. We can acheive this by the following code
```kotlin
val groups = list.chunks(3)
```
And here `groups` will have a 2D array as follows
```
1001 1002 1003
1004 1005 1006
1007 1008 1009
1010 1011 1012
1013 1014
```# Range - `_t.range`
Creates a list of numbers (positive and/or negative) progressing from start up to end
```
_t.range(end = 3)
=> listOf(0, 1, 2, 3)_t.range(start = 1, end = 5, endInclusive = false)
=> listOf(1, 2, 3, 4)_t.range(start = 1, end = 5, endInclusive = true)
=> listOf(1, 2, 3, 4, 5)_t.range(start = 0, end = 20, step = 5)
=> listOf(0, 5, 10, 15, 20)
```# Reduce - `_t.reduce`
Reduce function that will resolve to one value after performing combine function on all elements
```
_t.reduce(listOf(1,2,3,4))
=> 10
```# Remove - `Iterable.remove`
Removes items from an list.
```
val list = listOf("a", "b", "c", "d", "e", "f")
_t.remove(list, "a", "b", "f")
=> result - listOf("c", "d", "e")
```# Contributing
----
Feel free to add new methods and submit a pull request.
- Code
- Add comments to your code (if necessary)
- Add documentation to your methods
- Not to forget adding test case for your methods
- Add function usage in README.md
- Generate pull request
> No **Pull Request** will be entertained without test cases# License
----MIT License
Copyright (c) 2017 Shahroz Khan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[dollar]:
[lodash]:
[underscore-js]: