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
1
vote
1 answer

wrong number of arguements clojure

I'm building a program that's meant to allow users count either the number of letters or number of words in a string, however when running the program through the cmd, I'm getting a clojure.lang.ArityException, wrong number of args (1) passed to :…
Karab
  • 23
  • 3
1
vote
1 answer

Is there anything python function decorators can do that I can't do with first class functions?

I'm trying to understand Python function decorators, but so far I don't see any case where a function decorator can do something I couldn't do using first class functions. As an example, the logger decorator is often given as a simple example of a…
arnfred
  • 313
  • 3
  • 10
1
vote
1 answer

How do i refer to a get function as an object

I'd like to reference a get function as a Function object rather than as the value that it returns. Normally i would be able to simply refer to the function without parenthesizes like so: private function getFoo():int { return…
1
vote
1 answer

What exactly are First Class Functions?

I searched the web to try and understand this term. I understand that 'first class functions' are functions that can be assigned to variables and 'passed around'. But I don't actually understand what this means. So what does it mean? What are First…
Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
1
vote
4 answers

Java 8 dancing around functions as first class citizens?

So the functional programmer in me likes languages like python that treat functions as first class citizens. It looks like Java 8 caved to the pressure and "sort of" implemented things like lambda expressions and method references. My question is,…
C.B.
  • 8,096
  • 5
  • 20
  • 34
1
vote
2 answers

What is different between First-class function and Anonymous function?

I saw two concepts First-class function Anonymous function It seems that these two concepts are the same? (lambda) I'm confused?
1
vote
2 answers

Lua: how to verify that a table contains a specific function

I'm developing a module that returns a table full of functions based on the arguments that are passed in. Specifically, the module returns a set of data transformation rules (functions) that need to be applied to a data set depending on which…
Mitch A
  • 2,050
  • 1
  • 21
  • 41
1
vote
1 answer

Function with varargs in constructor

I am trying to get this working in Scala: class MyClass(some: Int, func: AnyRef* => Int) { } The above code won't compile (why?) but the following does: class MyClass(some: Int, func: Seq[AnyRef] => Int) { } That's OK but are the two equivalent?…
nobeh
  • 9,784
  • 10
  • 49
  • 66
0
votes
4 answers

What is the equivalent of passing functions as arguments using an object oriented approach

I have a program in python that includes a class that takes a function as an argument to the __init__ method. This function is stored as an attribute and used in various places within the class. The functions passed in can be quite varied, and…
savagent
  • 2,114
  • 1
  • 13
  • 10
0
votes
0 answers

Using FnMut, Fn, FnOnce as arguments to a function

I don't quite understand when/how a function can use bounds of types Fn, FnOnce, FnMut. Can someone explain in what scenario these would be used and how one would use them? As I understand it, I can create a function like this: struct Dog { …
0
votes
1 answer

On the fly function expression doesn't throw error when triggering with first class

I am learning Javascript and I came across these concepts of function expressions & first class functions. While I do understand their definition, I am unable to understand why my code behaves the way it does. I have a first class function which…
0
votes
1 answer

Why are functions only sometimes first class variables in R? Using them from built-in data structures causes "Error: attempt to apply non-function"

I am trying to understand how first class functions work in R. I had understood that functions were first class in R, but was sorely disappointed when applying that understanding. When a function is saved to a list, whether that be as an ordinary…
David
  • 300
  • 1
  • 14
0
votes
0 answers

How to use std::function with template as argument

I'm trying to understand templates and I have this piece of code: #include #include using namespace std; template bool myCompare(const T& a, const T& b) { return a>b; } template bool encCompare(const…
0
votes
2 answers

How to write a JavaScript function that works both as a first-class function and higher-order function?

For example let's take the function add. I want to be able to call: add(1, 2)(3, 4) and add(1, 2) using same implementation. I have tried writing the add function the following way: var add = (a, b) => { return (c, d) => { return a +…
0
votes
1 answer

Making a first-class Function with arguments in Python

My issue is simple: I want to make a first-class function in python that has some arguments in it. But the problem is that to assign it, you need to call it, wich can be problematic def print_double(num): print(num*2) a = print_double(4) I…
Stepa1411
  • 27
  • 3
1 2 3
8 9