Questions tagged [function-composition]

Applying one function to the result of another is known as function composition: `(f.g)(x) = f(g(x))`

Filter by
Sorted by
Tagged with
2 votes
2 answers
61 views

How to compose functions that modify a struct in Rust

Take a super-simple struct Foo: #[derive(Debug)] struct Foo { a: i32 } and a compose macro I got here: macro_rules! compose { ( $last:expr ) => { $last }; ( $head:expr, $($tail:expr), +...
  • 19.1k
0 votes
1 answer
30 views

Function composition vs decorators, in a memoized recursive Fibonacci sequence generator

I'm practicing memoization to improve recursive functions, so I wrote this memoized Fibonacci generator: memo = {} def memo_fibo(n): if n not in memo: if n < 2: memo[n] = n ...
0 votes
1 answer
25 views

How do composition and currying interact?

While doing exercises of chapter 3 of the Purescript by Example book was puzzled by this: The exercise is to write an isInBook :: String -> String -> AddressBook -> Boolean findEntryByName :: ...
3 votes
1 answer
105 views

Haskell version of APL trains

TLDR: Is there a Haskell library that offers function definitions (preferably with concise notation or naming) for handling common patterns of multi-argument function composition such as those in APL? ...
  • 521
1 vote
1 answer
59 views

Debug Haskell composition chain without converting to point-full representation

I have learned that point-free style is preferred in the Haskell community, and I often write expressions like this: naive = (slugifyUnicode . T.take maxFilenameSize . T.pack . stripHtmlTags . T....
  • 194
0 votes
1 answer
30 views

Complex function composition in 1 line | Python 3.7

function composition of single argument functions is a simple 1 liner. how would I (if it's possible) write the following composition in 1 line? def fn(x: int) -> str: def inner(a, b): ...
  • 11
0 votes
0 answers
13 views

When nesting function calls like f(g(x)), which one is called first? [duplicate]

If I want to call multiple functions in JavaScript, which function will run first? Like in this example: sum(range(1, 10)); Does the sum function run first or does the range function run first? I am ...
3 votes
3 answers
605 views

function composition for multiple arguments and nested functions

I have a pure function that takes 18 arguments process them and returns an answer. Inside this function I call many other pure functions and those functions call other pure functions within them as ...
  • 160
0 votes
1 answer
37 views

Understanding pipe function in JavaScript (functional programming)

I'm working through question 11 on bfe.dev's JavaScript coding exercises. The question asks to chain multiple basic math functions together via composition. Here's some sample code that passes the ...
0 votes
1 answer
83 views

Composing a Java BiFunction and Consumer

Similar to this question Composing a Java Function and Consumer. What is the best way to functionally compose a java BiFunction and a Consumer? For example given some BiFunction<String, String,...
1 vote
1 answer
120 views

How to do function composition in Lean 4?

If I have two functions f and g, in Haskell I can compose them by writing g.f. How do I do the same thing in Lean 4?
  • 3,017
0 votes
1 answer
67 views

Easy-to-read way of function composition in Javascript [closed]

I'm a huge fan of functional programming. I strive to use point free notation when I can. However, I often don't understand when point free notation is appropriate or when it is overkill. I typical ...
  • 725
-2 votes
3 answers
77 views

Function composition with just lambdas in python

Is there a way to do the following in just one line with lambdas in python? squared = lambda x : x**2 cubed = lambda x : x**3 squareCubed = lambda x : squared(cubed(x)) without using any helper ...
  • 63
2 votes
1 answer
171 views

Function composition chain with a pure macro in Rust

I've read How to compose functions in Rust? Function composition chain in Rust I've learned implementing a Function composition chain in Rust is rather difficult, and people use Macro with some ...
0 votes
1 answer
65 views

Function composition type inference in Dart

In dart, with generics, the identity function works as below: a to a: var id = <A>(A a) => a; a to the list type [a]: var list = <A>(A a) => [a]; Now, I have function composition ...
1 vote
1 answer
405 views

Function composition chain in Rust

I want to implement function composition in an object method chain in Rust. Before that, I confirmed that implementation of an object method chain for "pipe" of function application is ...
1 vote
0 answers
131 views

Why does parameter type inference break in compose function?

I encountered this in my own library, so I thought I would check against the Redux compose function that does the same thing. I found the same issue. Because redux is considerably more popular than my ...
  • 116k
2 votes
6 answers
153 views

Is there a way to create an array of combined functions compile-time in c++?

I'm working on an NES emulator in c++ and figured that the most efficient way to run opcodes would be to call a function pointer in an array of functions that do exactly what the opcode does. The ...
1 vote
1 answer
37 views

Simple function-composition class does not work (missing 2 required positional arguments)

I am digging into math currently and tried to build a simple function composition maker https://en.wikipedia.org/wiki/Function_composition I want to tell the program: Look at two tuples and compare ...
  • 31
2 votes
2 answers
387 views

Using Go generics to implement a chain of processors

I am trying to implement a kind of simple processing pipeline in Go, where each processor has a determined input and output type, and a list of successor processors that take current processor output ...
  • 31
2 votes
1 answer
132 views

Why does Idris 2 fail to resolve constraints with function composition in this trivial example?

I have encountered a problem with some code I am trying to write in Idris 2. I would like to resolve this issue, but more importantly, I wish to understand it more deeply and develop some skills in ...
  • 153
1 vote
4 answers
344 views

How to implement function composition in JavaScript?

Below are three functions that need to be composed and give us the output 30: const add = (a) => a + 10; const mul = (a) => a * 10; const divide = (a) => a / 5; // How to implement `...
1 vote
1 answer
102 views

What would a compose method in the BiFunction interface look like?

The Function interface has the compose() and andThen() methods while the BiFunction interface only has the andThen() method. My question is simply how could the corresponding method be implemented? I'...
1 vote
2 answers
125 views

About Function composition order of execution in Haskell

I have some trouble understanding this example: data Row = R [Int] deriving Show data Matrix = M [Row] deriving Show check :: Matrix -> Int -> Bool check (M matrix) n = foldr ((&&...
  • 447
3 votes
2 answers
247 views

Decorators for functions in TypeScript

I'm trying to make a statically type-checked "decorators" for functions. Basically, it is a helper for function composition from the right to remove the nesting. The issue is that while ...
  • 375
2 votes
1 answer
172 views

Exercise Multicomposition , functions in list

This is an exercise: -- Ex 12: recall the binary function composition operation -- (f . g) x = f (g x). In this exercise, your task is to define a function -- that takes any number of functions given ...
  • 113
-1 votes
1 answer
196 views

Should I return the object itself just for function chain

I have been wondering whether should I return the object itself from a setter or procedure (returning void originally), so that all the function can chain together. We've all been taught to return ...
user avatar
1 vote
0 answers
158 views

Is there any workaround for ts(2345) error for TypeScript lacks higher kinded types?

Related to my previous question: Type inference of Function composition method (chain) in TypeScript I try to implement a function that is to extend a specified function to have a chainable method for ...
  • 572
1 vote
1 answer
243 views

Type inference of Function composition method (chain) in TypeScript

I try to implement a function that is to extend a specified function to have a chainable method for function composition; as below; Also see: TypeScript playground { const F = <T, U>(f: (a: T) ...
  • 572
16 votes
4 answers
687 views

Why does compose() need an explicit cast when andThen() does not?

I'm studying functional composition and have an example: Function<String, String> test = (s) -> s.concat("foo"); String str = test.andThen(String::toUpperCase).apply("bar")...
  • 165
3 votes
2 answers
115 views

Using composition to get ceiling of a division

I'm learning haskell, and I'm trying to rewrite a function using composition only Here's the function I'm trying to refactor: ceilingDiv a b = ceiling (a / b) So far I managed to make it work using ...
  • 33
3 votes
1 answer
108 views

Ramda apply an argument to both functions and compose them point free

I have two curried functions f and g: f: a -> b -> c g: a -> c -> d I want to create h: h: a -> b -> d Currently I'm composing them via pipe: const h = a => pipe(f(a), g(a)); ...
  • 12.4k
1 vote
3 answers
77 views

Point free composition of multivariate functions

Say, we want to introduce the notion of sum of functions of different arguments (let's call it <+>), which behaves like the that: (f1 <+> f2)(x1, x2) == f1(x1) + f2(x2). While this can be ...
  • 396
1 vote
1 answer
443 views

Kotlin - function composition - explanation

I am following some video presentation on Kotlin's arrow library about functional programming. I have come to this example of function composition: val greaterThanThree = { it > 3 } val even = { ...
  • 1,904
0 votes
2 answers
109 views

Compose functions using generators

I am trying to make composition using generators. function A(x){return 1+x} function B(y){return 2*y} let val=tryCompose([A,B](1); //output should be : A(B(1)) How should the generator function ...
0 votes
3 answers
202 views

Nested function pointers or function composition

I am working on C implementations of calculus operations (such as derivatives, integrals, etc...). As an example, here's the template definition of my derivative function: double derivative(double (*f)...
1 vote
1 answer
89 views

Can container and ranges algorithm be two operands of function composition operator in c++20?

As we know that in c++20 pipe operator | stands for function composition but here in below code we are passing one container and one algorithm so how is it working and how we are able to pass ...
  • 1,811
3 votes
3 answers
426 views

How to invert the predicate here?

I have the following filter procedure: ; (2) filter (define (filter test sequence) ; return a list of the elements that pass the predicate test (let ((elem (if (null? sequence) nil (car sequence)))...
  • 107k
2 votes
1 answer
123 views

Reasoning about types in Haskell

Chapter 16 of "Haskell Programming from First Principles" on page 995 has an exercise to manually work out how (fmap . fmap) typechecks. It suggests substituting the type of each fmap for ...
7 votes
1 answer
354 views

(How) Can you curry compose monadic functions?

I have the following functions: f: a -> m[b] g: (b,c) -> m[d] h: (a,c) -> m[d] How can h be expressed as a composition of f and g? Using do/for notation we can implement h easily like so: h: ...
  • 11.1k
1 vote
1 answer
96 views

How to compose monadic functions/methods of type Function1 via 'andThen' in Scala?

I'm trying to compose functions of types f: a -> Either[_,b] with each other but nothing seems to work. I tried extending Function1 with implicits like below but keep getting compilation errors: ...
  • 11.1k
-1 votes
1 answer
4k views

TypeError: Object is not a function or its return value is not iterable, when i use useState (function component)

const [text, setText] = useSate('') const [day, setDay] = useSate('') const [remainder, setRemainder] = useSate(false) <div className="mb-3"> <label htmlFor="task&...
  • 83
1 vote
1 answer
51 views

What's the significance of the "this" keyword in this nested function

This function computes f(g(...arg)) in JavaScript. function compose(f,g){ return function(...arg){ return f.call(this, g.apply(this, args)}; } } I know that the first argument to the ...
  • 902
2 votes
3 answers
293 views

How to chain binary functions in Haskell?

I want to generate N numbers using System.Random.next function. I implemented a function which takes a StdGen and a list of numbers and returns new generator and updated list of numbers: import System....
  • 1,883
-1 votes
1 answer
94 views

Refactor `f(g(x))(x)` so that I can compose this nicely

I have a couple of functions that currently are written (in abstract form) like this... const someFunction = x => f(g(x))(x); i.e. pass x into g to get the output. Then pass that output into f to ...
  • 75.6k
0 votes
1 answer
81 views

Why does this composition function apply functions in the wrong order when passed as a reference?

Problem The problem is that a composition function sometimes applies its functions in the wrong order but only when it is passed as a reference (e.g., mapValues(compose(appendB, appendA))(...
  • 431
1 vote
3 answers
359 views

Better way to compose nested functions

Suppose I have two functions with the following types. f :: (a, b) -> c g :: (a, b, c) -> d I can compose them as follows. function h(a, b) { const c = f(a, b); const d = g(a, b, c); ...
  • 169
2 votes
1 answer
89 views

How a value is being retrieved with partial function

I have the following codes: const PENDING = 'PENDING'; const FULFILLED = 'FULFILLED'; const REJECTED = 'REJECTED'; class simplePromise { constructor(callback) { this.state = PENDING; ...
1 vote
1 answer
34 views

Is there a google sheets function to count with using arrays of sums as criteria?

I have been struggling for a few days with this problem. Anyone kind enough to show some interested will be highly appreciated. I have the table shown below. Suppose columns represent months. I would ...
  • 11
2 votes
2 answers
107 views

Can `A => List[B]` be transformed into a `List[A => B]`?

I am trying to find an implementation for this Scala function signature: def explode[A, B](f: A => List[B]): List[A => B] The opposite direction is possible: def nest[A, B](fs: List[A => B]):...

1
2 3 4 5
9