Skip to content

Instantly share code, notes, and snippets.

@ViksaaSkool
Last active December 9, 2018 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ViksaaSkool/0c94b43348ce32767f34967f96141528 to your computer and use it in GitHub Desktop.
Save ViksaaSkool/0c94b43348ce32767f34967f96141528 to your computer and use it in GitHub Desktop.
UtilsAndExtensions
//A higher-order function is a function that takes functions as parameters, or returns a function. kotlin has the power
//to pass around functions
/* Lambdas */
//by definition, lambda expressions are functions that are not declared, but passed immediately as an expression
//surrounded by curly braces and the body goes after ->
//very basic definition
val sum = { x: Int, y: Int -> x + y }
//lambdas can be passed as parameter
val product = items.fold(1) { acc, e -> acc * e }
//if variables are unused, underscore is added (by the editor)
map.forEach { _, value -> println("$value!") }
//if lambdas have only one parameter, which is the common practice it's called "it", it that case there's no need to use the -> syntax
//and just use "it"
ints.filter { it > 0 } // this literal is of type '(it: Int) -> Boolean'
// it is an extension function
// it send this as it’s argument.
// it returns this (i.e. itself)
//another thing about the lamdas is that you can specify the return, i.e. use qualified return syntax - @return type
ints.filter {
val shouldFilter = it > 0
return@filter shouldFilter
}
//anonymous function - very much like a regular function declaration, except that its name is omitted
fun(x: Int, y: Int): Int = x + y
/* Function types */
//Kotlin uses a family of function types like (Int) -> String for declarations that deal with functions:
//1. All function types have a parenthesized parameter types list and a return type: (A, B) -> C denotes a type
//that represents functions taking two arguments of types A and B and returning a value of type C
//2. Function types can optionally have an additional receiver type,
//which is specified before a dot in the notation: the type A.(B) -> C represents
//functions that can be called on a receiver object of A with a parameter of B and return a value of C
//3.Suspending functions belong to function types of a special kind, which have a suspend modifier in the notation,
//such as suspend () -> Unit or suspend A.(B) -> C
/* Inline functions */
//inline function will be substituted by its code during compilation, instead of doing the real call to a function.
//It will reduce memory allocations and runtime overhead in some situations - inline functions will substitute
//the code of the function in the place where it is called, so it won’t require an internal object for that.
inline fun <T> lock(lock: Lock, body: () -> T): T { ... }
//example
inline fun supportsLollipop(code: () -> Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
code()
}
}
//use the function as
supportsLollipop {
window.setStatusBarColor(Color.BLACK)
}
/*Example for extensions*/
//instead of using util methods, predefined methods can be redefined/extended to suit your needs
//transit to another Activity
fun Activity.callTo(clazz: Class<out Activity>) {
startActivity(Intent(this, clazz))
}
//add and replace fragment using inline functions
inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> Unit) {
val fragmentTransaction = beginTransaction()
fragmentTransaction.func()
fragmentTransaction.commit()
}
fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int, backStackTag: String? = null) {
supportFragmentManager.inTransaction {
add(frameId, fragment)
backStackTag?.let { addToBackStack(fragment.javaClass.name) }
}
}
fun AppCompatActivity.replaceFragment(fragment: Fragment, frameId: Int, backStackTag: String? = null) {
supportFragmentManager.inTransaction {
replace(frameId, fragment)
backStackTag?.let { addToBackStack(fragment.javaClass.name) }
}
}
/* Utils classes */
//util methods and classes can still be used and are relevant concept in a lot of cases
//anotate the static fun and definie it in companion object inside of the class
companion object{
@JvmStatic
fun debugToastMessage(context: Context, message:String, duration:Int){
if(BuildConfig.DEBUG){
Toast.make(context, message, duration).show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment