First-class functions can be assigned to variables, passed to other functions as arguments, or returned from other functions. The ability to return a function enables deferred execution. Functions that accept other functions as arguments are called higher-order functions.
Questions tagged [first-class-functions]
132 questions
2
votes
1 answer
Ruby passing method
Trying to understand ruby's complexity, but makes no sense so far.
5.times(method(:puts))
Gives error, that doesn't make a lot of sense. Do I have some kind of syntax error or is it not possible to do in ruby?
ArgumentError: wrong number of…

Ebuall
- 1,306
- 1
- 12
- 14
2
votes
4 answers
Sum of range in Javascript in function local variable
I have range function and output functions they works correct,now I want create sum function for using as callbac function in range function,but when some function executed local variable let us say total or sum initialize 0(zero),how can solve this…
user7596377
2
votes
1 answer
C++: Template argument deduction when passing template functions as argument to other template functions
I have the following snippet of example code:
The idea is that there is a container class, here called Box, and that we might want to create new versions of this container by mapping a function over its current contents.
#include
#include…

Qqwy
- 5,214
- 5
- 42
- 83
2
votes
3 answers
Python: variables inside class methods
I'm learning python and am trying to write a wound system based on hot zones of a character. Here's what I've written. Don't judge me too much.
class Character:
def __init__ (self, agility, strength, coordination):
self.max_agility =…

Lui Martinez Laskowski
- 67
- 1
- 10
2
votes
2 answers
F# function composition where the first function has arity >1
I have two functions f and g:
let f (x:float) (y:float) =
x * y
let g (x:float) =
x * 2.0
I want to compose (>>) them to get a new function that performs f and then g on the result.
The solution should behave like:
let h x y =
(f…

sdgfsdh
- 33,689
- 26
- 132
- 245
2
votes
3 answers
How to store a function in a variable in Lisp and use it
I want to store a function like print in a variable so that I can just type something short like p, e.g:
In Scheme:
(define print display)
(print "Hello world\n")
;; alternate way
(define print 'display)
((eval print) "Hello world\n")
The same…

Plakhoy
- 1,846
- 1
- 18
- 30
2
votes
3 answers
How can I get a reference to a method that contains the arguments used for invocations, in Ruby?
Given this code:
a = {1=>2}
m = a.method(:[])
I know that I can now use :
value = m.call(1)
and it will return 2. The thing is, what do I need to change so that I can call the method directly like :
m.call()
and it will get the 1 sent as a…

Geo
- 93,257
- 117
- 344
- 520
2
votes
4 answers
Using function names as parameters
In Go, you can pass functions as parameters like callFunction(fn func). For example:
package main
import "fmt"
func example() {
fmt.Println("hello from example")
}
func callFunction(fn func) {
fn()
}
func main() {
…

lmorg
- 113
- 1
- 7
1
vote
4 answers
How to assign to members of a function?
Since functions are first class objects, it should be possible to assign to the members of them.
Am I right thinking that arguments.callee does this?
Are there any other ways to set these fields?
How is it possible to set field in first…

Dims
- 47,675
- 117
- 331
- 600
1
vote
1 answer
are first order function and first class functions same in javascript..?
what are first class functions and first order functions..? Both are same or not..? Is there any similarities/difference between first order and first class functions in javascript.
I tried to get an answer but didn't get a well-explained one.…

Jackson
- 21
- 3
1
vote
1 answer
Scala literal function (syntax) - two versions, same result - what is the difference?
I'm a Scala beginner. Can someone explain me what is the difference (except syntax) between these two lines of code (though they return the same result)? I wrote them practicing literal functions and try to find out if there is anything up 'behind…

vonBluten
- 11
- 2
1
vote
1 answer
Representing a queue as a procedure with local state
Section §2.1.3 at page 90 explains, with a very clear example, that first class functions in a language make functions themselves and data be the same thing looked at from different perspectives, or, to cite the book:
the ability to manipulate…

Enlico
- 23,259
- 6
- 48
- 102
1
vote
1 answer
Invoking a function from a class member in PHP
Say I have the following:
class C {
private $f;
public function __construct($f) {
$this->f = $f;
}
public function invoke ($n) {
$this->f($n); // <= error thrown here
}
}
$c = new C(function ($m) {
echo…

Andreas Grech
- 105,982
- 98
- 297
- 360
1
vote
2 answers
Force evaluation with deparse in R
I have a function that produces a function:
fun1 <- function (x)
return (function() x)
By the time a function is produced, the value of x is already basically a constant:
fun2 <- fun1 ("a")
However, when I have fun2 printed, it won't show the…

Kamil S.
- 406
- 3
- 10
1
vote
1 answer
Swift: Calling an Enum value converter function as a first class function
enum SolarSystemPlanet: String, CaseIterable {
case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
func toRawValue(_ value: SolarSystemPlanet) -> PlanetName {
value.rawValue
}
}
With the enum above, one way to…

Tanin
- 585
- 7
- 10