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
0
votes
1 answer

In Python, how do I change which class's attribute a function will look for based on a parameter?

I'd like to use the same function twice but change which class attribute the function will use. It would look similiar to below: class Player: def __init__(self): self.primary_color = 'blue' self.secondary_color = 'pink' def…
0
votes
1 answer

type mismatch when create a kotlin infix notation to nest a function to another

I am trying to create a infix notation as a extension function of (Int) -> Int function which is used to nest a function to another. For example: class Entry { companion object { private fun f(x: Int) = x * 2 private fun g(x:…
0
votes
2 answers

what is the different between Function first class and void function in Dart

in order to optimize my code, I try to use Function first class in the onTap property of the GestureDetector widget, like in the code below: import 'package:flutter/material.dart'; class ReusableCard extends StatelessWidget { final Color…
Taha Kerrouzi
  • 177
  • 2
  • 15
0
votes
1 answer

How to call a function stored as variable with arguments - Python 3

def some_function(): print("This will always be the same") def some_other_function(text): print(text) some_variable = "hello" list_of_elements = [ "element_one": { "name": "static_print", "function": some_function …
0
votes
3 answers

Difficulty Iterating Through Function Call - First Class Functions

The code I have provided executes properly, however as you will see it offers refreshments to each guest repeatedly before moving on to the next guest. I'm scratching my head as to how I can alter my code, in an efficient way, so that each customer…
0
votes
1 answer

python: assigning first class function to dictionary item

I have an existing dictionary and I want to add new element as a first-class function generated from a list. Is there a more pythonic way of doing this? agg_dict = {} for x in attribute_list: agg_dict[x] = lambda x: ','.join(x) I've tried using…
little_stone_05
  • 145
  • 1
  • 9
0
votes
2 answers

Is there any difference between First Class Functions and Callback Functions in Javascript?

Are there any differences between first class functions and callback functions in javascript? I thought that first class functions were functions that were treated as regular variables and can be passed as arguments. Aren't callback functions the…
0
votes
1 answer

Does fortran have first-class function?

In Fortran, we can pass a function name as an argument to a subroutine/function. Does this mean that fortran has first-class functions? The answer seems to be no, since it seems that fortran does not support returning functions as the values from…
Youjun Hu
  • 991
  • 6
  • 18
0
votes
0 answers

Why is a list of anonymous closures not identical to a list of evaluations of anonymous enclosing functions?

Note: This concept is touched upon in Lambda function in list comprehensions, but not to a sufficient depth for my understanding. We start with an example (in Python) of a closure: def enclosing_function(free_variable): def nested_function(x): …
Jon Middleton
  • 363
  • 1
  • 3
  • 8
0
votes
1 answer

What alternative when I get: "Partial application of 'mutating' method is not allowed"

When I do this: struct Test { var value: Int = 0 mutating func increment(amount: Int) { value += amount } mutating func decrement(amount: Int) { value -= amount } func apply(technique: (Int) -> ()) { print("Before:\(value)") …
Cortado-J
  • 2,035
  • 2
  • 20
  • 32
0
votes
3 answers

Executing higher-order functions

I'm learning the concepts of first class functions and closures in Python and I was curious to know: Given a higher-order function: def html_tag(tag): def wrap_text(text): print("<{0}>{1}".format(tag, text)) return…
0
votes
4 answers

How to implement currying in javascript?

Question as below: create a sum function, and the requirement: sum(1,2).result === 3 sum(1,2)(3).result == 6 sum(1,2)(3,4).result == 10 sum(1,2)(3,4)(5).result == 15 This is a question about currying in JS. I have implemented the most functions…
Wayne Ho
  • 45
  • 1
  • 8
0
votes
3 answers

trying to do math, int object is not callable

def square(x): int(x) return 2(x * x) def my_map(func, arg_list): result = [] for i in arg_list: result.append(func(i)) return result squares = my_map(square, [1,2,3,4,5]) print(squares) i'm trying to pass a number to…
0
votes
1 answer

Type mismatch on basic "apply twice" hello world in Haskell

The minimal code: twice :: (a -> a) -> a -> a twice f = f . f main = do return twice ++ "H" The errors generated: stack runhaskell "c:\Users\FruitfulApproach\Desktop\Haskell\test.hs" C:\Users\FruitfulApproach\Desktop\Haskell\test.hs:5:1:…
0
votes
4 answers

JS :: First Class Functions :: Parameter Value Confusion

I do not understand how the parameter of the first class function, "number", has the value it does. I have been mulling over this problem for a couple of days and I am not making any progress in my thinking. There is no error code as this is…
sector7g
  • 5
  • 3
1 2 3
8 9