Multivariate function composition
up vote
1
down vote
favorite
Let's consider the following:
f: Int -> Int -> Int -> Int
f a b c = a + b + c
g: Int -> Int
g x = x * 2
now, I'd like to create a composed function like
f(g(a), g(b), g(c))
how to use << and >> operators to achieve that? Is it even possible to compose multivariadic functions?
elm
add a comment |
up vote
1
down vote
favorite
Let's consider the following:
f: Int -> Int -> Int -> Int
f a b c = a + b + c
g: Int -> Int
g x = x * 2
now, I'd like to create a composed function like
f(g(a), g(b), g(c))
how to use << and >> operators to achieve that? Is it even possible to compose multivariadic functions?
elm
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Let's consider the following:
f: Int -> Int -> Int -> Int
f a b c = a + b + c
g: Int -> Int
g x = x * 2
now, I'd like to create a composed function like
f(g(a), g(b), g(c))
how to use << and >> operators to achieve that? Is it even possible to compose multivariadic functions?
elm
Let's consider the following:
f: Int -> Int -> Int -> Int
f a b c = a + b + c
g: Int -> Int
g x = x * 2
now, I'd like to create a composed function like
f(g(a), g(b), g(c))
how to use << and >> operators to achieve that? Is it even possible to compose multivariadic functions?
elm
elm
edited Nov 25 at 20:16
asked Nov 22 at 8:54
Oskar Szura
1,30042240
1,30042240
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
Multivariadic does not really exist in Elm. Best I can suggest is
f : Int -> Int -> Int -> Int
f a b c =
[ a, b, c ] |> List.map ((<|) g) |> List.sum
g : Int -> Int
g x =
x * 2
Note that you had an error in the type signature for g
Corrected, good point - as every function is curried (as I understand). Thanks for the answer.
– Oskar Szura
Nov 25 at 20:17
add a comment |
up vote
4
down vote
how to use << and >> operators to achieve that? Is it even possible to compose multivariadic functions?
Yes, easily:
h = flip flip g << ((<<) (<<)) << ((>>) g) << f << g
h is a <function> : number -> number -> number -> number, which is the same as h a b c = f (g a) (g b) (g c).
I think we don't need to dig deep in the correctness, since it's obvious that the version with arguments is much more readable.
Note thatflipwas removed fromelm/corein 0.19, so I'd be very hesitant to accept this as a proper solution! /s
– glennsl
Nov 22 at 15:14
thanks for the note aboutflip. my point in the answer is that it's possible to compose the functions in order to achieve the desired result, but it makes no sense since the code becomes less readable than straight solution
– Igor Drozdov
Nov 22 at 15:29
Yeah, I got that, and I'm actually pretty impressed you managed (and bothered) to figure it out. Hence the (easily missed) "/s" to indicate sarcasm on my end ;)
– glennsl
Nov 22 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 at 20:16
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Multivariadic does not really exist in Elm. Best I can suggest is
f : Int -> Int -> Int -> Int
f a b c =
[ a, b, c ] |> List.map ((<|) g) |> List.sum
g : Int -> Int
g x =
x * 2
Note that you had an error in the type signature for g
Corrected, good point - as every function is curried (as I understand). Thanks for the answer.
– Oskar Szura
Nov 25 at 20:17
add a comment |
up vote
3
down vote
accepted
Multivariadic does not really exist in Elm. Best I can suggest is
f : Int -> Int -> Int -> Int
f a b c =
[ a, b, c ] |> List.map ((<|) g) |> List.sum
g : Int -> Int
g x =
x * 2
Note that you had an error in the type signature for g
Corrected, good point - as every function is curried (as I understand). Thanks for the answer.
– Oskar Szura
Nov 25 at 20:17
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Multivariadic does not really exist in Elm. Best I can suggest is
f : Int -> Int -> Int -> Int
f a b c =
[ a, b, c ] |> List.map ((<|) g) |> List.sum
g : Int -> Int
g x =
x * 2
Note that you had an error in the type signature for g
Multivariadic does not really exist in Elm. Best I can suggest is
f : Int -> Int -> Int -> Int
f a b c =
[ a, b, c ] |> List.map ((<|) g) |> List.sum
g : Int -> Int
g x =
x * 2
Note that you had an error in the type signature for g
answered Nov 22 at 11:55
Simon H
12.5k74375
12.5k74375
Corrected, good point - as every function is curried (as I understand). Thanks for the answer.
– Oskar Szura
Nov 25 at 20:17
add a comment |
Corrected, good point - as every function is curried (as I understand). Thanks for the answer.
– Oskar Szura
Nov 25 at 20:17
Corrected, good point - as every function is curried (as I understand). Thanks for the answer.
– Oskar Szura
Nov 25 at 20:17
Corrected, good point - as every function is curried (as I understand). Thanks for the answer.
– Oskar Szura
Nov 25 at 20:17
add a comment |
up vote
4
down vote
how to use << and >> operators to achieve that? Is it even possible to compose multivariadic functions?
Yes, easily:
h = flip flip g << ((<<) (<<)) << ((>>) g) << f << g
h is a <function> : number -> number -> number -> number, which is the same as h a b c = f (g a) (g b) (g c).
I think we don't need to dig deep in the correctness, since it's obvious that the version with arguments is much more readable.
Note thatflipwas removed fromelm/corein 0.19, so I'd be very hesitant to accept this as a proper solution! /s
– glennsl
Nov 22 at 15:14
thanks for the note aboutflip. my point in the answer is that it's possible to compose the functions in order to achieve the desired result, but it makes no sense since the code becomes less readable than straight solution
– Igor Drozdov
Nov 22 at 15:29
Yeah, I got that, and I'm actually pretty impressed you managed (and bothered) to figure it out. Hence the (easily missed) "/s" to indicate sarcasm on my end ;)
– glennsl
Nov 22 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 at 20:16
add a comment |
up vote
4
down vote
how to use << and >> operators to achieve that? Is it even possible to compose multivariadic functions?
Yes, easily:
h = flip flip g << ((<<) (<<)) << ((>>) g) << f << g
h is a <function> : number -> number -> number -> number, which is the same as h a b c = f (g a) (g b) (g c).
I think we don't need to dig deep in the correctness, since it's obvious that the version with arguments is much more readable.
Note thatflipwas removed fromelm/corein 0.19, so I'd be very hesitant to accept this as a proper solution! /s
– glennsl
Nov 22 at 15:14
thanks for the note aboutflip. my point in the answer is that it's possible to compose the functions in order to achieve the desired result, but it makes no sense since the code becomes less readable than straight solution
– Igor Drozdov
Nov 22 at 15:29
Yeah, I got that, and I'm actually pretty impressed you managed (and bothered) to figure it out. Hence the (easily missed) "/s" to indicate sarcasm on my end ;)
– glennsl
Nov 22 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 at 20:16
add a comment |
up vote
4
down vote
up vote
4
down vote
how to use << and >> operators to achieve that? Is it even possible to compose multivariadic functions?
Yes, easily:
h = flip flip g << ((<<) (<<)) << ((>>) g) << f << g
h is a <function> : number -> number -> number -> number, which is the same as h a b c = f (g a) (g b) (g c).
I think we don't need to dig deep in the correctness, since it's obvious that the version with arguments is much more readable.
how to use << and >> operators to achieve that? Is it even possible to compose multivariadic functions?
Yes, easily:
h = flip flip g << ((<<) (<<)) << ((>>) g) << f << g
h is a <function> : number -> number -> number -> number, which is the same as h a b c = f (g a) (g b) (g c).
I think we don't need to dig deep in the correctness, since it's obvious that the version with arguments is much more readable.
answered Nov 22 at 14:03
Igor Drozdov
11.9k52443
11.9k52443
Note thatflipwas removed fromelm/corein 0.19, so I'd be very hesitant to accept this as a proper solution! /s
– glennsl
Nov 22 at 15:14
thanks for the note aboutflip. my point in the answer is that it's possible to compose the functions in order to achieve the desired result, but it makes no sense since the code becomes less readable than straight solution
– Igor Drozdov
Nov 22 at 15:29
Yeah, I got that, and I'm actually pretty impressed you managed (and bothered) to figure it out. Hence the (easily missed) "/s" to indicate sarcasm on my end ;)
– glennsl
Nov 22 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 at 20:16
add a comment |
Note thatflipwas removed fromelm/corein 0.19, so I'd be very hesitant to accept this as a proper solution! /s
– glennsl
Nov 22 at 15:14
thanks for the note aboutflip. my point in the answer is that it's possible to compose the functions in order to achieve the desired result, but it makes no sense since the code becomes less readable than straight solution
– Igor Drozdov
Nov 22 at 15:29
Yeah, I got that, and I'm actually pretty impressed you managed (and bothered) to figure it out. Hence the (easily missed) "/s" to indicate sarcasm on my end ;)
– glennsl
Nov 22 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 at 20:16
Note that
flip was removed from elm/core in 0.19, so I'd be very hesitant to accept this as a proper solution! /s– glennsl
Nov 22 at 15:14
Note that
flip was removed from elm/core in 0.19, so I'd be very hesitant to accept this as a proper solution! /s– glennsl
Nov 22 at 15:14
thanks for the note about
flip. my point in the answer is that it's possible to compose the functions in order to achieve the desired result, but it makes no sense since the code becomes less readable than straight solution– Igor Drozdov
Nov 22 at 15:29
thanks for the note about
flip. my point in the answer is that it's possible to compose the functions in order to achieve the desired result, but it makes no sense since the code becomes less readable than straight solution– Igor Drozdov
Nov 22 at 15:29
Yeah, I got that, and I'm actually pretty impressed you managed (and bothered) to figure it out. Hence the (easily missed) "/s" to indicate sarcasm on my end ;)
– glennsl
Nov 22 at 15:42
Yeah, I got that, and I'm actually pretty impressed you managed (and bothered) to figure it out. Hence the (easily missed) "/s" to indicate sarcasm on my end ;)
– glennsl
Nov 22 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 at 20:16
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 at 20:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427082%2fmultivariate-function-composition%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown