428 questions
4
votes
1
answer
120
views
How does the point-free expression ((*) .) . (*) work in Haskell?
I'm learning about Haskell and came across this concise, but weird, definition for a function that multiplies three numbers:
volume :: Float -> Float -> Float -> Float
volume = ((*) .) . (*)
...
1
vote
1
answer
73
views
Function to add a command line to an existing function [closed]
I need to write a function to add a particular command/string of characters to an existing function.
Consider this function:
constraint = function(cov_matrix,max_variance){
function(x){
f = NULL
g =...
1
vote
0
answers
64
views
Composition of user-defined functions in SymPy
I am expecting achieve function composition. The output for laplace defined as composition is different to symmetric
This is my tried.
from sympy import Function, Symbol
from sympy.printing import ...
3
votes
2
answers
268
views
How do I compose or chain multiple function in Python
When writing a program, I often want to perform a series of steps on a piece of data (usually a list or string). For example I might want to call a function which returns a string, convert it to a ...
1
vote
1
answer
103
views
Why is the function composition with itself allowed in this question?
I was doing a past paper and was very confused by this question:
enter image description here
I tried to research about it, look at threads, watch youtube videos but I still do not understand it and ...
1
vote
1
answer
74
views
How can I use an array as a composition function argument?
I have trouble with building composition function, using an array as an argument.
Got this error
TypeError: arr.map is not a function
const testArray = ["CusTom", "Web", "...
1
vote
1
answer
165
views
Why one of my compose functions cannot infer types?
I have 2 composing functions: compose and composeR. They receive 2 functions and compose them together in a way that, output of one function is passed as an input to another function. This is their ...
2
votes
2
answers
257
views
How does the function composition (.) operator work with multiple compositions?
I know that the dot (.) operator is defined to take three arguments(two functions and a value of type "a"), and works by the application of applying the argument to the function on the right,...
0
votes
2
answers
272
views
Can I compose functions using std::bind*?
I'm playing with functional programming in C++20 and write something like that:
template <class OuterFn, class InnerFn, class... Args>
concept Composable =
std::invocable<OuterFn, std::...
7
votes
3
answers
1k
views
How to compose functions through purely using Python's standard library?
Python's standard library is vast, and my intuition tells that there must be a way in it to accomplish this, but I just can't figure it out. This is purely for curiosity and learning purposes:
I have ...
1
vote
1
answer
5k
views
Complex TypeScript type causes: Type instantiation is excessively deep and possibly infinite.ts(2589)
I have a nested TypeScript type created with function composition.
export const billingLoader = asyncPipe(
withOrganizationMembership,
withTFunction,
withPageTitle({ tKey: 'billing:billing' }),
...
0
votes
0
answers
76
views
Couldn't deduce template parameter problem with composing functions in c++
So I wanted to practice composing functions in C++, I have two functions f and g and composition is defined by x ↦ f(g(x)), I implemented the functions with specific data type (int) and it worked and ...
0
votes
1
answer
115
views
Making Java identify function composition more efficient
Java has java.util.function.Function.identity(T) which return a function equivalent to the lambda expression t -> t (and in fact that is the precise implementation on OpenJDK 17, which I'm looking ...
1
vote
1
answer
56
views
Optimizing the composition of two fuctions
While developing my programming skills, I often run into information along the lines of "Don't chain these functions together. Instead, use the built-in function that does both much faster."...
2
votes
2
answers
320
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), +...
0
votes
1
answer
99
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
53
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
324
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?
...
1
vote
1
answer
86
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....
0
votes
1
answer
55
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):
...
3
votes
3
answers
1k
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 ...
2
votes
1
answer
3k
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
1k
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,...
2
votes
1
answer
1k
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?
0
votes
1
answer
152
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 ...
-2
votes
3
answers
297
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 ...
3
votes
1
answer
537
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
207
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 ...
23
votes
3
answers
16k
views
Pinia store vs composable functions
I would like to figure out what are the advantages of using Pinia store instead of using just pure ts composable functions like
const userName = ref('')
export default function useUser() {
const ...
1
vote
1
answer
1k
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
192
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 ...
2
votes
6
answers
235
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
52
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 ...
2
votes
2
answers
1k
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 ...
2
votes
1
answer
355
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 ...
2
votes
4
answers
1k
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 `...
3
votes
1
answer
335
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
207
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 ((&&...
3
votes
2
answers
347
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 ...
2
votes
1
answer
224
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 ...
1
vote
1
answer
609
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 ...
1
vote
0
answers
239
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 ...
1
vote
1
answer
343
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) ...
16
votes
4
answers
894
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")...
3
votes
2
answers
256
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 ...
3
votes
1
answer
166
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));
...
1
vote
3
answers
104
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 ...
1
vote
1
answer
727
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 = { ...
0
votes
2
answers
165
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
291
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)...