Evaluate a list of computation expression values










2














What's a good way to evaluate a list of computation expression values into the corresponding list of values?



Let's say my computation expression type is M<a> then I'm wondering what is the best way to write the function:



mysequence : list<M<'a>> -> M<list<'a>>


I could write this out recursively:



let rec mysequence = function
| -> builder return
| (x::xs) -> builder let! y = x
let! ys = mysequence xs
return (y::ys)



Is there a more concise way?










share|improve this question


























    2














    What's a good way to evaluate a list of computation expression values into the corresponding list of values?



    Let's say my computation expression type is M<a> then I'm wondering what is the best way to write the function:



    mysequence : list<M<'a>> -> M<list<'a>>


    I could write this out recursively:



    let rec mysequence = function
    | -> builder return
    | (x::xs) -> builder let! y = x
    let! ys = mysequence xs
    return (y::ys)



    Is there a more concise way?










    share|improve this question
























      2












      2








      2







      What's a good way to evaluate a list of computation expression values into the corresponding list of values?



      Let's say my computation expression type is M<a> then I'm wondering what is the best way to write the function:



      mysequence : list<M<'a>> -> M<list<'a>>


      I could write this out recursively:



      let rec mysequence = function
      | -> builder return
      | (x::xs) -> builder let! y = x
      let! ys = mysequence xs
      return (y::ys)



      Is there a more concise way?










      share|improve this question













      What's a good way to evaluate a list of computation expression values into the corresponding list of values?



      Let's say my computation expression type is M<a> then I'm wondering what is the best way to write the function:



      mysequence : list<M<'a>> -> M<list<'a>>


      I could write this out recursively:



      let rec mysequence = function
      | -> builder return
      | (x::xs) -> builder let! y = x
      let! ys = mysequence xs
      return (y::ys)



      Is there a more concise way?







      f#






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 4:24









      Strecster

      352




      352






















          1 Answer
          1






          active

          oldest

          votes


















          4














          You could make this a bit shorter by using List.fold and even shorter if you introduced a couple of helper combinators such as a lift2 function of the following type:



          lift2 : ('a -> 'b -> 'c) -> M<'a> -> M<'b> -> M<'c>


          This is something you can easily define using the builder .. notation and it lets you turn functions that work on normal values into functions that work on wrapped values. You could then lift the list consing operation and use that with fold. Doing that will make the code shorter (if you ignore all the helpers one has to write), but it also makes it pretty ugly and obscure.



          If I was writing this, I'd go with something very close to your version. I prefer to keep the entire body in builder .. rather than having function and I'd also use the accumulator parameter, so I'd write:



          let rec mysequence acc input = builder -> return List.rev acc



          But aside from the accumulator and some minor syntactic differences, it's pretty much the same as yours!






          share|improve this answer




















          • Thankyou Tomas, very informative.
            – Strecster
            Nov 12 at 22:32










          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%2f53255953%2fevaluate-a-list-of-computation-expression-values%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You could make this a bit shorter by using List.fold and even shorter if you introduced a couple of helper combinators such as a lift2 function of the following type:



          lift2 : ('a -> 'b -> 'c) -> M<'a> -> M<'b> -> M<'c>


          This is something you can easily define using the builder .. notation and it lets you turn functions that work on normal values into functions that work on wrapped values. You could then lift the list consing operation and use that with fold. Doing that will make the code shorter (if you ignore all the helpers one has to write), but it also makes it pretty ugly and obscure.



          If I was writing this, I'd go with something very close to your version. I prefer to keep the entire body in builder .. rather than having function and I'd also use the accumulator parameter, so I'd write:



          let rec mysequence acc input = builder -> return List.rev acc



          But aside from the accumulator and some minor syntactic differences, it's pretty much the same as yours!






          share|improve this answer




















          • Thankyou Tomas, very informative.
            – Strecster
            Nov 12 at 22:32















          4














          You could make this a bit shorter by using List.fold and even shorter if you introduced a couple of helper combinators such as a lift2 function of the following type:



          lift2 : ('a -> 'b -> 'c) -> M<'a> -> M<'b> -> M<'c>


          This is something you can easily define using the builder .. notation and it lets you turn functions that work on normal values into functions that work on wrapped values. You could then lift the list consing operation and use that with fold. Doing that will make the code shorter (if you ignore all the helpers one has to write), but it also makes it pretty ugly and obscure.



          If I was writing this, I'd go with something very close to your version. I prefer to keep the entire body in builder .. rather than having function and I'd also use the accumulator parameter, so I'd write:



          let rec mysequence acc input = builder -> return List.rev acc



          But aside from the accumulator and some minor syntactic differences, it's pretty much the same as yours!






          share|improve this answer




















          • Thankyou Tomas, very informative.
            – Strecster
            Nov 12 at 22:32













          4












          4








          4






          You could make this a bit shorter by using List.fold and even shorter if you introduced a couple of helper combinators such as a lift2 function of the following type:



          lift2 : ('a -> 'b -> 'c) -> M<'a> -> M<'b> -> M<'c>


          This is something you can easily define using the builder .. notation and it lets you turn functions that work on normal values into functions that work on wrapped values. You could then lift the list consing operation and use that with fold. Doing that will make the code shorter (if you ignore all the helpers one has to write), but it also makes it pretty ugly and obscure.



          If I was writing this, I'd go with something very close to your version. I prefer to keep the entire body in builder .. rather than having function and I'd also use the accumulator parameter, so I'd write:



          let rec mysequence acc input = builder -> return List.rev acc



          But aside from the accumulator and some minor syntactic differences, it's pretty much the same as yours!






          share|improve this answer












          You could make this a bit shorter by using List.fold and even shorter if you introduced a couple of helper combinators such as a lift2 function of the following type:



          lift2 : ('a -> 'b -> 'c) -> M<'a> -> M<'b> -> M<'c>


          This is something you can easily define using the builder .. notation and it lets you turn functions that work on normal values into functions that work on wrapped values. You could then lift the list consing operation and use that with fold. Doing that will make the code shorter (if you ignore all the helpers one has to write), but it also makes it pretty ugly and obscure.



          If I was writing this, I'd go with something very close to your version. I prefer to keep the entire body in builder .. rather than having function and I'd also use the accumulator parameter, so I'd write:



          let rec mysequence acc input = builder -> return List.rev acc



          But aside from the accumulator and some minor syntactic differences, it's pretty much the same as yours!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 11:50









          Tomas Petricek

          198k13288461




          198k13288461











          • Thankyou Tomas, very informative.
            – Strecster
            Nov 12 at 22:32
















          • Thankyou Tomas, very informative.
            – Strecster
            Nov 12 at 22:32















          Thankyou Tomas, very informative.
          – Strecster
          Nov 12 at 22:32




          Thankyou Tomas, very informative.
          – Strecster
          Nov 12 at 22:32

















          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.





          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255953%2fevaluate-a-list-of-computation-expression-values%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







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3