Equivalent of Java Static Final Fields in Kotlin

Soumak De
Updated on 23-Nov-2021 07:01:54

873 Views

"static" is a Java keyword that helps developers to define a class member, whereas the keyword "final" is used to declare a constant variable in Java. Once a variable is declared as static in Java, the value of the variable remains unchanged in every instance of the object. Similarly, once a variable is declared as final, its value cannot be altered.In this article, we will see how we can implement the same concept using Kotlin library function.Example - using companion objectA "companion object" is an object which is declared in the same file as a class. Both the class and ... Read More

Clone or Copy a List in Kotlin

Soumak De
Updated on 23-Nov-2021 06:58:48

2K+ Views

A list is a type of collection in which data is stored in a sequential manner. We might come across a situation where the contents of one List have to be copied into another. In this article, we will see how to use Kotlin's built-in methods to clone a list.Example - Using toList()toList() is the basic method which can convert a collection into a list. We can use this method to clone an existing list too.fun main(args: Array) {    val x = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)    val copiedList=x.toList();    println("Given collection: " +x) ... Read More

Difference Between Fold and Reduce in Kotlin

Soumak De
Updated on 23-Nov-2021 06:54:27

1K+ Views

Kotlin is a cross-platform and statically typed general-purpose programming language. Kotlin provides many optional methods to traverse through the collections. fold() and reduce() are two different methods, both are helpful for traversing a collection. In this article, we will see how and when to use these two methods.Example – fold()If we want to traverse through a collection serially, then we can use fold().fold() takes one initial value and one operation to perform the operation on the initial value.There are different kinds of fold(), for example, foldRight() folds from right to left. By default, fold() will traverse from left to right.The ... Read More

Find Moving Standard Deviation in an R Matrix

Nizamuddin Siddiqui
Updated on 23-Nov-2021 06:52:55

414 Views

To find the moving standard deviation in a matrix is done in the same way as in a data frame, we just need to use the matrix object name in place of data frame name. Hence, we can make use of rollapply function of zoo package for this purpose.For example, if we have a matrix called M and we want to find the 2 moving standard deviations then we can use the below given command −rollapply(M,width=2,FUN=sd,fill=0,align="r")Example 1Following snippet creates a matrix −M1

Try-With-Resources in Kotlin

Soumak De
Updated on 23-Nov-2021 06:51:34

709 Views

Kotlin is very efficient in managing the memory. Unlike Java, developers in Kotlin need not have to manage the memory explicitly. We do have different kinds of memory management techniques and Try-with-resource is one of them. In Kotlin, we have a function called 'use' which takes the burden of managing the resources automatically. This is a part of std library function provided by Kotlin.As per Kotlin documentation, use() is defined as a generic extension on all closeable types. The implementation looks like this −public inline fun T.use(block: (T) -> R): R {}In the above function, the definition block is the ... Read More

Round Summary Output in R

Nizamuddin Siddiqui
Updated on 23-Nov-2021 06:49:16

4K+ Views

To round the output of summary function in R, we can use digits argument while applying the summary function.For example, if we have a data frame called df then to find the summary statistics with two digits in the output we can use the below given command −summary(df, digits=2)Example 1Following snippet creates a dataframe −head(iris, 20) The following dataframe is created − Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1    5.1       3.5         1.4         0.2         setosa 2    4.9       3.0         1.4 ... Read More

Break and Continue in Foreach Loop in Kotlin

Soumak De
Updated on 23-Nov-2021 06:48:01

5K+ Views

In Kotlin, we have three types of structural jump expressions: "break", "return", and "continue". In this article, we will see how break and continue work in Kotliln.Break - This is a keyword that helps in terminating an iteration, once a given condition is met, while traversing through a collection.Continue - This keyword helps to continue the iteration once a condition is fulfilled.In Kotlin, we cannot explicitly use break and continue statements explicitly inside a forEach loop, but we can simulate the same action. In this example, we will see how to do it.Example: Return at labels :: directly to the ... Read More

Find Row-wise Sum for N Columns in R

Nizamuddin Siddiqui
Updated on 23-Nov-2021 06:36:30

3K+ Views

To find the row wise sum of n number of columns can be found by using the rowSums function along with subsetting of the columns with single square brackets.For example, if we have a data frame called df that contains five columns and we want to find the row sums for last three columns then we can use the following command −df$Sum_3

Extract Maximum Value from Named Vector in R

Nizamuddin Siddiqui
Updated on 23-Nov-2021 06:31:08

1K+ Views

To extract the maximum value from named vector in R, we can use which.max function.For example, if we have a vector called X which is a named vector then we can use the following command to find the maximum value in X.X[which.max(X)]Check out the below examples to understand how it works.Example 1Following snippet creates a vector −x1

Swift If-Let Statement Equivalent in Kotlin

Soumak De
Updated on 23-Nov-2021 06:28:11

2K+ Views

Swift "if let" is a conditional check operator to check whether a reference variable is NULL or not. This is a very useful technique to evaluate the unwrap optional value using swift language.In Kotlin, we can't use this operator directly. Instead, we will be using "let" and "run" to evaluate the same. In the following example, we will see how to use "let" and "run" using Kotlin library class.Example: Null Check using 'let' and 'run'In this example, we will check whether the value of a reference variable is NULL or not. If the value of the reference variable is NULL, ... Read More

Advertisements