Multivariate function composition
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 |
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 |
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 '18 at 20:16
Oskar Szura
asked Nov 22 '18 at 8:54
Oskar SzuraOskar Szura
1,31552240
1,31552240
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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 '18 at 20:17
add a comment |
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 thatflip
was removed fromelm/core
in 0.19, so I'd be very hesitant to accept this as a proper solution! /s
– glennsl
Nov 22 '18 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 '18 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 '18 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 '18 at 20:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 20:17
add a comment |
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 '18 at 20:17
add a comment |
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 '18 at 11:55
Simon HSimon H
12.8k74477
12.8k74477
Corrected, good point - as every function is curried (as I understand). Thanks for the answer.
– Oskar Szura
Nov 25 '18 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 '18 at 20:17
Corrected, good point - as every function is curried (as I understand). Thanks for the answer.
– Oskar Szura
Nov 25 '18 at 20:17
Corrected, good point - as every function is curried (as I understand). Thanks for the answer.
– Oskar Szura
Nov 25 '18 at 20:17
add a comment |
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 thatflip
was removed fromelm/core
in 0.19, so I'd be very hesitant to accept this as a proper solution! /s
– glennsl
Nov 22 '18 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 '18 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 '18 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 '18 at 20:16
add a comment |
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 thatflip
was removed fromelm/core
in 0.19, so I'd be very hesitant to accept this as a proper solution! /s
– glennsl
Nov 22 '18 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 '18 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 '18 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 '18 at 20:16
add a comment |
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 '18 at 14:03
Igor DrozdovIgor Drozdov
12k52444
12k52444
Note thatflip
was removed fromelm/core
in 0.19, so I'd be very hesitant to accept this as a proper solution! /s
– glennsl
Nov 22 '18 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 '18 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 '18 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 '18 at 20:16
add a comment |
Note thatflip
was removed fromelm/core
in 0.19, so I'd be very hesitant to accept this as a proper solution! /s
– glennsl
Nov 22 '18 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 '18 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 '18 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 15:42
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 '18 at 20:16
So practically it's possible but there's no point of doing so... Thanks for the answer :).
– Oskar Szura
Nov 25 '18 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.
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