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
0 answers

Can I rewrite this code to avoid the F# error

I have the following code in F# live version at https://repl.it/repls/CarefulGiganticExtraction let inline tryParse text = let mutable r = Unchecked.defaultof<_> (^a : (static member TryParse: string * ^a byref -> bool) (text, &r)), r let…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
1
vote
1 answer

Initializing a class variable with array of class functions

I would like to create an array of member functions as a class variable so I can switch throughout the class etc. The following doesn't work class A: def b(self): pass def c(self): pass d = [A.b, A.c] because A is not…
1
vote
2 answers

Is it possible to use a reference to some function's call attribute and pass it around as a value?

This doesn't work: -> f = Number.prototype.toLocaleString.call <- ƒ call() { [native code] } -> typeof f <- "function" -> f(1) <- Uncaught TypeError: f is not a function at :1:1 Is it possible to reference and use some function's…
1
vote
0 answers

Is it possible to get a first class function with default parameters?

I have a bunch of similar functions, which all have the same parameters. Some of the parameters have a default value. Here are two of them: func debug(_ message: String, eventid: String = Foundation.UUID().uuidString, logFile: String = "log.log",…
Josef Zoller
  • 921
  • 2
  • 8
  • 24
1
vote
4 answers

Elixir's anonymous function written in external .ex file can't run with interactive shell

I can write an anonymous function in the interactive shell directly like below. iex> total_bottles_milk = fn total -> total * 2 end iex> total_bottles_milk.(2) However, if I write in an external file and run in the interactive shell, it shows a…
1
vote
1 answer

Assign function.call to a variable

I have this code var a = function(){} var b = a.call b() // TypeError: b is not a function typeof b is 'function' and console.log(b) shows ƒ call() { [native code] }. Can someone please explain this behaviour?
1
vote
1 answer

Understanding "Side Effects" in Javascript with Respect to First-class Functions

I've run into a particularly interesting case of what I believe to be a side-effect in Javascript. I'm trying to wrap my head around scope and the fact that functions are first-class citizens. Please let me know if I'm on the right track, and…
MFave
  • 1,044
  • 2
  • 8
  • 19
1
vote
1 answer

Using Swift3 Function Parameter in Selector

I'm attempting to pass in a function to add as a target to a UIButton in Swift3, but I'm getting the following error: Argument of '#selector' does not refer to an '@objc' method, property, or initializer Here's my code: func updateButtonAction(_…
rmooney
  • 6,123
  • 3
  • 29
  • 29
1
vote
1 answer

Implement F# interface via tacit programming

An idea from tacit programming is to not apply arguments to functions if it can be avoided. Why doesn't F# allow this to compile if functions are first class members? type IAdder = interface abstract member Add : int -> int -> int end type…
1
vote
2 answers

Documenting first-class-assigned functions

I have a function defined by making use of the first-class nature of Python functions, as follows: add_relative = np.frompyfunc(lambda a, b: (1 + a) * (1 + b) - 1, 2, 1) Either I need a way to add a docstring to the function defined as it is, or…
1
vote
1 answer

How do I copy a function to a new symbol?

I've got a question regarding the copy of function in Common Lisp. In Scheme I'd go with: (define (foo par1 par2) (+ par1 par2)) (define bar foo) (print (bar 1 2)) ;; --> prints 3 (define (foo par1 par2) (* par1 par2)) (print (bar 1 2)) ;; -->…
Y.S
  • 311
  • 1
  • 10
1
vote
2 answers

Ruby anonymous classes as first-class functions

Ruby does not have first class functions; although it has procs and lambdas, these notoriously require significant overhead. (Python has first class functions, apparently without the overhead.) It occurred me to that first class functions can be…
user5699217
  • 111
  • 1
  • 3
1
vote
2 answers

How can the compiler decide if self needs to be passed?

I am working on a new class based, dynamically typed programming language where functions are first class objects. Functions defined inside a class (aka methods) are called passing self as first parameter while globally defined functions does not…
1
vote
1 answer

Are Hack named functions fully first-class citizens?

HHVM 3.9 is not such a fan of ternary statements with named functions, even when passed through fun(), but ≥3.10 is totally fine with them. It seems as though this is one of few cases, however, because 3.9 does accept named functions returned from…
concat
  • 3,107
  • 16
  • 30
1
vote
2 answers

Array of first-class functions as an instance variable in Swift

I experience the following problem trying to create a variable that stores an array of functions: class MySwiftClass { // Compilation Error: '()' is not a subtype of 'MySwiftClass' var arrayOfFunctions: [() -> Int] = [myFunction] func…
edward
  • 295
  • 2
  • 8
1 2 3
8 9