Skip to content

Instantly share code, notes, and snippets.

@jacobmoncur
Last active January 15, 2016 04:55
Show Gist options
  • Save jacobmoncur/c3b8acd69391156f5fc6 to your computer and use it in GitHub Desktop.
Save jacobmoncur/c3b8acd69391156f5fc6 to your computer and use it in GitHub Desktop.

Lambdas

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        doSomethingAmazing();
    }
});
button.setOnClickListener {
    doSomethingAmazing()
}
fun View.onClick(listener: (View) -> Unit){
    this.setOnClickListener(listener)
}

button.onClick {

}
// Java
List<String> list = Arrays.asList("1", "2", "3", "4");

Integer maxValue = null;
for(String item : list){
    Integer value = Integer.parseInt(item);
    Integer doubledValue = value * 2;
    if(doubledValue < 6) {
        if(maxValue == null){
            maxValue = doubledValue;
        } else {
            maxValue = (doubledValue > maxValue) ? doubledValue : maxValue;
        }
    }
}
val list = listOf("1", "2", "3", "4")
val max = list.map { it.toInt() * 2 } .filter { it > 6 } .max()
fun uploadValue(value: Any, callback: (response: Response?, error: Error?) -> Unit) {
    // Do something amazing and async...
    asyncOperation {
        callback(response, error)
    }
}

uploadValue("HARRY POTTER") { success, error ->

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment