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

Last update: May 6, 2022

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.


πŸ“• Documentation

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

filter|194x74

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

filter_indexed|244x74

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

filter_not|216x74

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

filter_not_null|240x49

Returns a list containing all elements that are not null.

map|189x74

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|239x74

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|210x78

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

for_each_indexed|260x78

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

verify_all|212x74

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

verify_any|221x74

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

verify_none|231x74

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

find|190x74

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

find_last|218x74

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

drop|194x74

Returns a list containing all elements except first n elements.

drop_while|231x74

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

drop_last|222x74

Returns a list containing all elements except last n elements.

drop_last_while|259x74

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

take|192x74

Returns a list containing first n elements.

take_while|229x74

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

take_last|220x74

Returns a list containing last n elements.

take_last_while|257x74

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


πŸ”‘ 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|603x150

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|344x89


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|344x89

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|307x113


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.

πŸ’» UI Design

search_list_design|292x296 search_list_design_preview|242x500

kblocks|24x24 Blocks

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

list_of_titles|566x260

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

update_list_view|454x90

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

add_index_to_title|307x113

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

filter_title_predicate|603x150

  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.

initialize|690x176

  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|634x256

πŸ“‚ Project Files


notepad|24x24 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|307x113

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|603x150


Download Extension

1.0.0

GitHub

https://github.com/zainulhassan815/appinventor-list-utils
You might also like...

An awesome list that curates the best KMM libraries, tools and more.

Awesome KMM Kotlin Multiplatform Mobile (KMM) is an SDK designed to simplify creating cross-platform mobile applications. With the help of KMM, you ca

Jun 20, 2022

Name of your app is an android app that allows building a todo list

Name of your app is an android app that allows building a todo list

Project 1 - SimpleToDo Name of your app is an android app that allows building a todo list and basic todo items management functionality including add

Nov 23, 2021

Location-reminders - A Todo list app with location reminders that remind the user to do something when he reaches a specific location

Location Reminder A Todo list app with location reminders that remind the user t

Feb 1, 2022

Joker-App - List application tha make requests to a Chuck Norris Api

Joker-App - List application tha make requests to a Chuck Norris Api

Joker App About β€’ Technologies β€’ Features β€’ Author β€’ License πŸ“š About the Projec

Feb 4, 2022

UserLoc - A API call using Retrofit to obtain list of users details and show on UI in recycler view and google map

UserLoc - A API call using Retrofit to obtain list of users details and show on UI in recycler view and google map

UserLoc This uses a API call using Retrofit to obtain list of users details and

Feb 11, 2022

Run Kotlin/JS libraries in Kotlin/JVM and Kotlin/Native programs

Zipline This library streamlines using Kotlin/JS libraries from Kotlin/JVM and Kotlin/Native programs. It makes it possible to do continuous deploymen

Jun 17, 2022

A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

Jan 20, 2022

Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in Kotlin with Jetpack Compose and a backed in Kotlin hosted on AppEngine.

Conferences4Hall Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in K

Jun 15, 2022

[Android Library] A SharedPreferences helper library to save and fetch the values easily.

Preference Helper A SharedPreferences helper library to save and fetch the values easily. Featured in Use in your project Add this to your module's bu

Apr 4, 2020
A kotlin library of extension functions that add smalltalk style methods to objects.

KtTalk A kotlin library of extension functions that add smalltalk style methods to objects. Motivation Smalltalk is a pure OO language in which everyt

Oct 16, 2021
A library provides some useful kotlin extension functions

ktext ?? A library provides some useful kotlin extension functions. Including in your project Gradle Add below codes to your root build.gradle file (n

May 17, 2022
Extension functions over Android's callback-based APIs which allows writing them in a sequential way within coroutines or observe multiple callbacks through kotlin flow.

callback-ktx A lightweight Android library that wraps Android's callback-based APIs into suspending extension functions which allow writing them in a

Apr 16, 2022
A collection of small utility functions to make it easier to deal with some otherwise nullable APIs on Android.

requireKTX requireKTX is a collection of small utility functions to make it easier to deal with some otherwise nullable APIs on Android, using the sam

Apr 30, 2022
Use Flink's Stateful Functions as a control-plane technology for operating a streaming-platform
Use Flink's Stateful Functions as a control-plane technology for operating a streaming-platform

statefun-ops Use ?? Flink Stateful Functions as a control-plane technology for operating a streaming-platform based on Apache Kafka. Walkthrough Ensur

Jan 3, 2022
Jun 10, 2022
This sample Kotlin app shows a list of custom shoes added by the users

Shoe-store This sample Kotlin app shows a list of custom shoes added by the users. The app displays the content with RecyclerView and uses a tradition

Apr 13, 2022
A collaborative list of awesome jetpack compose resources.

Awesome Jetpack compose A collaborative list of awesome jetpack compose resources. Featured in Content Official Documentation Conference talks Article

Jun 17, 2022
πŸ’‘πŸš€β­οΈ A generalized adapter for RecyclerView on Android which makes it easy to add heterogeneous items to a list
πŸ’‘πŸš€β­οΈ A generalized adapter for RecyclerView on Android which makes it easy to add heterogeneous items to a list

Mystique is a Kotlin library for Android’s RecyclerView which allows you to create homogeneous and heterogeneous lists effortlessly using an universal

May 6, 2022
:speedboat: Floating navigation view for displaying a list of items dynamically on Android.
:speedboat: Floating navigation view for displaying a list of items dynamically on Android.

Submarine Fully customizable floating navigation view for listing items dynamically on Android. Including in your project Gradle Add below codes to yo

Apr 23, 2022