When would it be appropriate to pass a function to a function?

Good, so you can pass a function to another function. Passing a function to another function in ActionScript 3

This is obviously a very powerful, but more important question, when does it make sense, since performance overhead occurs when calling another function?

+3
source share
6 answers

A function that takes a function as an argument is called a higher order function . Google has a lot of information about this.

Examples of higher order functions:

function compose(f, g) {
  return function(x) {
    return f(g(x));
  };
}

function map(f, xs) {
  var ys = [];
  for(var i = 0; i < xs.length; ++i)
    ys.push(f(xs[i]));
  return ys;
}

With this, you can convert an array with two functions in a string:

var a = ["one", "two", "three"];
var b = map(compose(toUpperCase, reverse), a);
// b is now ["ENO", "OWT", "EERHT"]
+3

, , , , .

AddEventListener EventDispatcher , , :

addEventListener ( String, : , useCapture: Boolean = false, priority: int = 0, useWeakReference: Boolean = false): void

http://livedocs.adobe.com/flex/3/langref/flash/events/EventDispatcher.html

. , .

+4

, OO.

, . . . , .

  (define sum
    (lambda (ls)
      (if (null? ls)
          0
          (+ (car ls) (sum (cdr ls))))))

  (define product
    (lambda (ls)
      (if (null? ls)
          1
          (* (car ls) (product (cdr ls))))))

, + - (0 1). , , .

, . .

  (define fold
    (lambda (proc id)
      (lambda (ls)
        (if (null? ls)
            id
            (proc (car ls) (fold (cdr ls) proc id))))))

  (define sum (fold + 0))
  (define product (fold * 1))

sum product. , . - , .

+4

1 - AJAX javascript

namespace.class.method(parm1, parm2, callback,onErr);

, , callBack,

function callback(result) {
      $('#myDiv').innerHTML = result;
}

, .

+1

- , ​​, , , , , , . , , . "" , , . OO, , , ..

+1

, , , , .

(a > )

> - , . , , , , .

(sort (lambda (a b) ( > ( a) ( b))) list-to-sort)

, , , , .

HOF - //, .

0

Source: https://habr.com/ru/post/1724869/


All Articles