patternMinor
Is there one (or a few) canonical/ specific use cases for "functions returning functions" (beyond "decorators")?
Viewed 0 times
canonicalonefewreturningforbeyondspecificfunctionstheredecorators
Problem
-
Is there one (or a few) canonical/ specific use cases for "functions returning functions" (beyond "decorators")?
-
What can you do with "functions returning functions" that you cannot (easily or elegantly or expressively) do with functions as variables ("functions taking functions as their arguments") and/or composing functions, as in :
Is there one (or a few) canonical/ specific use cases for "functions returning functions" (beyond "decorators")?
-
What can you do with "functions returning functions" that you cannot (easily or elegantly or expressively) do with functions as variables ("functions taking functions as their arguments") and/or composing functions, as in :
(f(g(x)).Solution
A common reason to return a function is when programming with iterators. Specifically, when you have a data structure (such as a list or set) and you want to return an iterator over it.
In a pure functional style, an iterator is an initial state
where S is the state of the iterator and A is the output type of the iterator.
It takes in a current state and returns either None or a value and a new state.
So, if you want to define an iterator over a list, you need to write a function
where
There's no way to avoid returning a function here because an iterator is something that you call repeatedly (every time you want a new element, you ask the iterator to generate a new element).
To give an even simpler example along the same lines, sometimes you want an iterator over all integers starting at a given integer. Then you would write a function
where
In a pure functional style, an iterator is an initial state
init: S together with a functionnext: S -> Optionwhere S is the state of the iterator and A is the output type of the iterator.
It takes in a current state and returns either None or a value and a new state.
So, if you want to define an iterator over a list, you need to write a function
iterate_over_list: List A -> (S, S -> Option)where
S is defined appropriately -- depending on your implementation it could either be (List A, int) (the list together with an index into it) or just List A if you consume the list while iterating over it.There's no way to avoid returning a function here because an iterator is something that you call repeatedly (every time you want a new element, you ask the iterator to generate a new element).
To give an even simpler example along the same lines, sometimes you want an iterator over all integers starting at a given integer. Then you would write a function
iterate_starting_from: int -> (S, S -> Option)where
S in this case would just be int (the state of the iterator is the current integer to be returned). The implementation of this is something like (in pseudocode):iterate_starting_from(i):
let init = i
let next = lambda x: Some((x, x+1))
return (init, next)Code Snippets
next: S -> Option<(A, S)>iterate_over_list: List A -> (S, S -> Option<(A, S)>)iterate_starting_from: int -> (S, S -> Option<(int, S)>)iterate_starting_from(i):
let init = i
let next = lambda x: Some((x, x+1))
return (init, next)Context
StackExchange Computer Science Q#143427, answer score: 5
Revisions (0)
No revisions yet.