Skip to main content

All Questions

0 votes
1 answer
210 views

Scope error when returning function from function [duplicate]

I am having issues with using function generators in python, i.e. returning functions from a function, where some of the inputs are defined via the outer function's arguments, yielding a function with ...
teepee's user avatar
  • 2,714
9 votes
2 answers
4k views

How to implement a multi-level currying function in Rust?

I attempt to implement a currying function similar to Functional Programming Jargon in Rust: fn add_origin(x: i32) -> impl Fn(i32) -> i32 { return move |y| { x + y }; } fn main()...
rustyhu's user avatar
  • 2,175
0 votes
1 answer
58 views

What is the flow of execution with this compose function passed into Javascripts reduce?

I just want to know how reduce works in the case of code below(which was provided by a stackoverflow user in my previous question, i'm asking this question as his code snippet led to me having more ...
SuthanS's user avatar
  • 53
0 votes
1 answer
98 views

Is there anyway to tell if a function has a state in Javascript?

a = function() { if(this.n === undefined) { this.n = 0; } this.n += 1; return(this.b); } b = function() { return(5); } In this case a would stateful, and b would be ...
gersh's user avatar
  • 2,437
0 votes
1 answer
571 views

Is closure is the way to avoid global variable?

I know that using global variable is not good practice and programmers should avoid it when possible. func foo(a *A) func() *A { return func() *A { return a } } If I call foo_closure ...
Kenenbek Arzymatov's user avatar
0 votes
1 answer
55 views

R: closure can't find object until called

I have a problem with a higher order function in R: power <- function(x , modify){ return( function(y){ return( modify( y^x ) ) } ) } mod <- function(z){z+1} sq <-...
David Heckmann's user avatar
1 vote
0 answers
2k views

What is the best way to implement a parametric function?

Since Python believes There should be one-- and preferably only one --obvious way to do it. I wonder if there is a best way to use parametric functions in Python. I came across with two approaches, ...
Nicholas's user avatar
  • 2,668
-1 votes
1 answer
127 views

Swift: Calling impure function with another function

I am attempting to use a helper function to call an input context sensitive function within the same context. Within the context of a class function called 'drawGrid()' I have the following code and ...
Timothy Swan's user avatar
2 votes
2 answers
53 views

Why would a function be returned in this example instead of just a string

In an answer to this question, I can see the value of i being retained by sort of throwing it into another function: var funcs = []; function createfunc(i) { return function() { console.log("My ...
1252748's user avatar
  • 15.4k
4 votes
3 answers
484 views

"Side-effecting lexical closure" vs function in Scala

In his answer's comment section, Apocalisp states the following: Well, you did ask for a function. A side-effenting [sic] lexical closure is emphatically not a function. What exactly does he mean ...
corazza's user avatar
  • 32.4k
1 vote
3 answers
132 views

Can someone explain to me the difference between a Function Object and a Closure

By "Function Object", I mean an object of a class that is in some sense callable and can be treated in the language as a function. For example, in python: class FunctionFactory: def __init__ (...
Owen's user avatar
  • 1,736
0 votes
5 answers
202 views

is this a good example of a closure?

I saw this as an example on MDN and didn't understand why this was a Good Thing (TM). function makeSizer(size) { return function() { document.body.style.fontSize = size + 'px'; ...
dotnetN00b's user avatar
  • 5,141
6 votes
3 answers
2k views

Do you use Template Method Pattern in programming languages with closures/delegates/function pointers?

I have been going back and forth between C# and Java for the last 8 years. One thing that strikes me is that I have completely stopped using the "Template Method" design pattern in C#. Actually, in ...
krosenvold's user avatar
  • 77.3k
999 votes
18 answers
192k views

What is the difference between a 'closure' and a 'lambda'?

Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused. And now that we're here, how do they differ from a regular function?
sker's user avatar
  • 18.4k