This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sum4(4) //return 8 | |
div2(4) //return 2 | |
listOf(3,4,6,8,10) | |
.map(sum4) | |
.map(div2) | |
//[3, 4, 5, 6, 7] | |
fun hello(name: String, textDecorator: () -> String): String { | |
return "hello $name ${textDecorator()}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T : Comparable<T>> List<T>.sort() { | |
Collections.sort(this) | |
} | |
list.sort(); | |
val index = list.binarySearch(x); | |
val |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bundle?.let { intent.putExtras(it) } //si el bundle no es null, lo añadimos como extra al intent | |
//crear un animationSet al estilo builder | |
val animationSet = AnimatorSet().apply { | |
playTogether(animX, animY) | |
duration = 350 | |
interpolator = FolioBounceInterpolator() | |
}.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
operator fun String.get(intRange: IntRange): String { | |
return this.substring(intRange) | |
} | |
val firstSentence = "hello Marc!" | |
val secondSentence = "World Wide" | |
val mixedSentence = firstSentence[0..4] + secondSentence[0..4] | |
//Hello World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun twice(int: Int) = int * 2 | |
fun trice(int: Int) = int * 3 | |
fun getString(int: Int) = int.toString() | |
//el operador rangeTo es el '..' acepta un parametro de entrada y otro de salida | |
//si usamos mónadas para enlazarlo podemos concatenar la salida de uno con la entrada del otro | |
operator fun <T,R,V> ((T)->R).rangeTo(other: ((R)->V)): ((T)->V){ | |
return { | |
other(this(it)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified NewActivity : Activity> Activity.start(bundle: Bundle? = null) { | |
val intent: Intent = Intent(this, NewActivity::class.java) | |
bundle?.let { intent.putExtras(it) } | |
this.startActivity(intent) | |
} | |
startActivity<MainActivity>() | |
infix fun <Value>Boolean.then(block: () -> Value) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.databinding.Observable as DataBindingObservable | |
private inline fun <reified T : DataBindingObservable, R : Any?> T.observe( | |
crossinline block: (T) -> R | |
): Observable<R> = create { subscriber -> | |
object : android.databinding.Observable.OnPropertyChangedCallback() { | |
override fun onPropertyChanged(observable: DataBindingObservable, id: Int) = try { | |
subscriber.onNext(block(observable as T)) | |
} catch (e: Exception) { | |
subscriber.onError(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ByDouble(incrementer: Incrementer) : Incrementer by incrementer { | |
override fun increment(number: Int): Int { | |
return super.increment(number) * 2 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Database(private context: Context) { | |
val helper: SQLiteOpenHelper by lazy { SQLiteOpenHelper(context,...-) } | |
val observableProperty: Int by Delegates.observable { thisRef , newValue , oldValue -> doSomething()} | |
val vetoableProperty: Int by Delegates.vetoable { _ , newValue , oldValue -> return if (oldValue < newValue) true else false } | |
} |
OlderNewer