Pascal's Triangle - Remove hack

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












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

























    up vote
    2
    down vote

    favorite












    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





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      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











      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










      share|improve this question




      share|improve this question









      asked May 14 at 16:14









      Carcigenicate

      2,31911128




      2,31911128

























          active

          oldest

          votes











          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          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



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          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













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?