How could I create a function with a completion handler in Swift?

Say you have a download function to download a file from network, and want to be notified when download task has finished. typealias CompletionHandler = (success:Bool) -> Void func downloadFileFromURL(url: NSURL,completionHandler: CompletionHandler) { // download code. let flag = true // true if download succeed,false otherwise completionHandler(success: flag) } // How to use it. downloadFileFromURL(NSURL(string: … Read more

What is the difference between a ‘closure’ and a ‘lambda’?

A lambda is just an anonymous function – a function defined with no name. In some languages, such as Scheme, they are equivalent to named functions. In fact, the function definition is re-written as binding a lambda to a variable internally. In other languages, like Python, there are some (rather needless) distinctions between them, but … Read more

How to create a custom EL function to invoke a static method?

First create a final class with a public static method which does exactly the job you want: package com.example; import java.util.Collection; public final class Functions { private Functions() { // Hide constructor. } public static boolean contains(Collection<Object> collection, Object item) { return collection.contains(item); } } Then define it as a facelet-taglib in /WEB-INF/functions.taglib.xml: <?xml version=”1.0″ … Read more

Table name as a PostgreSQL function parameter

This can be further simplified and improved: CREATE OR REPLACE FUNCTION some_f(_tbl regclass, OUT result integer) LANGUAGE plpgsql AS $func$ BEGIN EXECUTE format(‘SELECT (EXISTS (SELECT FROM %s WHERE id = 1))::int’, _tbl) INTO result; END $func$; Call with schema-qualified name (see below): SELECT some_f(‘myschema.mytable’); — would fail with quote_ident() Or: SELECT some_f(‘”my very uncommon table … Read more