Pascal's Triangle - Remove hack












2












$begingroup$


I had a thought last night on how I could easily write a Pascal's Triangle generator basically just using partition and map. I tried writing it this morning, and it got slightly more complicated than I originally thought it would, although it's still pretty nice.



My main concern is the wrap "hack" that I'm using. I found two similar, but different ways of writing it. I can either wrap the input to the iteration function in 0s, or I can wrap the result of the iteration function in 1s. Both give the same result, but the need for a wrapping helper pollutes an otherwise nice ->> chain. If anyone can think of how I can avoid a separate anonymous function here, and ideally avoid wrapping the input/output in 0s/1s, I'd appreciate it. I'll take any other critique as well, although there isn't much else going on here.



Usage:



(let [pre-t (pascals-triangle-pre) ; Wrapping pre-processing
post-t (pascals-triangle-post) ; Wrapping post-processing
n 5]
(println (take n pre-t))
(println (take n post-t)))

([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])




(ns minesweeper.irrelevant.pas-tri)

(defn pascals-triangle-post
(let [wrap #(vec (concat [1] % [1]))]
(iterate #(->> %
(partition 2 1)
(mapv (partial apply +'))
(wrap))
[1])))

(defn pascals-triangle-pre
(let [wrap #(vec (concat [0] % [0]))]
(iterate #(->> %
(wrap)
(partition 2 1)
(mapv (partial apply +')))
[1])))









share|improve this question









$endgroup$

















    2












    $begingroup$


    I had a thought last night on how I could easily write a Pascal's Triangle generator basically just using partition and map. I tried writing it this morning, and it got slightly more complicated than I originally thought it would, although it's still pretty nice.



    My main concern is the wrap "hack" that I'm using. I found two similar, but different ways of writing it. I can either wrap the input to the iteration function in 0s, or I can wrap the result of the iteration function in 1s. Both give the same result, but the need for a wrapping helper pollutes an otherwise nice ->> chain. If anyone can think of how I can avoid a separate anonymous function here, and ideally avoid wrapping the input/output in 0s/1s, I'd appreciate it. I'll take any other critique as well, although there isn't much else going on here.



    Usage:



    (let [pre-t (pascals-triangle-pre) ; Wrapping pre-processing
    post-t (pascals-triangle-post) ; Wrapping post-processing
    n 5]
    (println (take n pre-t))
    (println (take n post-t)))

    ([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
    ([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])




    (ns minesweeper.irrelevant.pas-tri)

    (defn pascals-triangle-post
    (let [wrap #(vec (concat [1] % [1]))]
    (iterate #(->> %
    (partition 2 1)
    (mapv (partial apply +'))
    (wrap))
    [1])))

    (defn pascals-triangle-pre
    (let [wrap #(vec (concat [0] % [0]))]
    (iterate #(->> %
    (wrap)
    (partition 2 1)
    (mapv (partial apply +')))
    [1])))









    share|improve this question









    $endgroup$















      2












      2








      2





      $begingroup$


      I had a thought last night on how I could easily write a Pascal's Triangle generator basically just using partition and map. I tried writing it this morning, and it got slightly more complicated than I originally thought it would, although it's still pretty nice.



      My main concern is the wrap "hack" that I'm using. I found two similar, but different ways of writing it. I can either wrap the input to the iteration function in 0s, or I can wrap the result of the iteration function in 1s. Both give the same result, but the need for a wrapping helper pollutes an otherwise nice ->> chain. If anyone can think of how I can avoid a separate anonymous function here, and ideally avoid wrapping the input/output in 0s/1s, I'd appreciate it. I'll take any other critique as well, although there isn't much else going on here.



      Usage:



      (let [pre-t (pascals-triangle-pre) ; Wrapping pre-processing
      post-t (pascals-triangle-post) ; Wrapping post-processing
      n 5]
      (println (take n pre-t))
      (println (take n post-t)))

      ([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
      ([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])




      (ns minesweeper.irrelevant.pas-tri)

      (defn pascals-triangle-post
      (let [wrap #(vec (concat [1] % [1]))]
      (iterate #(->> %
      (partition 2 1)
      (mapv (partial apply +'))
      (wrap))
      [1])))

      (defn pascals-triangle-pre
      (let [wrap #(vec (concat [0] % [0]))]
      (iterate #(->> %
      (wrap)
      (partition 2 1)
      (mapv (partial apply +')))
      [1])))









      share|improve this question









      $endgroup$




      I had a thought last night on how I could easily write a Pascal's Triangle generator basically just using partition and map. I tried writing it this morning, and it got slightly more complicated than I originally thought it would, although it's still pretty nice.



      My main concern is the wrap "hack" that I'm using. I found two similar, but different ways of writing it. I can either wrap the input to the iteration function in 0s, or I can wrap the result of the iteration function in 1s. Both give the same result, but the need for a wrapping helper pollutes an otherwise nice ->> chain. If anyone can think of how I can avoid a separate anonymous function here, and ideally avoid wrapping the input/output in 0s/1s, I'd appreciate it. I'll take any other critique as well, although there isn't much else going on here.



      Usage:



      (let [pre-t (pascals-triangle-pre) ; Wrapping pre-processing
      post-t (pascals-triangle-post) ; Wrapping post-processing
      n 5]
      (println (take n pre-t))
      (println (take n post-t)))

      ([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
      ([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])




      (ns minesweeper.irrelevant.pas-tri)

      (defn pascals-triangle-post
      (let [wrap #(vec (concat [1] % [1]))]
      (iterate #(->> %
      (partition 2 1)
      (mapv (partial apply +'))
      (wrap))
      [1])))

      (defn pascals-triangle-pre
      (let [wrap #(vec (concat [0] % [0]))]
      (iterate #(->> %
      (wrap)
      (partition 2 1)
      (mapv (partial apply +')))
      [1])))






      clojure






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 14 '18 at 16:14









      CarcigenicateCarcigenicate

      2,85511229




      2,85511229






















          1 Answer
          1






          active

          oldest

          votes


















          1












          $begingroup$

          There's a simpler way.



          To get the next line




          • take two copies of the line,

          • extend them respectively with 0 at the start and with 0 at the end, and

          • add the corresponding elements.


          In Clojure,



          (defn pascal 
          (iterate
          #(mapv + (cons 0 %) (conj % 0))
          [1]))

          => (take 5 (pascal))
          ([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])


          This avoids the partitioning.





          Not sure if it's what you're looking for, but showing off was irresistible.






          share|improve this answer









          $endgroup$













          • $begingroup$
            Cool, thanks. Can't complain half a year later.
            $endgroup$
            – Carcigenicate
            6 hours ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          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: "196"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f194374%2fpascals-triangle-remove-hack%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









          1












          $begingroup$

          There's a simpler way.



          To get the next line




          • take two copies of the line,

          • extend them respectively with 0 at the start and with 0 at the end, and

          • add the corresponding elements.


          In Clojure,



          (defn pascal 
          (iterate
          #(mapv + (cons 0 %) (conj % 0))
          [1]))

          => (take 5 (pascal))
          ([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])


          This avoids the partitioning.





          Not sure if it's what you're looking for, but showing off was irresistible.






          share|improve this answer









          $endgroup$













          • $begingroup$
            Cool, thanks. Can't complain half a year later.
            $endgroup$
            – Carcigenicate
            6 hours ago
















          1












          $begingroup$

          There's a simpler way.



          To get the next line




          • take two copies of the line,

          • extend them respectively with 0 at the start and with 0 at the end, and

          • add the corresponding elements.


          In Clojure,



          (defn pascal 
          (iterate
          #(mapv + (cons 0 %) (conj % 0))
          [1]))

          => (take 5 (pascal))
          ([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])


          This avoids the partitioning.





          Not sure if it's what you're looking for, but showing off was irresistible.






          share|improve this answer









          $endgroup$













          • $begingroup$
            Cool, thanks. Can't complain half a year later.
            $endgroup$
            – Carcigenicate
            6 hours ago














          1












          1








          1





          $begingroup$

          There's a simpler way.



          To get the next line




          • take two copies of the line,

          • extend them respectively with 0 at the start and with 0 at the end, and

          • add the corresponding elements.


          In Clojure,



          (defn pascal 
          (iterate
          #(mapv + (cons 0 %) (conj % 0))
          [1]))

          => (take 5 (pascal))
          ([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])


          This avoids the partitioning.





          Not sure if it's what you're looking for, but showing off was irresistible.






          share|improve this answer









          $endgroup$



          There's a simpler way.



          To get the next line




          • take two copies of the line,

          • extend them respectively with 0 at the start and with 0 at the end, and

          • add the corresponding elements.


          In Clojure,



          (defn pascal 
          (iterate
          #(mapv + (cons 0 %) (conj % 0))
          [1]))

          => (take 5 (pascal))
          ([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])


          This avoids the partitioning.





          Not sure if it's what you're looking for, but showing off was irresistible.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 6 hours ago









          ThumbnailThumbnail

          1,30157




          1,30157












          • $begingroup$
            Cool, thanks. Can't complain half a year later.
            $endgroup$
            – Carcigenicate
            6 hours ago


















          • $begingroup$
            Cool, thanks. Can't complain half a year later.
            $endgroup$
            – Carcigenicate
            6 hours ago
















          $begingroup$
          Cool, thanks. Can't complain half a year later.
          $endgroup$
          – Carcigenicate
          6 hours ago




          $begingroup$
          Cool, thanks. Can't complain half a year later.
          $endgroup$
          – Carcigenicate
          6 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


          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%2fcodereview.stackexchange.com%2fquestions%2f194374%2fpascals-triangle-remove-hack%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

          How to make a Squid Proxy server?

          Is this a new Fibonacci Identity?

          19世紀