How closure is used in functional languages

For some reason, I tend to associate closures with functional languages. I believe this is mainly due to the fact that the discussions that I saw regarding closure are almost always in an environment that focuses on functional programming. At the same time, the actual practical use of closures, which I can think of, is all non-functional.

Are there practical applications of closures in functional languages ​​or an association in my mind, mainly because closures are used for programming in a style that is also common to functional programming languages ​​(first-class functions, currying, etc.)?

Edit: I have to clarify that I refer to the actual functional languages, that is, I was looking for applications that preserve referential transparency (for the same input, you get the same result).

Edit: Add a summary of what has been published so far:

  • Closure is used to perform a partial assessment. In particular, for a function that takes two arguments, it can be called with one argument, which returns a function that takes one argument. Typically, the method by which this second function “stores” the first value passed to it is a closure.
  • Objects can be implemented using closures. A function is returned that closes next to a series of variables and can then use them as attributes of objects. The function itself can return more methods that act as object methods that also have access to these variables. Assuming the variables are not changed, referential transparency is maintained.
+3
source share
4 answers

, . classify(), , , leftClassify() rightClassify() . . Python D .

+3

Javascript ( - , ). , .

:

var generateId = function() {
    var id = 0;
    return function() {
        return id++;
    }
}();
window.alert(generateId());
window.alert(generateId());

, Javascript. .

, , . - :

var slide = function() {
    var photoSize = ...
    var ... // lots of calculations of sizes, distances to scroll, etc
    var scroll = function(direction, amout) {
        // here we use some of the variables defined just above
        // (it will be returned, therefore it is a closure)
    };
    return {
        up: function() { scroll(1, photoSize); },
        down: function() { scroll(-1, photoSize); }
    }
}();

slide.up();
// actually the line above would have to be associated to some
// event handler to be useful

, : Javascript " " slide.up().

+7

. , , :

let compose f g = fun x -> f (g x)

, , . , OCaml Haskell, . :

let flip f a b = f b a

let minusOne = flip (-) 1 , 1 . " " , :

let flip f a = fun b -> f b a

, , , .

+3

Closures can be used to simulate objects that can respond to messages and maintain their own local state. Here is a simple counter object in the diagram:

;; counter.ss
;; A simple counter that can respond to the messages
;; 'next and 'reset.  

(define (create-counter start-from)
  (let ((value start-from))
    (lambda (message)
      (case message
    ((next) (set! value (add1 value)) value)
    ((reset) (set! value start-from))
    (else (error "Invalid message!"))))))

Sample Usage:

> (load "counter.ss")
> (define count-from-5 (create-counter 5))
> (define count-from-0 (create-counter 0))
> (count-from-5 'next)
6
> (count-from-5 'next)
7
> (count-from-0 'next)
1
> (count-from-0 'next)
2
> (count-from-0 'reset)
> (count-from-0 'next)
1
+1
source

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


All Articles