Nektarios
Nektarios

Reputation: 10351

Ways to use design using Closures (or Blocks), as a C programmer

My 2 main development environments are C, and Objective-C. With Clang and LLVM, using closures has become or is becoming a complete reality, called Blocks.. but they're still closures. I'm not concerned with the technical "how to"

What I want to know is, what are the best ways to design software to use Closures?

I know that you can do nice sorting things using them, but what other possibilities exist? I've seen ways to simplify program structure, but the topic is very heard to search on. I've never seen any exhaustive list or a good resource.

Bounty placed: a bounty answer will give a thorough list and specific examples for uses of blocks in C and Objective-C

Upvotes: 5

Views: 2046

Answers (3)

umlcat
umlcat

Reputation: 4143

I haven't work with Objective-C, but have use closures in C, C++, Object Pascal, Delphi, Javascript. "Closure" is a very ambigous term, and it's implementation in code, differs from one programming language from another, as well as a math term.

That's why we have "blocks", "callbacks", "function pointers", "method references", blah.

Basically, a "closure" is a function that is used as a data. Programmers store the function reference in a variable or parameter, and instead of been call inmediatly, is used when required. Since we can have "function variables", the same variable can be used to execute different closures.

Since you mention "Objective C", who is also Object Oriented, the way to use "closure" is different if you want to use closures for methods or closures for global functions.

Example with "Plain C":


typedef int (*ClosureType)(int a, int b);

int add(int a, int b)
{
    return a + b;
}

int substract(int a, int b)
{
    return a - b;
}

int multiply(int a, int b)
{
    return a * b;
}

int main(int argc, char *argv[])
{
    ClosureType functions[3] = { add, substract, multiply };

    int option = 0;

    printf("Operation Menu:\n");
    printf("(1) Addition:\n");
    printf("(2) Substraction:\n");
    printf("(3) Multiplication:\n");
    printf("Select an operation\n"); 
        scanf("%d", &option);

    int r = -1;
    switch () {
        case 1:
            r = functions[0] (3, 4);
        break;

        case 2:
            r = functions[1] (3, 4);
        break;

        case 3:
            r = functions[2] (3, 4);
        break;

        default
        break;
    }

    printf("The result of %d and %d: %d\n", 3, 4, r); 
    getch()("\n"); 

    return 0;
}

Upvotes: 0

Laurent Parenteau
Laurent Parenteau

Reputation: 2576

For Blocks in Objective-C or C:

More on Closure/Blocks/Lambdas :

Upvotes: 3

CRD
CRD

Reputation: 53010

Probably not the answer you're looking for, but it may help...

Closures are not new, they've been around since the dawn of programming (Lisp, Algol-60). The best way to learn about how they are used is to look at a mature language where they've been around forever and see how people use them.

Functional languages and closures are synonymous, usually referred to as "higher order" programming - where functions (methods) take other functions as arguments and functions can be created on the fly (blocks). So looking at a book on programming in Haskell is not a bad choice, or maybe F#.

If that seems too radical you could look at Ada 95, a "conventional" language with inline functions, though it is a (profitable) niche language and the features are probably not widely used.

If you want something relatively new look at C# where inline functions were added in v2. C#'s inline functions have many of the same pitfalls as Obj-C ones, and people fell into them with uninformed glee - so look for a recent C# book when their use was more mature.

And in closing, don't overuse blocks. They are a very powerful tool, and when applicable can produce very good solutions. When used inappropriately however... here be dragons ;-)

Upvotes: 0

Related Questions