[F/OS] ListUtils - A set of utility functions to work with list. Advance list functions from Kotlin Standard Library

ListUtil.kt ListUtils - Advance list functions from kotlin standard library

A set of utility functions to work with lists in a robust way. It is based on a pattern that I discussed earlier called Callback Pattern. You can further read that topic to get basic overview of how things work. You can also take a look at the official docs to get more details about ListFunctions.


:closed_book: Documentation

Lets take a look at what all the blocks do and then we shall see how to use them in your projects.

filter

Returns a list containing only elements matching the given predicate. Provides item to the predicate.

filter_indexed

Returns a list containing only elements matching the given predicate. Provides index & item to the predicate.

filter_not

Returns a list containing all elements not matching the given predicate. Provides item to the predicate.

filter_not_null

Returns a list containing all elements that are not null.

map

Returns a list containing the results of applying the given transform function to each element in the original collection. Provides item to the action.

map_indexed

Returns a list containing the results of applying the given transform function to each element in the original collection. Provides index & item to the action.

for_each

Performs the given action on each element. Provides item to the action.

for_each_indexed

Performs the given action on each element. Provides index & item to the action.

verify_all

Returns true if all elements match the given predicate. Provides item to the predicate.

verify_any

Returns true if at least one element matches the given predicate. Provides item to the predicate.

verify_none

Returns true if no elements match the given predicate… Provides item to the predicate.

find

Returns the first element matching the given predicate, or null if no such element was found. Provides item to the predicate.

find_last

Returns the last element matching the given predicate, or null if no such element was found. Provides item to the predicate.

drop

Returns a list containing all elements except first n elements.

drop_while

Returns a list containing all elements except first elements that satisfy the given predicate. Provides item to the predicate.

drop_last

Returns a list containing all elements except last n elements.

drop_last_while

Returns a list containing all elements except last elements that satisfy the given predicate. Provides item to the predicate.

take

Returns a list containing first n elements.

take_while

Returns a list containing first elements satisfying the given predicate. Provides item to the predicate.

take_last

Returns a list containing last n elements.

take_last_while

Returns a list containing last elements satisfying the given predicate. Provides item to the predicate.


:key: Key Concepts

There is one thing common in all of these functions. That is they accept a list. Then some functions take a prdicate while some take an action to perform. Lets take a brief look into Predicate and Action.


Predicate

A predicate is a function that takes some input and returns either true or false.

For example below is a predicate function that takes in a title and check whether the the search query that user entered exists in the title.

filter_title_predicate

Action

An action is a function takes some input but doesn’t return any value.

For example below is an action that takes an error and logs it with the notifier. It doesn’t return any value.

log_error_action


Procedure Types

Almost all extension functions take predicate or action. The input procedure are further of two types. One takes only item while the other takes index and item.

1- Procedure With Item

The procedure below takes only the item.

log_error_action

2- Procedure With Index & Item

The procedure below takes index as well as the item. Always be careful about the sequence in such procedures. The first parameter is index and second parameter is the actual item.

add_index_to_title


Usage

Lets see a basic example in which we fill use MapIndexed and Filter. We will add index to all the titles and we will filter them using a search box.

:computer: UI Design

search_list_design

kblocks Blocks

  1. Lets start by creating a list of titles. Create one more list that will contain filteredTitles.

list_of_titles

  1. Create a function that updates filteredList data and also updates ListView elements.

update_list_view

  1. Create a function that takes index and title as input and and returns a new title by appending index to the title.

add_index_to_title

  1. Create a function that takes in title as input and checks whether it contains SearchBox.Text (user entered query).

filter_title_predicate

  1. When screen initializes, we will create a local variable titlesWithIndexes that contains mapped titles. We will use a function called MapIndexed that provies index & item to the transformer action procedure. Pass the list of titles and the procedure name addIndexToTitle that we create in step 3. Now pass the variable titlesWithIndexes to the updateListView procedure that we created in step 2.

  1. In order to add search functionality, we will use SearchBox.OnTextChanged event to filter data using Filter function whenever the search query changes. Create a local variable called searchResult and set its initial value to an empty list. Next set the value of searchResult to the Filter function from ListUtils. Pass the list of titles and filterTitlePredicate procedure name to the Filter function. Now repeat step 5 but change the list from titles to searchResult and we have a working search box.

on_search_text_changed

:open_file_folder: Project Files


notepad Note

When using a function that has word Indexed at the end, it expects a procedure that takes index and item. For example, when we used MapIndexed function to add index to the title, the procedure accpted
index as well as title. So be careful when using Indexed functions.

add_index_to_title

For functions that don’t have word Indexed at the end, you can use procedure that accept only item as input. For example, filterTitlePredicate that we used earlier takes only title as input.

filter_title_predicate


Download Extension

1.0.0


:github: Open Source


Changelog

1.0.0

  • Adds general list methods from Kotlin Standard Library
20 Likes

Good, but work Very Slowly, if db big, please add Async search/ other extension have async

Its not the responsibility of ListUtils to do stuff asynchronously. You can use Async Utils for it.

ListUtils not working with Recycler List View ver. 1.0.3

yes, ive tried everything and its not working. have you find any solution?

anyone who can clarify this listutils extension for recyclerlist 1.03? ive tried to update this example to 1.03 and it just doesnt work anymore.

Great Extanyion

1 Like