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

Pages: 1-

closures

Name: Anonymous 2011-11-07 17:48

this thread is dedicated to javascript's most important innovation: the concept of functions that capture their lexical environment

Name: Anonymous 2011-11-07 17:57

Name: Anonymous 2011-11-07 18:14

If only it had let-expressions to make it easier to introduce a new scope.

Name: Anonymous 2011-11-07 18:25

If it ain't Lisp, it's crap.

Name: Anonymous 2011-11-07 18:32

>>3
implying function scope isn't superior
10/10

Name: Anonymous 2011-11-07 21:44


var arr = [];
for (var i = 0; i < 10; i++ ) {
  arr[i] = (function (i) {
    return function () {
      return i;
    };
  })(i)
}

beautiful!

i'd like to see your language do the same thing more elegantly!

Name: Anonymous 2011-11-07 23:03

>>6

(define (incarr n)
 (define (incarr-iter n i)
 (cond
  ((= n i) '())
  (else (cons i (incarr-iter n (+ i 1))))))
 (incarr-iter n 0))

(define arr (incarr 10))

Name: Anonymous 2011-11-08 0:40

>>7
Terrible!

Name: Anonymous 2011-11-08 6:11

>>7
/puke

Name: Anonymous 2011-11-09 1:10

>>8

hrmmm let me twi:


(define (range i j)
  (letrec [(helper (lambda (j acc)
                     (if (> i j)
                       acc
                       (helper (- j 1) (cons j acc)))))]
    (helper j '())))

(define arr (map (lambda (i)
                   (lambda () i))
                 (range 1 10)

Name: Anonymous 2011-11-09 1:31

>>1
They're hardly the first to invent closures.
>>6
(loop for i from 0 to 10 collect (let ((a i)) #'(lambda () a))) (Would have been (loop for i from 0 to 10 collect #'(lambda () i)), if not for how loop's bindings work)
Alternatively:
(mapcar #'(lambda (i) (lambda () i)) (loop for i from 1 to 10 collect i))
or

(defun seq (a b) (loop for i from a to b collect i))
(mapcar #'(lambda (i) (lambda () i)) (seq 1 10))

Do you want an array instead of a list? just change 2 atoms: (map 'vector #'(lambda (i) (lambda () i)) (seq 1 10))

Much more elegant, don't you agree?

Name: Anonymous 2011-11-09 3:41

>>6,11
arr := map(->i[ ->[ i ] ], 0..10);

Lisp is shit.

Name: Anonymous 2011-11-09 4:01

>>12
Enjoy your linenoise.

Name: Anonymous 2011-11-09 4:13

>>13
Enjoy your soup of parens.

Name: Anonymous 2011-11-09 4:24

>>14
Why thank you! Paredit makes them quite edible.

Name: Anonymous 2011-11-09 4:26

>>15
Still unreadable.

Name: Anonymous 2011-11-09 6:50

map(a, ->x[ x+y ])
vs
(map a (lambda (x) (+ x y)))

Lisp is shit.

Name: Anonymous 2011-11-09 8:29

>>13
A CLer calling another language line noise?  I thought I'd seen everything!

Name: Anonymous 2011-11-09 9:39

>>17
Symta (a dialect of Lisp made completely on Lisp by a crazy hobbyist):

map ?+Y A


Python (a language developed by a team of pro-codemonkeys at google):

map(a, ->x[ x+y ])

Name: Anonymous 2011-11-09 10:06

>>19
Symta is somehow less readable than Lisp, and that's not valid Python. Go back to /g/.

Name: FrozenVoid !!mJCwdV5J0Xy2A21 2011-11-09 12:09

>most important innovation
Ability to prototype code in minutes. Closures are for leaking memory and messing up stack frames: they are completely useless, despite what CS courses tell you.

Name: Anonymous 2011-11-09 12:19

>>18
It's all about mental maps. When I read Lisp code I understand the semantics of the code nearly instantly, and if I'm missing something, using SLIME's x-ref/macroexpand features is only a key-chord away. CL can be as difficult to read as normal languages when someone (ab)uses readtables extensively, but by default CL's syntax is easy to remember (usually it's just a few prefixes on some forms which are directly expanded as they should be into regular s-exp's (most of the time, technically you can generate in-memory objects at read-time, and in some cases this is used)).

Name: Anonymous 2011-11-09 12:33

>>19
a dialect of Lisp made completely on Lisp by a crazy hobbyist
Split personality disorder?

Name: Anonymous 2011-11-09 12:38

(assert (shitp *lisp*))

Name: Anonymous 2011-11-09 12:57

>>21
Ability to prototype code in minutes.
sounds more like a python invention, ``faggot''

Name: Anonymous 2011-11-09 13:02

>>24

The assertion (SHITP *LISP*) failed.
   [Condition of type SIMPLE-ERROR]

Restarts:
 0: [CONTINUE] Retry assertion.
 1: [RETRY] Retry SLIME REPL evaluation request.
 2: [*PROCESS-INPUT] Continue reading input.
 3: [ABORT] Return to SLIME's top level.
 4: [CLOSE-CONNECTION] Close SLIME connection.
 5: [ABORT] Exit debugger, returning to top level.

Backtrace:
  0: (SB-KERNEL:ASSERT-ERROR (SHITP *LISP*) NIL NIL)[:EXTERNAL]
  1: ((LAMBDA ()))
  2: (SB-INT:SIMPLE-EVAL-IN-LEXENV (ASSERT (SHITP *LISP*)) #<NULL-LEXENV>)
  3: (SWANK::EVAL-REGION "(assert (shitp *lisp*))\n")

Name: Anonymous 2011-11-09 13:55

>>26
No, no, it really is shit.

Name: Anonymous 2011-11-09 13:57

Prototyping anything of value in minutes of jabbascript is ass.

Name: Anonymous 2011-11-09 21:22

not using classes
2011

Name: Anonymous 2011-11-09 21:42

not doing functional programming
2011

Name: Anonymous 2011-11-10 8:20

>>30
Functional programming is great, but Lisp is shit.

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