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 in Swift which is similar to Lodash and 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
In your build.gradle file, add the following dependency
compile 'com.tilde:tilde:1.0.0'
Add the following dependency
<dependency>
<groupId>com.tilde</groupId>
<artifactId>tilde</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
Consider a map where we have mapped characters to list of their occurences. e.g. in kotlin we have
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
val fruit = "Banana"
val key = fruit.elementAt(0) // key is b here
var occurences = map.get(key)
if (occurences == null) {
occurences = ArrayList<String>()
map.put(key, occurences)
}
occurences.add(fruit)
But Tilde makes our life simple
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<String>()
map.put(key, list)
return@assign list
}
occurences.add(fruit)
Consider we have a list
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
val selected = list.at(0, 4, 3)
// Selected here will be arrayListOf("User first", "User fifth", "User fourth")
Consider we have list of students roll number
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
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
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 function that will resolve to one value after performing combine function on all elements
_t.reduce(listOf(1,2,3,4))
=> 10
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")
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
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.