Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

What is the Purpose of Closures?

Name: Anonymous 2012-02-29 8:48

I really don't understand what the purpose of closures is. It seems to me that everything that can be written using closures, is just as easily written without. For example:


void main()
{
    int[] haystack = [345,15,457,9,56,123,456];
    int   needle = 123;
    bool needleTest(int n)
    {
        return n == needle;
    }
    printf(find(haystack, &needleTest));
}


Wouldn't this be better written as:


void main()
{
    int[] haystack = [345,15,457,9,56,123,456];
    int   needle = 123;
    foreach(int i : haystack)
    {
        if (i == needle)
        {
            printf(i);
        }
    }
}


Or at least defining the function outside of main() and referencing it inside? I just don't understand the benefit of closures.

Name: Anonymous 2012-02-29 8:49

Read SICP.

Name: Anonymous 2012-02-29 11:31

closure mouth, ``faggot''

Name: Anonymous 2012-02-29 12:10

Closures can be seen as a form of runtime linking. You can create different functions that each have the same code, but refer to different values (for instance functions) that are lexically visible outside of the function body.

Without closures, functions can usually only refer to global variables and functions.

Name: Anonymous 2012-02-29 13:29

>>4
Wow that's a strange explanation but it's also a good one.

Upvoted, dub czechs, shop for a whopper, etc.

Name: Anonymous 2012-02-29 15:04

With closures, high order procedures can be used to create objects with their own changeable state and identity.

(define ((person name age sex) msg)
  (cond ((eq? msg 'speak)
         (list "hello, my name is" name "and I'm a" age sex))
        ((eq? msg 'birthday)
         (set! age (+ age 1))
         (list "yay! it's my mother fucking birthday and I'm"
                        age
                        "mother fucking years old! fuck yeah!"))
        (else (list "I don't understand your command"))))


]=> (define boy (person 'Joe 19 'male))
boy
]=> (define girl (person 'Jane 21 'trap))
girl
]=> (boy 'speak)
(hello, my name is Joe and I'm a 19 male)
]=> (girl 'speak)
(hello, my name is Jane and I'm a 21 trap)
]=> (boy 'birthday)
(yay! it's my mother fucking birthday and I'm 20 mother fucking years old! fuck yeah!)
]=> (boy 'speak)
(hello, my name is Joe and I'm a 20 male)
]=> (girl 'speak)
(hello, my name is Jane and I'm a 21 trap)


Read SICP.

Name: Anonymous 2012-02-29 18:19

>>6
not a lisp user but how would you go about finding joe or jane then seeing as boy and girl are not lists or arrays?

Name: Anonymous 2012-02-29 19:32

>>6
Listen to >>7. This is what's wrong with LISP.

Name: Anonymous 2012-02-29 21:04

Check the man pages for ftw. It's more of an argument for lambdas, but you suppose you wanted to do a file search for user-supplied names. You can't write a function for each one because you don't know until runtime. You could write a replacement for ftw that takes an additional parameter which it will supply to fn (this is common) or use a global (this is stupid.)

If you aren't using a posix system, try reading along with qsort. It takes a comparator. Suppose you had a parametric comparator—same deal.

Or you could use a closure in place of fn (oh but not in C, sorry.)

Non-enclosing lambdas are more common in my experience, such as arguments to map/fold/reduce/filter. Try learning a language besides C, Python or C++/Java (C++ and Java are getting closures though) and you'll probably see it immediately. If you still don't see it, try using JQuery—that'll learn you.

Name: Anonymous 2012-02-29 22:08

>>7-8
But that has nothing to do with the demonstration in >>7...

Name: Anonymous 2012-02-29 23:38

Apple felt that lambdas were so useful they should be added to C.

Name: Anonymous 2012-02-29 23:38

>>11
Whoops forgot my dubs

Name: Anonymous 2012-02-29 23:46

>>1
Closures are not so much a gimmick meant to fix something in particular like what the scripting languages introduce every month, it is a fundamental building block in some languages like Scheme, and thats pretty much it. Non-functional languages getting closures is a side effect of closures having extra flexibility over the original procedure definition mechanism of that language.

It seems to me that everything that can be written using closures, is just as easily written without.
For a function that maps data with a single closure, a for loop would be easier. But what about a function that maps data over several closures, sometimes with one, sometimes the other, one closure returns a number n, and the mapping function invokes Nth function for that element. Its useful to have the flexibility.

Or at least defining the function outside of main()
Lexical scope is where closures get its power, needleTest cannot access needle if its outside main(). If you passed needle as an extra argument, you lose the interfaceability with find().

Name: Anonymous 2012-03-01 6:04

according to "paradigms for dummies":

records (eg s-exp)
+ procedures
+ closures
= functional programming (eg scheme)

records
+ procedures
+ cells (state)
= imperative programming (eg c)

records
+ procedures
+ cells
+ closures
= stateful functional programming (eg ocaml)

Name: Anonymous 2012-03-01 7:31

+ cells (state)
What is a cell?

Name: Anonymous 2012-03-01 8:39

>>15
the place you will be locked up for the rest of yo'ure life once the jews convince the population that hobbyist programmers are terrorists

Name: Anonymous 2012-03-01 9:01

>>15
It's something your brain has. Some of us have more of them than others.

Name: Anonymous 2012-03-02 5:01

>>15

a cell is named state, aka a variable

Name: Anonymous 2012-03-02 5:05

anotha quote:

The closure therefore has two sets of references: a closed environment (from the definition) and the arguments (from each call).

Almost all programming languages (except for a few venerable ancestors such as Pascal and C) use this kind of closure:

* functions are closures;
* procedures are closures;
* objects are closures;
* classes are closures;
* software components are closures.

Name: Anonymous 2012-03-02 5:20

Use the power glove. It makes emacs usable.

Name: Anonymous 2012-03-02 10:28

You can also think of first order functions and closures as a class that only have one implicit method (call) and a constructor, which is the procedure literal expression.

The constructor is implicit in the code itself. It takes the variable names in lexical scope that are visible when the function literal is evaluated and makes the necessary ones part of the closure (the name describes this). What variables are those? Local variables and arguments, as well as any name outside the enclosing function (in case it also is a closure). This implies that when a procedure returns, any closure that was created might refer to local variables that are about to be popped. Either don't pop (like chicken scheme) or (more commonly) allocate the values that might be part of a closure on the heap.

Name: Anonymous 2012-03-02 11:24

Closures exist to check my dubs.

Name: Anonymous 2012-03-02 11:58

>>22

import prague;
import std.stdio;

void main()
{
    int n = getPostNum(currentThread.mostRecentPost);
    void checkem()
    {
        if (to!string(n)[0] == to!string(n)[1])
        {
            currentThread.post("Nice dubs, bro!");
        }
    }
}

Name: Anonymous 2012-03-02 12:08

>>23
This will spam threads with three digit post counts, you talentless hack!

Name: Anonymous 2012-03-02 12:16

>>24
getPostNum only returns values in the range of 1-99.

Name: Anonymous 2012-03-02 12:17

>>23
UuUUUu U UuuUUUu!!!??! uUu UuUu UUUUu u Uuuu u uuuu UUUUu u U??!??!! U uUu!??!?!! uuuu uu U!?!??!! U UUuu uuUu uUUUu Uu uuUu uUUUu UUUUu!!???!! uUUu U?!!??!! uuuu uu U!?!??!! uUUu uuUu U uUUUu UUUu u??!??!! UUuUuu UUuuuu UUUuUUu uUuuuu Uuuuuu UuuUuu uUUuuu uUUuuu UUuUuu UUUu UUuuuu UUUuUUu uUuuuu Uuuuuu UuuUuu uUUuUUu uuUuUUu UuUuuu UUUUUUu UuuuUUu Uuuuuu UUuUuu UUuuuu UUUuUUu uUuuuu Uuuuuu UuuUuu U U UUuUuu UUUu UUuuuu UUUuUUu uUuuuu Uuuuuu UuuUuu uuu!!!??! U UuUuUu!??!??! U uuUu!??!?!! U!????!! uUu uUUUu U UUuu UUu UuuUu Uuuu uUUUu UuUu!????!! uuu UUu Uuu U!?!??!! U uuuu uuuUu UuUuu uUUUUu?!??!?! uuUUu uuu U uuUUu!????!! U uuUu uuUu Uuuu UuuUu UuUUu!????!! uUUuuu uUuUUUu UUUuuu UUUuuu U uuuUUUu uuUuuu UUuuuu uUUUUUu Uuuuuu UUuuuu uUUUUUu UuUuuu!?!!?! UUUUu uu U uu UuUu uUUUu?!???!! uUu U!!!!?!! Uuuu u U u Uu uUu UuUu u??!??!! uuuu uu U!?!??!! uuUUuu UuuUuu uUuuUu UuuUUu UUuUuu U!!!!!! tomot

Name: Anonymous 2012-03-02 12:18

>>26
I'm >>23-san, and I thought that was hilarious !

Name: Anonymous 2012-03-02 12:26

>>25
Is there a reason why int is base 10 in D?

Name: Anonymous 2012-03-02 12:29

>>28
I think it's because humans have generally used base 10 as a counting system (though there are some notable exceptions), due to the fact that they have 10 fingers. It makes sense, then, that any programming language designed by humans will use base ten. There is, however, notational support in D for base 2, base 8, and base 16 numbers.

Name: Anonymous 2012-03-02 12:30

>>27
UuUUUu uUuuUUu U UUuuUUu!!!??! U u Uuu?!!!?!! uuuu uUUUu UUuu UUuu uuUu uuuu u U??!??!! uUu U!!!!?!! U uu?!???!! U!????!! Uuuuuu uuUUUUu uuuuuu uUUuuu U??!!?! uuuuuu uUUUUUu UUuUUUu uUUUUUu UUUUUUu U?!!!?! UuUUUu uUuuUUu U UUuuUUu!!!??! U uUuu!????!! Uu uuUu U UUuu u Uu u!!???!! uuuuuu uuuUuu uUuUUUu uUuUUUu uuUUuu uUUuuu uuuUuu U?!!!?! U!??!??! UUuu uuUu uuuu UUUUu u U??!??!! uuuu uu U!?!??!! UUUUu UUuu UuUUu uuuu U Uu!!???!! U uUUu UUuu?!!??!! U uUuu!????!! UuuUuu uuuuuu UUuUuu UUUUUUu uUuUuu UuuUuu uuUUUUu U uUuUuu UuuUuu UUuUuu UUUUUUu uUuUuu UuuUuu uuUUUUu!!!!?! UUuu U u uuUu???!?!! uUUUUUu uuUUUUu UuuUUUu UuUUUUu uUuuuu UuUUUUu uuUuuu U??!!?! uu U?!!??!! UuUu U Uuu!!!!?!! uUuUuu uuUUuu UUuUUUu uuuUuu U?!!!?! U uUUUUu UuuUUu?!!!??! uuUUu uuu U uuUUu!????!! UuUu U Uuu!!!!?!! Uu U uUuu!????!! U uUUUu U uUUu!?!??!! uUUUu UUu U uu!????!! UuuUUUu UuUuuu uuUuuu uUuUUUu U?!!!?! tomot

Name: Anonymous 2012-03-02 12:32

>>29
So does D store numeric types in 4-bit-encoded base 10 like COBOL? (I forget the name of this practice)

Name: Anonymous 2012-03-02 12:36

>>31
No, it stores them as 32 bit base 2 numeric types; and the term you're looking for is Binary Coded Decimal.

Name: Anonymous 2012-03-02 12:36

dubs n = (n `mod` 100) `mod` 11 == 0

Name: Anonymous 2012-03-02 17:23

>>33
That's it. Dubs are checked. Everyone go home. /prog/ is over.

Name: Anonymous 2012-03-03 1:50

>>30
I thought it was kind of funny too. Free-thinking rule redefining rebels? LOL

Name: Anonymous 2012-03-03 6:08

>>31
It's called BCD. (Binary-Coded Decimals, or so)

Don't change these.
Name: Email:
Entire Thread Thread List