Articles on Trending Technologies

Technical articles with clear explanations and examples

What is the equivalent of Java static final fields in Kotlin?

Soumak De
Soumak De
Updated on 23-Nov-2021 903 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

How to clone or copy a list in Kotlin?

Soumak De
Soumak De
Updated on 23-Nov-2021 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
Soumak De
Updated on 23-Nov-2021 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

How to find the moving standard deviation in an R matrix?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 23-Nov-2021 427 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

Read More

Try-with-resources in Kotlin

Soumak De
Soumak De
Updated on 23-Nov-2021 740 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

How to round the summary output in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 23-Nov-2021 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

How to find the row wise sum for n number of columns in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 23-Nov-2021 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

Read More

How to extract the maximum value from named vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 23-Nov-2021 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

Read More

Swift "if let" statement equivalent in Kotlin

Soumak De
Soumak De
Updated on 23-Nov-2021 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

How to reduce the space between Y-axis value and ticks using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 23-Nov-2021 5K+ Views

To reduce the space between axis value and ticks using ggplot2, we can use theme function of ggplot2 package with margin set to 0.For example, if we have a data frame called df that contains two columns say x and y then the scatterplot between x and y with reduced space between Y-axis value and ticks can be created by using the following command −ggplot(df,aes(x,y))+geom_point()+theme(axis.text.y=element_text(margin=margin(r=0)))ExampleFollowing snippet creates a sample data frame −x

Read More
Showing 25881–25890 of 61,248 articles
Advertisements