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
104
votes
5 answers

Python - Passing a function into another function

I am solving a puzzle using python and depending on which puzzle I am solving I will have to use a special set of rules. How can I pass a function into another function in Python? Example def Game(listA, listB, rules): if rules == True: …
Hildur Sif Thorarensen
84
votes
7 answers

Assigning a function to a variable

Let's say I have a function def x(): print(20) Now I want to assign the function to a variable called y, so that if I use the y it calls the function x again. if i simply do the assignment y = x(), it returns None.
stensootla
  • 13,945
  • 9
  • 45
  • 68
54
votes
7 answers

What is a first-class-citizen function?

What is a first class citizen function? Does Java supports first class citizen function? Edit: As mention on Wikepedia First class functions are a necessity for the functional programming style. Is there any other use of first class functions?
Alpine
  • 3,838
  • 1
  • 25
  • 18
36
votes
4 answers

How do I reference a function in Ruby?

In python, it's fairly straightforward to reference a function: >>> def foo(): ... print "foo called" ... return 1 ... >>> x = foo >>> foo() foo called 1 >>> x() foo called 1 >>> x >>> foo
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
32
votes
8 answers

What is first class function in Python

I am still confused about what first-class functions are. If I understand correctly, first-class functions should use one function as an object. Is this correct? Is this a first-class function? def this_is_example(myarg1): return myarg1 def…
rwrwerwer
  • 381
  • 1
  • 3
  • 6
28
votes
2 answers

What is the difference between delegates in C# and functions as first class values in F#?

More specifically what are the characteristics (if any) that delegates have that functions as first class values in F# don't have; and what are the characteristics that functions as first class values have (if any) that delegates in C# don't have?
Steve Ellinger
  • 3,957
  • 21
  • 19
26
votes
6 answers

What does it mean when the parentheses are omitted from a function or method call?

I have this example code: class objectTest(): def __init__(self, a): self.value = a def get_value(self): return self.value a = objectTest(1) b = objectTest(1) print(a == b) print(a.get_value() ==…
user3311142
  • 345
  • 1
  • 4
  • 13
19
votes
2 answers

Why isn't the Ruby 1.9 lambda call possible without the dot in front of the parentheses ?

I checked out the latest Ruby version, to play a bit with the latest changes. The first thing I tried to do was call a Ruby lambda/block/proc just like you'd do with a Python callable. a = lambda {|x| puts x} a.call(4) # works, and prints 4 a[4] #…
Geo
  • 93,257
  • 117
  • 344
  • 520
18
votes
4 answers

If functions in JS are first-class, what allows them to be called before they are defined?

Don't first class functions mean that they behave as variables? Clearly they don't behave exactly like variables though, since this: console.log(foo); var foo = 'bar'; ...doesn't work, whereas this: console.log(foo()); function foo() { …
wwaawaw
  • 6,867
  • 9
  • 32
  • 42
13
votes
7 answers

Overriding Methods vs Assigning Method Delegates / Events in OOP

This is a bit of an odd oop question. I want to create a set of objects (known at design time) that each have certain functions associated with them. I can either do this by giving my objects properties that can contain 'delegates': public class…
Flash
  • 15,945
  • 13
  • 70
  • 98
10
votes
3 answers

Swift any difference between Closures and First-Class Functions?

In the Swift documentation Apple says this: Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming…
Jeggy
  • 1,474
  • 1
  • 19
  • 35
9
votes
5 answers

“Functions are a first-class type” in swift?

the little knowledge , I have about first class function is that it supports passing functions as arguments and we can also return them as the values in another function ... I am very new in Swift programming language can anyone please elaborate it…
Anurag Bhakuni
  • 2,379
  • 26
  • 32
9
votes
1 answer

ScalaFx: Event Handler with First Class Function

i try to write an event handler in a scalaFx app. I found followin solution: import scalafx.scene.control.ListView import javafx.scene.input.MouseEvent import javafx.event.EventHandler ... val list = new ListView[String] { …
USSEraser
  • 315
  • 2
  • 11
9
votes
2 answers

Pass a function (with arguments) as a parameter in PowerShell

I've been successfully passing no-argument functions around in PowerShell using ScriptBlocks. However, I can't get this to work if the function has arguments. Is there a way to do this in PowerShell? (v2 preferably) Function Add([int] $x, [int] $y) …
8
votes
1 answer

Using overloaded functions as first class citizens

Suppose that we have several overloaded functions in one class: func appendToABC(string s: String) -> String { return "ABC \(s)" } func appendToABC(duplicatedString s: String) -> String { return "ABC \(s)\(s)" } And we have some API that…
Anton Tyutin
  • 626
  • 1
  • 5
  • 15
1
2 3
8 9