Multivariate function composition












1















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?










share|improve this question





























    1















    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?










    share|improve this question



























      1












      1








      1








      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 20:16







      Oskar Szura

















      asked Nov 22 '18 at 8:54









      Oskar SzuraOskar Szura

      1,31552240




      1,31552240
























          2 Answers
          2






          active

          oldest

          votes


















          3














          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






          share|improve this answer
























          • Corrected, good point - as every function is curried (as I understand). Thanks for the answer.

            – Oskar Szura
            Nov 25 '18 at 20:17



















          4















          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.






          share|improve this answer
























          • 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













          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          3














          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






          share|improve this answer
























          • Corrected, good point - as every function is curried (as I understand). Thanks for the answer.

            – Oskar Szura
            Nov 25 '18 at 20:17
















          3














          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






          share|improve this answer
























          • Corrected, good point - as every function is curried (as I understand). Thanks for the answer.

            – Oskar Szura
            Nov 25 '18 at 20:17














          3












          3








          3







          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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



















          • 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













          4















          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.






          share|improve this answer
























          • 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













          • 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
















          4















          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.






          share|improve this answer
























          • 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













          • 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














          4












          4








          4








          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.






          share|improve this answer














          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 14:03









          Igor DrozdovIgor Drozdov

          12k52444




          12k52444













          • 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













          • 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











          • 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













          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy