Questions tagged [first-class-functions]

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.

132 questions
7
votes
2 answers

Dynamic function call in Go

I'm trying to dynamically call functions returning different types of struct. For example, let's take the following code. struct A { Name string Value int } struct B { Name1 string Name2 string Value float } func doA() (A) { …
Eric
  • 256
  • 1
  • 3
  • 8
7
votes
1 answer

Does a language with first-class functions necessarily allow closures?

I understand the broad concept of a closure (functions are stored along with a snapshot of environment at the time they were defined), and functions as first class citizens means that functions can be passed and returned like any other data type in…
6
votes
1 answer

passing functions as arguments in clojure

I have a function which takes a function and a number and returns the application of the function on the number, and a cube function: (defn something [fn x] (fn x)) (defn cube [x] (* x x x)) When I call the function as follows it…
Pranav
  • 3,340
  • 8
  • 35
  • 48
6
votes
1 answer

alternate ways to implement observer pattern in python

i was going through a post post about how observer pattern can be implemented in python . on the same post there are these comments. 1) In python you may as well just use plain functions, the ‘Observer’ class isnt really needed. 2) This is great…
anekix
  • 2,393
  • 2
  • 30
  • 57
5
votes
2 answers

Can function templates be used as first class citizens in higher order function calls?

Passing a function template as argument to another function template is always a bit tricky. Typically one has to resort to creating a lambda object that goes and calls the original function. Example template void f(It, It) {} void…
5
votes
2 answers

Are Kotlin functions really first class types?

Does the fact that this doesn't compile mean that they're not quite first class types? fun foo(s: String): Int = s.length // This won't compile. val bar = foo Is there a way to do this without resorting to OO?
F. P. Freely
  • 1,026
  • 14
  • 24
5
votes
2 answers

Can I create an anonymous on-the-fly class (implementation of an interface) in C++

In C++, can I create an implementation of an interface on the fly (which ideally binds local-scope variables.) Not sure how to explain it better, so I will put down what I would like the code to look like (roughly): // Given the following: class…
nappyfalcon
  • 909
  • 1
  • 10
  • 17
5
votes
1 answer

Error using "apply" function in Clojure: "Don't know how to create ISeq from: java.lang.Long"

Working on the following example in "Clojure in Action" (p. 63): (defn basic-item-total [price quantity] (* price quantity)) (defn with-line-item-conditions [f price quantity] {:pre [(> price 0) (> quantity 0)] :post [(> % 1)]} …
dtg
  • 1,803
  • 4
  • 30
  • 44
4
votes
3 answers

How many of these points in the definition of a first-class element do Java's functions satisfy?

Structure and Interpretation of Computer Programs gives the following as the conditions that an element of a programming language must satisfy to be considered first-class: They may be named by variables. They may be passed as arguments to…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
4
votes
2 answers

In Julia, why do scope rules for functions appear inconsistent compared to other first class objects?

The Julia docs state that functions are first class objects. I understand that to mean that I ought to be able to use and define them in the same way I would plain old data types. I'm therefore surprised that function a(b::Int64) if b > 0 …
hjab
  • 163
  • 6
4
votes
1 answer

Setting attributes on a method after class creation raises "'instancemethod' object has no attribute" but the attribiute is clearly there

In Python (2 and 3) we can assign attributes to function: >>> class A(object): ... def foo(self): ... """ This is obviously just an example """ ... return "FOO{}!!".format(self.foo.bar) ... foo.bar = 123 ... >>> a = A() >>>…
frnhr
  • 12,354
  • 9
  • 63
  • 90
4
votes
2 answers

Callback functions in TypeScript

I'm just getting started with Angular 2 and TypeScript and I can't seem to figure out how to use callback functions, I know this may be a silly question but given this regular javascript code: someOnject.doSomething('dsadsaks', function(data){ …
Leonardo Jines
  • 360
  • 5
  • 17
4
votes
2 answers

Can Functions Be Created at Run-time in Javascript?

Wikipedia's article on first-class citizens states that "some authors" believe functions are only first-class citizens in a language if the language supports their creation at run-time. This article written by James Coglan plainly calls functions…
Thomas
  • 6,291
  • 6
  • 40
  • 69
3
votes
2 answers

Desugar Lua operators

Since Lua supports first-class functions, I'd like to know if you can desugar operators, like in many functional languages. E.g in OCaml you can do: let x = (+) 3 5 The code above inits the variable x with the value 3 + 5. Writing (+) is equivalent…
elmattic
  • 12,046
  • 5
  • 43
  • 79
3
votes
1 answer

When using a function as a first-class value in Typescript, is there a way to pass Generic bindings?

I am attempting to wrap typescript functions, returning new functions that add extra behaviour. For example, in the minimal repro below, (see playground) the function after wrapping always returns a Promise and its arguments and return value are…
cefn
  • 2,895
  • 19
  • 28
1
2
3
8 9