Subtract n from every element of a Clojure sequence










1














I assume this is a very simple question, but I can't seem to find the answer online: how do you subtract n from every element of a Clojure sequence? E.g subtract 4 from each element of (6 9 11) and get (2 5 7)?



I assume this should be done with map, and I know that for the special case of subtracting 1 I can do (map dec (sequence)), but how do I do it for any other case?



For what it's worth, I figured out eventually that (map - (map (partial - n) (sequence)) technically does work, but it's clearly not how this is meant to be done.




For anyone who lands here from search and has a slightly different problem: if you want to subtract every element of a Clojure sequence from n, you can do that with (map (partial - n) (sequence))



Similarly, for multiplying every element of a Clojure sequence by n you can do (map (partial * n) (sequence))



For adding n to every element of a Clojure sequence (map (partial + n) (sequence))



I found that answer in these Clojure docs so I assume it's idiomatic, but obviously I'm not able to vouch for that myself.










share|improve this question


























    1














    I assume this is a very simple question, but I can't seem to find the answer online: how do you subtract n from every element of a Clojure sequence? E.g subtract 4 from each element of (6 9 11) and get (2 5 7)?



    I assume this should be done with map, and I know that for the special case of subtracting 1 I can do (map dec (sequence)), but how do I do it for any other case?



    For what it's worth, I figured out eventually that (map - (map (partial - n) (sequence)) technically does work, but it's clearly not how this is meant to be done.




    For anyone who lands here from search and has a slightly different problem: if you want to subtract every element of a Clojure sequence from n, you can do that with (map (partial - n) (sequence))



    Similarly, for multiplying every element of a Clojure sequence by n you can do (map (partial * n) (sequence))



    For adding n to every element of a Clojure sequence (map (partial + n) (sequence))



    I found that answer in these Clojure docs so I assume it's idiomatic, but obviously I'm not able to vouch for that myself.










    share|improve this question
























      1












      1








      1







      I assume this is a very simple question, but I can't seem to find the answer online: how do you subtract n from every element of a Clojure sequence? E.g subtract 4 from each element of (6 9 11) and get (2 5 7)?



      I assume this should be done with map, and I know that for the special case of subtracting 1 I can do (map dec (sequence)), but how do I do it for any other case?



      For what it's worth, I figured out eventually that (map - (map (partial - n) (sequence)) technically does work, but it's clearly not how this is meant to be done.




      For anyone who lands here from search and has a slightly different problem: if you want to subtract every element of a Clojure sequence from n, you can do that with (map (partial - n) (sequence))



      Similarly, for multiplying every element of a Clojure sequence by n you can do (map (partial * n) (sequence))



      For adding n to every element of a Clojure sequence (map (partial + n) (sequence))



      I found that answer in these Clojure docs so I assume it's idiomatic, but obviously I'm not able to vouch for that myself.










      share|improve this question













      I assume this is a very simple question, but I can't seem to find the answer online: how do you subtract n from every element of a Clojure sequence? E.g subtract 4 from each element of (6 9 11) and get (2 5 7)?



      I assume this should be done with map, and I know that for the special case of subtracting 1 I can do (map dec (sequence)), but how do I do it for any other case?



      For what it's worth, I figured out eventually that (map - (map (partial - n) (sequence)) technically does work, but it's clearly not how this is meant to be done.




      For anyone who lands here from search and has a slightly different problem: if you want to subtract every element of a Clojure sequence from n, you can do that with (map (partial - n) (sequence))



      Similarly, for multiplying every element of a Clojure sequence by n you can do (map (partial * n) (sequence))



      For adding n to every element of a Clojure sequence (map (partial + n) (sequence))



      I found that answer in these Clojure docs so I assume it's idiomatic, but obviously I'm not able to vouch for that myself.







      clojure






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 8:51









      marbiru

      204




      204






















          3 Answers
          3






          active

          oldest

          votes


















          2














          An anonymous function literal is convenient here:



          > (map #(- % 4) [6 9 11])
          (2 5 7)


          The #(- % 4) form is short-hand for the anonymous function in



          > (map (fn [x] (- x 4)) [6 9 11])
          (2 5 7)





          share|improve this answer




























            0














            If you're looking for a combinatory expression for the function you want, try (comp - (partial - 4)):



            => (map (comp - (partial - 4)) '(6 9 11))
            (2 5 7)


            Remember that + and - are just ordinary named Clojure functions. The function that you need is probably not worth naming, so you express it directly as #(- % 4) or (fn [n] (- n 4)), or as above.



            It is also worth remembering that map is lazy, so can deal with endless sequences:



            => (take 10 (map (comp - (partial - 4)) (range)))
            (-4 -3 -2 -1 0 1 2 3 4 5)





            share|improve this answer




























              -1














              This can be achieved through maps. Clojure maps takes a function and list as an argument.
              Synatax:




              (map (function) (seq))




              test=> (map (fn[arg] (- arg 4) ) '(7 8 9) )
              (3 4 5)
              test=> (map (fn[arg] (- arg 4) ) [7 8 9] )
              (3 4 5)


              Map can take function in shorthand notation too.




              (- % 4)




              . Here % is argument passed. Defaults to first argument. %1 to explicitly say first argument



              test=> (map #(- %1 4) [7 8 9])
              (3 4 5)
              test=> (map #(- % 4) [7 8 9])
              (3 4 5)





              share|improve this answer




















              • Maps, the data structures, have nothing to do with map the function. Map the function applies a function f on every element in a sequence. A map data structure is storing values at keys.
                – Shlomi
                Nov 13 at 4:23











              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%2f53258601%2fsubtract-n-from-every-element-of-a-clojure-sequence%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              An anonymous function literal is convenient here:



              > (map #(- % 4) [6 9 11])
              (2 5 7)


              The #(- % 4) form is short-hand for the anonymous function in



              > (map (fn [x] (- x 4)) [6 9 11])
              (2 5 7)





              share|improve this answer

























                2














                An anonymous function literal is convenient here:



                > (map #(- % 4) [6 9 11])
                (2 5 7)


                The #(- % 4) form is short-hand for the anonymous function in



                > (map (fn [x] (- x 4)) [6 9 11])
                (2 5 7)





                share|improve this answer























                  2












                  2








                  2






                  An anonymous function literal is convenient here:



                  > (map #(- % 4) [6 9 11])
                  (2 5 7)


                  The #(- % 4) form is short-hand for the anonymous function in



                  > (map (fn [x] (- x 4)) [6 9 11])
                  (2 5 7)





                  share|improve this answer












                  An anonymous function literal is convenient here:



                  > (map #(- % 4) [6 9 11])
                  (2 5 7)


                  The #(- % 4) form is short-hand for the anonymous function in



                  > (map (fn [x] (- x 4)) [6 9 11])
                  (2 5 7)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 9:37









                  jas

                  7,11621632




                  7,11621632























                      0














                      If you're looking for a combinatory expression for the function you want, try (comp - (partial - 4)):



                      => (map (comp - (partial - 4)) '(6 9 11))
                      (2 5 7)


                      Remember that + and - are just ordinary named Clojure functions. The function that you need is probably not worth naming, so you express it directly as #(- % 4) or (fn [n] (- n 4)), or as above.



                      It is also worth remembering that map is lazy, so can deal with endless sequences:



                      => (take 10 (map (comp - (partial - 4)) (range)))
                      (-4 -3 -2 -1 0 1 2 3 4 5)





                      share|improve this answer

























                        0














                        If you're looking for a combinatory expression for the function you want, try (comp - (partial - 4)):



                        => (map (comp - (partial - 4)) '(6 9 11))
                        (2 5 7)


                        Remember that + and - are just ordinary named Clojure functions. The function that you need is probably not worth naming, so you express it directly as #(- % 4) or (fn [n] (- n 4)), or as above.



                        It is also worth remembering that map is lazy, so can deal with endless sequences:



                        => (take 10 (map (comp - (partial - 4)) (range)))
                        (-4 -3 -2 -1 0 1 2 3 4 5)





                        share|improve this answer























                          0












                          0








                          0






                          If you're looking for a combinatory expression for the function you want, try (comp - (partial - 4)):



                          => (map (comp - (partial - 4)) '(6 9 11))
                          (2 5 7)


                          Remember that + and - are just ordinary named Clojure functions. The function that you need is probably not worth naming, so you express it directly as #(- % 4) or (fn [n] (- n 4)), or as above.



                          It is also worth remembering that map is lazy, so can deal with endless sequences:



                          => (take 10 (map (comp - (partial - 4)) (range)))
                          (-4 -3 -2 -1 0 1 2 3 4 5)





                          share|improve this answer












                          If you're looking for a combinatory expression for the function you want, try (comp - (partial - 4)):



                          => (map (comp - (partial - 4)) '(6 9 11))
                          (2 5 7)


                          Remember that + and - are just ordinary named Clojure functions. The function that you need is probably not worth naming, so you express it directly as #(- % 4) or (fn [n] (- n 4)), or as above.



                          It is also worth remembering that map is lazy, so can deal with endless sequences:



                          => (take 10 (map (comp - (partial - 4)) (range)))
                          (-4 -3 -2 -1 0 1 2 3 4 5)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 at 16:09









                          Thumbnail

                          10.9k11628




                          10.9k11628





















                              -1














                              This can be achieved through maps. Clojure maps takes a function and list as an argument.
                              Synatax:




                              (map (function) (seq))




                              test=> (map (fn[arg] (- arg 4) ) '(7 8 9) )
                              (3 4 5)
                              test=> (map (fn[arg] (- arg 4) ) [7 8 9] )
                              (3 4 5)


                              Map can take function in shorthand notation too.




                              (- % 4)




                              . Here % is argument passed. Defaults to first argument. %1 to explicitly say first argument



                              test=> (map #(- %1 4) [7 8 9])
                              (3 4 5)
                              test=> (map #(- % 4) [7 8 9])
                              (3 4 5)





                              share|improve this answer




















                              • Maps, the data structures, have nothing to do with map the function. Map the function applies a function f on every element in a sequence. A map data structure is storing values at keys.
                                – Shlomi
                                Nov 13 at 4:23
















                              -1














                              This can be achieved through maps. Clojure maps takes a function and list as an argument.
                              Synatax:




                              (map (function) (seq))




                              test=> (map (fn[arg] (- arg 4) ) '(7 8 9) )
                              (3 4 5)
                              test=> (map (fn[arg] (- arg 4) ) [7 8 9] )
                              (3 4 5)


                              Map can take function in shorthand notation too.




                              (- % 4)




                              . Here % is argument passed. Defaults to first argument. %1 to explicitly say first argument



                              test=> (map #(- %1 4) [7 8 9])
                              (3 4 5)
                              test=> (map #(- % 4) [7 8 9])
                              (3 4 5)





                              share|improve this answer




















                              • Maps, the data structures, have nothing to do with map the function. Map the function applies a function f on every element in a sequence. A map data structure is storing values at keys.
                                – Shlomi
                                Nov 13 at 4:23














                              -1












                              -1








                              -1






                              This can be achieved through maps. Clojure maps takes a function and list as an argument.
                              Synatax:




                              (map (function) (seq))




                              test=> (map (fn[arg] (- arg 4) ) '(7 8 9) )
                              (3 4 5)
                              test=> (map (fn[arg] (- arg 4) ) [7 8 9] )
                              (3 4 5)


                              Map can take function in shorthand notation too.




                              (- % 4)




                              . Here % is argument passed. Defaults to first argument. %1 to explicitly say first argument



                              test=> (map #(- %1 4) [7 8 9])
                              (3 4 5)
                              test=> (map #(- % 4) [7 8 9])
                              (3 4 5)





                              share|improve this answer












                              This can be achieved through maps. Clojure maps takes a function and list as an argument.
                              Synatax:




                              (map (function) (seq))




                              test=> (map (fn[arg] (- arg 4) ) '(7 8 9) )
                              (3 4 5)
                              test=> (map (fn[arg] (- arg 4) ) [7 8 9] )
                              (3 4 5)


                              Map can take function in shorthand notation too.




                              (- % 4)




                              . Here % is argument passed. Defaults to first argument. %1 to explicitly say first argument



                              test=> (map #(- %1 4) [7 8 9])
                              (3 4 5)
                              test=> (map #(- % 4) [7 8 9])
                              (3 4 5)






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 12 at 11:42









                              Jaiganesh

                              92




                              92











                              • Maps, the data structures, have nothing to do with map the function. Map the function applies a function f on every element in a sequence. A map data structure is storing values at keys.
                                – Shlomi
                                Nov 13 at 4:23

















                              • Maps, the data structures, have nothing to do with map the function. Map the function applies a function f on every element in a sequence. A map data structure is storing values at keys.
                                – Shlomi
                                Nov 13 at 4:23
















                              Maps, the data structures, have nothing to do with map the function. Map the function applies a function f on every element in a sequence. A map data structure is storing values at keys.
                              – Shlomi
                              Nov 13 at 4:23





                              Maps, the data structures, have nothing to do with map the function. Map the function applies a function f on every element in a sequence. A map data structure is storing values at keys.
                              – Shlomi
                              Nov 13 at 4:23


















                              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%2f53258601%2fsubtract-n-from-every-element-of-a-clojure-sequence%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







                              這個網誌中的熱門文章

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

                              Node.js Script on GitHub Pages or Amazon S3

                              Museum of Modern and Contemporary Art of Trento and Rovereto