Questions tagged [higher-order-functions]

Higher-order functions are functions which either take functions as arguments or return them as output (or both). They are a cornerstone of functional programming.

higher-order-functions
Filter by
Sorted by
Tagged with
0 votes
2 answers
659 views

Function as class attribute

I am writing a class where I would like to pass function as a class attribute and later use it, like that: class Nevronska_mreza: def __init__(self, st_vhodni, st_skriti, st_izhod, ...
david1201's user avatar
0 votes
1 answer
106 views

Is it correct to refer to a higher-order function as a "template"?

Minimal example: Say I have the higher-order function const my_fn = (a) => (b) => a + b that, when called like so: my_fn(42) returns the function (b) => 42 + b. Would it be correct to refer ...
Asker's user avatar
  • 1,525
2 votes
1 answer
615 views

Spreading props in React Higher Order Components

I am trying to go very in-depth to understand the purpose of spreading props in React HOC So taking the below example; const EnhanceComponent = BaseComponent => { return class EnhancedComponent ...
copenndthagen's user avatar
3 votes
1 answer
702 views

Modify and return closure

I'm trying to make a function to do the following: accept a closure f of the form fn(T) -> T return a closure of the form fn(T, bool) -> T, that conditionally performs f depending on the bool ...
Zoey Hewll's user avatar
  • 5,093
3 votes
2 answers
8k views

Usage of React HOC for Functional Components

I was referring to the below link (section : HOCs for Functional Components) https://rossbulat.medium.com/how-to-use-react-higher-order-components-c0be6821eb6c In the example, below is the code for ...
copenndthagen's user avatar
0 votes
1 answer
53 views

Reach Understanding Higher Order Component syntax

I am trying to understand the basic syntax of a React HOC. Consider below example; //WithLoading.js import React from 'react'; function WithLoading(Component) { return function WithLoadingComponent({...
copenndthagen's user avatar
4 votes
1 answer
149 views

Rust Lifetimes in Higher Order Functions

I have a map: HashMap<&str, Vec<&str>> and I'm trying to create a reverse lookup map HashMap<&str, &str> which refers from each of the element in the original vector ...
baron's user avatar
  • 53
1 vote
0 answers
507 views

Higher Order Function return null

I'm going to MenuFragment from MainActivty and calling RecyclerViewAdapter inside in MenuFragment class to set my data to show . then I want to set the callback in my Adapter using higher order ...
NimaAzhd's user avatar
  • 192
0 votes
1 answer
79 views

How can I create 1 or 2 higher order functions / closures as a alternative to a class with methods?

I am trying to create higher order functions in a nice way for a function named someFn, I got it to work with a class kind of const fn = (ctx, a, ...rest) => {}; class A { constructor(ctx, opts) ...
user avatar
0 votes
2 answers
463 views

Typescript: Typing a combinator for reducer-like functions

Consider the following types type Reducer<S, A> = Red<S, A, A> type Red<S, C, A> = (s: S) => (c: C) => A and the following function // The type intended for `reds` is // `{...
Gerrit Begher's user avatar
1 vote
2 answers
708 views

Swift array map without $0 (Higher-Order Functions )

I have a function that generates a number with some conditions func randomWithCondition() -> Int { ... return n } And later I will use this function to build an array with N length in another ...
Dan Dan's user avatar
  • 86
1 vote
1 answer
322 views

Couldn't match expected type [Int] Haskell

I am new to Haskell so I don't quite understand most of its errors. I have encountered an error trying to make a higher order function that uses foldl() to read a list and then multiply it by 2 (it ...
candpythonprogrammer's user avatar
0 votes
2 answers
1k views

Is it possible to pass a Groovy closure defined as a variable to a function for execution, with another variable as part of the passed call?

As the title says, if I define a closure that takes one argument can I pass it with an argument to be executed. For example: def print = { String NAME -> println "$NAME"} And then pass ...
max_sargent's user avatar
1 vote
1 answer
95 views

Where is the second parameter coming from in arrow function returning another arrow function in react with redux

I don't understand where the (val) is coming from in the returned arrow function. I get that the max/minLength is an arrow function taking in a argument set on the input field(3 and 25) and it's ...
JeromeCode's user avatar
4 votes
1 answer
4k views

Is it legit to use .map() method only for side-effects?

Is it legit (or good practice) do a loop with a higher order function - like Array.map() - to perform some side effects ? This is mostly a theoretical question since I notice that sometimes I found (...
Santiago's user avatar
  • 413
2 votes
1 answer
413 views

Solving nonlinear differential third order equation using Python

I would like to solve a nonlinear third order differential equation using Python. In my case it is : d^3f/dx^3 = (1-f)/(f^3) I wrote the following program, but I have an issue with the solver, so I ...
Wiss's user avatar
  • 145
4 votes
1 answer
128 views

Is there a higher order Prolog that wouldn't need a type system?

I suspect that λProlog needs a type system to make their higher order unification sound. Otherwise through self application some Russell type anomalies can appear. Are there alternative higher order ...
user avatar
0 votes
1 answer
114 views

Haskell: Use Map only for specific constructor

I have defined the following data type: data Item = Item { name :: String, subitems :: [Itemunit] } deriving (Show, Eq) data Itemunit = Itemunit Item | Dataunit Datau deriving (Show, Eq) data Datau =...
Richi's user avatar
  • 1
0 votes
2 answers
56 views

How to turn objects in an array with similars keys/values into an array within that array

I currently have an array of objects and some of them have duplicate key values, specifically the country key in this example. I'm trying to join all the objects in that array with the same country ...
Jim41Mavs's user avatar
  • 522
3 votes
2 answers
49 views

return object in an array with the most props

Say I have an array of objects like this: const arr = [ { a: 1, b: 2, c: 3, d: 4 }, { a: 1 }, { a: 1, b: 2, c: 3 }, { a: 1, b: 2 } ]; How can I return the object with the most properties/...
stickleBrick's user avatar
0 votes
2 answers
31 views

Return a new array of objects with same values grouped by qauntity/length

So i currently have an array like this: const allMeats = ['Bacon','Bacon','Bacon', 'Steak', 'Lettuce', 'Cabbage','Cabbage','Cabbage','Steak', 'Veal'] I would like to morph the array so that it ...
Jim41Mavs's user avatar
  • 522
0 votes
1 answer
136 views

How to extend a class with functions based on data and checked at compile time?

I have some classes with methods that are generated by data. These classes are available to a user, and I want type checking at class instantiation time. So if a user doesn't implement these classes ...
Anthony Tomasic's user avatar
0 votes
1 answer
183 views

How to access objects from map?

I am mapping over an object and I am returning its values in the console perfectly fine. const editCritical = assetIdObject.map(item => { if(item.asset_id === criticalObjectsId) return { ...
stack blunter's user avatar
2 votes
1 answer
1k views

Filtering categories with react hooks

I am learning React and sometimes I write too much lines of code and I would like to see how other people would resolve this in a better way. I have created the landing page of an academy, listing the ...
Guillermina Martinez's user avatar
0 votes
3 answers
568 views

How to compare arrays with higher order functions? JavaScript

I'm trying to compare an array with a property for an assignment and I can only use higher order functions (.map(), .filter(), .forEach(), .some() etc...). The array is "userWishlist" and ...
SLaTour's user avatar
  • 33
1 vote
1 answer
138 views

Filtering a list with indexes MIT-scheme

Is there a good way to filter a list using each element and its index in scheme? This is how I'm doing it now, but it seems overly complex (map cdr (filter (lambda (index-and-element) ...
Noah Evans's user avatar
0 votes
1 answer
276 views

How can I just use high order functions to rewrite these (use Dr.Racket)

This is my homework but we are only allowed to use filter, map, foldr, sort, build-list, and lambda instead of the explicit recursion How can I rewrite these use those high order functions above to ...
jiandaojun's user avatar
1 vote
2 answers
126 views

Use higher-order functions instead of explicit recursion

I want to write a function f that converts a binary list to an integer, like: f :: [Integer] -> Integer f [] = 0 f list = (last list) * 2^(length list -1) + f (init list) For example, in f [1,1,1,...
sambud_Ger's user avatar
0 votes
1 answer
290 views

Higher Order Functions in JavaScript and referring to the 'this' keyword within a class for an EventEmitter [duplicate]

I have been stuck on this issue for two days and no dice on other stack overflow posts. I could really use some help. My goal is to create an event emitter that allows three functions. Subscribe, ...
tuffant21's user avatar
  • 441
0 votes
1 answer
96 views

Declaring an OCaml user-defined type as a function

I am writing a Lisp interpreter in OCaml. This describes my type system: type atom = Bool of bool | Int of int | Float of float | String of string | ...
Caspian Ahlberg's user avatar
-1 votes
1 answer
294 views

How would I write a function in Dr. Racket which consumes a list of list of integers and produces a new list of list of integers?

How would I write a function in Dr. Racket which consumes a list of list of integers and produces a new list of list of integers with a transformation (sqr) applied to each element. Note: There is an ...
Diamoniner12345's user avatar
1 vote
3 answers
819 views

Sort names in ascending order in arrays in an array

For example, when an array of names is given, let names = [ [ ['firstName', 'Rachel'], ['age', 10], ['gender', 'female'], ], [ ['firstName', 'Sam'], ['lastName', 'Smith'], ...
Emily Scone's user avatar
2 votes
2 answers
763 views

redefining the 'all' function in haskell [duplicate]

How can I redefine the all function in haskell using foldr? I tried writing it like this and I'm not sure why it doesn't work. all :: (a -> Bool) -> [a] -> Bool all p (x:xs) = foldr (&&...
AndreeaC26's user avatar
1 vote
1 answer
65 views

More complicated goals in higher order Prolog

Let me give a concrete example to explain my question: Given a list of lists LoL and a list Ex, construct X that contains all elements (sublists) of LoL after subtracting Ex from them. Just to give ...
bp99's user avatar
  • 338
2 votes
3 answers
581 views

Return a sublist of a given list only using lambda and built-in higher-order list functions

I have a problem with writing a function (sublist list from to) which consumes two parameters, from and to, and produces the subsequence from the list that begins at the index from and ends before the ...
Emily W's user avatar
  • 21
0 votes
1 answer
277 views

JavaScript multidimensional array using high order function like filter and length

I'm new to javascript and trying to figure out how to do the question below using HOF with length property but was not able to. My friends & I end up with other solution using counter during our ...
sajn's user avatar
  • 3
1 vote
1 answer
357 views

How to combine two boolean lambda functions to make a new boolean lambda function?

Given two lambda functions like A = (lambda x: x % 2 == 0) B = (lambda x: x % 2 == 1) How can I create a new function, that returns True for each x where either A(x) or B(x) (or both) returns True? I ...
WinnyDaPoo's user avatar
1 vote
1 answer
254 views

Get frame info for outer scope, but only invest the time in inner scope

I have the following scenario, where I want to get frame info for the scope above my scope. def outer(): # This is really slow. frames = inspect.stack() def inner(): # I would ...
Uri's user avatar
  • 26.3k
0 votes
0 answers
81 views

How does the syntax of this bind function to compose two debugging functions work?

I am working through this tutorial on how I could have implemented Monads by working through the concepts. Exercise 1 is to write a bind function which can glue together two functions using this ...
mattematt's user avatar
  • 495
2 votes
1 answer
695 views

Reusing reducers logic

Problem I'd like to chain multiple high order reducers using ngrx, so that similar parts of code in my reducers have only one implementation. My application has number of pages that have quite similar ...
bartosz.baczek's user avatar
1 vote
3 answers
973 views

Mimic std::bind_front in c++17 for calling member functions

Is it somehow possible to easily mimic std::bind_front in C++17 ? (just for member function wrapping is fine) I took a look at implementation in c++20 aiming to copy but it seems its very much ...
darune's user avatar
  • 10.8k
1 vote
0 answers
481 views

Is it possible in a Higher Order Component to exclude some wrapped Component from render on HOC render

I am learning React. I have 4 React Components that are Consumer of the same HOC. I would like one of them to not Consume the render from the HOC. Is this possible and if so how to do it? Where to do ...
Kid's user avatar
  • 2,017
1 vote
0 answers
75 views

How to make a higher-order function to process other functions and make them useable by a recursive `while` function?

So I have these functions: /** * @description Recurses while a condition is true. Not stack-safe. * @param {Object} object_arguments - Contains the arguments for the function. * @param {function} ...
nicoty's user avatar
  • 166
1 vote
1 answer
91 views

Trying to Implement MapTree in terms of FoldTree

Trying to implement a mapTree in terms of foldTree. data Tree a = Leaf | Node (Tree a) a (Tree a) deriving Show foldTree is shown here foldTree :: b -> (b -> a -> b -> b) -> Tree a -&...
Tony's user avatar
  • 27
0 votes
1 answer
947 views

How to check if the value of a specific key is the same in all objects within a grouped object?

I have a group of objects in which I would like to check whether a specific key always has the same value in every single object in this group. Here's is my simplified example: The console.log of the ...
Codehan25's user avatar
  • 2,874
0 votes
2 answers
212 views

How to use pattern matching with function values in Scala

I have several functions with same input and output types initialized in an object object Utils { def f1(value: Int): Double = ??? def f2(value: Int): Double = ??? def f3(value: Int): Double = ??...
Snorlite's user avatar
  • 327
4 votes
1 answer
494 views

Eta-conversion in Javascript

Eta-conversion is considering the function (x) => f(x) to be the same as the function f. While refactoring code at work, I sought to simplify some higher order function call using this equivalence. ...
Marc-André Brochu's user avatar
1 vote
1 answer
30 views

Infer the type of property on interface parametrically given a specifc key of that interface

Given an heterogeneous interface, I would like to generate a type-safe function that operates on a given property of that interface. For example, something like this: interface State { a: boolean; ...
rdcdr's user avatar
  • 21
0 votes
2 answers
332 views

What is the best way to break execution / return to caller of anonymous functions (Kotlin)?

I would like to use the return statement as a way to break the execution of a function like: fun divide(i1: Int, i2: Int) { if (i2 == 0) return print(i1 / i2) } The function I am using looks ...
KITRIK's user avatar
  • 37
0 votes
1 answer
80 views

Higher order functions in scala

I have these two functions: def replaceValue(value: String): String = if (value.startsWith(":")) value.replaceFirst(":", "/") else value def getData(value: String)...
Avenger's user avatar
  • 837

1
6 7
8
9 10
33