Clojure swapping elements in an input sequence





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















How do you swap adjacent elements in an input sequence using clojure.
[10 15 20 25] ---> [15 10 25 20]
[:q :e :g :t :p] ---> [:e :q :t :g :p]



this is how i did it, but pretty sure there are better ways to do it!



(defn switch [s]
(loop [[a b & rest] s
result ]
(if (empty? rest)
(cond
;;empty sequence
(empty? s) result
;;odd sequence
(nil? b) (conj result a)
;;even sequence
:else (conj result b a)
)
(recur rest (conj result b a))))
)









share|improve this question




















  • 1





    I'm voting to close this question as off-topic because: It appears to be a simple homework problem.

    – Alan Thompson
    Nov 27 '18 at 2:41











  • Show the work you have done so far.

    – nakiya
    Nov 27 '18 at 2:42











  • @AlanThompson That is not the official Stack Overflow policy on homework questions. We care about questions that are well asked, and that is independent of whether they are homework. This one looks fine to me, as it clearly defines the desired behavior and shows an attempt. It's even a working attempt, asking for ways to do it better.

    – amalloy
    Nov 27 '18 at 19:36













  • The first line was the only thing present when first posted

    – Alan Thompson
    Nov 27 '18 at 20:52


















0















How do you swap adjacent elements in an input sequence using clojure.
[10 15 20 25] ---> [15 10 25 20]
[:q :e :g :t :p] ---> [:e :q :t :g :p]



this is how i did it, but pretty sure there are better ways to do it!



(defn switch [s]
(loop [[a b & rest] s
result ]
(if (empty? rest)
(cond
;;empty sequence
(empty? s) result
;;odd sequence
(nil? b) (conj result a)
;;even sequence
:else (conj result b a)
)
(recur rest (conj result b a))))
)









share|improve this question




















  • 1





    I'm voting to close this question as off-topic because: It appears to be a simple homework problem.

    – Alan Thompson
    Nov 27 '18 at 2:41











  • Show the work you have done so far.

    – nakiya
    Nov 27 '18 at 2:42











  • @AlanThompson That is not the official Stack Overflow policy on homework questions. We care about questions that are well asked, and that is independent of whether they are homework. This one looks fine to me, as it clearly defines the desired behavior and shows an attempt. It's even a working attempt, asking for ways to do it better.

    – amalloy
    Nov 27 '18 at 19:36













  • The first line was the only thing present when first posted

    – Alan Thompson
    Nov 27 '18 at 20:52














0












0








0








How do you swap adjacent elements in an input sequence using clojure.
[10 15 20 25] ---> [15 10 25 20]
[:q :e :g :t :p] ---> [:e :q :t :g :p]



this is how i did it, but pretty sure there are better ways to do it!



(defn switch [s]
(loop [[a b & rest] s
result ]
(if (empty? rest)
(cond
;;empty sequence
(empty? s) result
;;odd sequence
(nil? b) (conj result a)
;;even sequence
:else (conj result b a)
)
(recur rest (conj result b a))))
)









share|improve this question
















How do you swap adjacent elements in an input sequence using clojure.
[10 15 20 25] ---> [15 10 25 20]
[:q :e :g :t :p] ---> [:e :q :t :g :p]



this is how i did it, but pretty sure there are better ways to do it!



(defn switch [s]
(loop [[a b & rest] s
result ]
(if (empty? rest)
(cond
;;empty sequence
(empty? s) result
;;odd sequence
(nil? b) (conj result a)
;;even sequence
:else (conj result b a)
)
(recur rest (conj result b a))))
)






clojure sequence swap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 4:03







Ceylon Boy

















asked Nov 27 '18 at 2:04









Ceylon BoyCeylon Boy

62




62








  • 1





    I'm voting to close this question as off-topic because: It appears to be a simple homework problem.

    – Alan Thompson
    Nov 27 '18 at 2:41











  • Show the work you have done so far.

    – nakiya
    Nov 27 '18 at 2:42











  • @AlanThompson That is not the official Stack Overflow policy on homework questions. We care about questions that are well asked, and that is independent of whether they are homework. This one looks fine to me, as it clearly defines the desired behavior and shows an attempt. It's even a working attempt, asking for ways to do it better.

    – amalloy
    Nov 27 '18 at 19:36













  • The first line was the only thing present when first posted

    – Alan Thompson
    Nov 27 '18 at 20:52














  • 1





    I'm voting to close this question as off-topic because: It appears to be a simple homework problem.

    – Alan Thompson
    Nov 27 '18 at 2:41











  • Show the work you have done so far.

    – nakiya
    Nov 27 '18 at 2:42











  • @AlanThompson That is not the official Stack Overflow policy on homework questions. We care about questions that are well asked, and that is independent of whether they are homework. This one looks fine to me, as it clearly defines the desired behavior and shows an attempt. It's even a working attempt, asking for ways to do it better.

    – amalloy
    Nov 27 '18 at 19:36













  • The first line was the only thing present when first posted

    – Alan Thompson
    Nov 27 '18 at 20:52








1




1





I'm voting to close this question as off-topic because: It appears to be a simple homework problem.

– Alan Thompson
Nov 27 '18 at 2:41





I'm voting to close this question as off-topic because: It appears to be a simple homework problem.

– Alan Thompson
Nov 27 '18 at 2:41













Show the work you have done so far.

– nakiya
Nov 27 '18 at 2:42





Show the work you have done so far.

– nakiya
Nov 27 '18 at 2:42













@AlanThompson That is not the official Stack Overflow policy on homework questions. We care about questions that are well asked, and that is independent of whether they are homework. This one looks fine to me, as it clearly defines the desired behavior and shows an attempt. It's even a working attempt, asking for ways to do it better.

– amalloy
Nov 27 '18 at 19:36







@AlanThompson That is not the official Stack Overflow policy on homework questions. We care about questions that are well asked, and that is independent of whether they are homework. This one looks fine to me, as it clearly defines the desired behavior and shows an attempt. It's even a working attempt, asking for ways to do it better.

– amalloy
Nov 27 '18 at 19:36















The first line was the only thing present when first posted

– Alan Thompson
Nov 27 '18 at 20:52





The first line was the only thing present when first posted

– Alan Thompson
Nov 27 '18 at 20:52












1 Answer
1






active

oldest

votes


















2














(let [A [:q :e :g :t :p]]
(->> A
(partition-all 2)
(mapcat reverse)))





share|improve this answer





















  • 1





    (map reverse) + flatten == (mapcat reverse)

    – leetwinski
    Nov 27 '18 at 6:25











  • Indeed. It is more succinct your way.

    – akond
    Nov 27 '18 at 6:38






  • 1





    It's not just more succinct, but more correct. flatten is (almost) never the right answer. Imagine if A were instead defined as [[1 2] [3 4]]. We'd like the result to be [[3 4] [1 2]], but with the flatten version we would get [3 4 1 2].

    – amalloy
    Nov 27 '18 at 19: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%2f53491731%2fclojure-swapping-elements-in-an-input-sequence%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









2














(let [A [:q :e :g :t :p]]
(->> A
(partition-all 2)
(mapcat reverse)))





share|improve this answer





















  • 1





    (map reverse) + flatten == (mapcat reverse)

    – leetwinski
    Nov 27 '18 at 6:25











  • Indeed. It is more succinct your way.

    – akond
    Nov 27 '18 at 6:38






  • 1





    It's not just more succinct, but more correct. flatten is (almost) never the right answer. Imagine if A were instead defined as [[1 2] [3 4]]. We'd like the result to be [[3 4] [1 2]], but with the flatten version we would get [3 4 1 2].

    – amalloy
    Nov 27 '18 at 19:32
















2














(let [A [:q :e :g :t :p]]
(->> A
(partition-all 2)
(mapcat reverse)))





share|improve this answer





















  • 1





    (map reverse) + flatten == (mapcat reverse)

    – leetwinski
    Nov 27 '18 at 6:25











  • Indeed. It is more succinct your way.

    – akond
    Nov 27 '18 at 6:38






  • 1





    It's not just more succinct, but more correct. flatten is (almost) never the right answer. Imagine if A were instead defined as [[1 2] [3 4]]. We'd like the result to be [[3 4] [1 2]], but with the flatten version we would get [3 4 1 2].

    – amalloy
    Nov 27 '18 at 19:32














2












2








2







(let [A [:q :e :g :t :p]]
(->> A
(partition-all 2)
(mapcat reverse)))





share|improve this answer















(let [A [:q :e :g :t :p]]
(->> A
(partition-all 2)
(mapcat reverse)))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 6:46

























answered Nov 27 '18 at 4:24









akondakond

12.6k32849




12.6k32849








  • 1





    (map reverse) + flatten == (mapcat reverse)

    – leetwinski
    Nov 27 '18 at 6:25











  • Indeed. It is more succinct your way.

    – akond
    Nov 27 '18 at 6:38






  • 1





    It's not just more succinct, but more correct. flatten is (almost) never the right answer. Imagine if A were instead defined as [[1 2] [3 4]]. We'd like the result to be [[3 4] [1 2]], but with the flatten version we would get [3 4 1 2].

    – amalloy
    Nov 27 '18 at 19:32














  • 1





    (map reverse) + flatten == (mapcat reverse)

    – leetwinski
    Nov 27 '18 at 6:25











  • Indeed. It is more succinct your way.

    – akond
    Nov 27 '18 at 6:38






  • 1





    It's not just more succinct, but more correct. flatten is (almost) never the right answer. Imagine if A were instead defined as [[1 2] [3 4]]. We'd like the result to be [[3 4] [1 2]], but with the flatten version we would get [3 4 1 2].

    – amalloy
    Nov 27 '18 at 19:32








1




1





(map reverse) + flatten == (mapcat reverse)

– leetwinski
Nov 27 '18 at 6:25





(map reverse) + flatten == (mapcat reverse)

– leetwinski
Nov 27 '18 at 6:25













Indeed. It is more succinct your way.

– akond
Nov 27 '18 at 6:38





Indeed. It is more succinct your way.

– akond
Nov 27 '18 at 6:38




1




1





It's not just more succinct, but more correct. flatten is (almost) never the right answer. Imagine if A were instead defined as [[1 2] [3 4]]. We'd like the result to be [[3 4] [1 2]], but with the flatten version we would get [3 4 1 2].

– amalloy
Nov 27 '18 at 19:32





It's not just more succinct, but more correct. flatten is (almost) never the right answer. Imagine if A were instead defined as [[1 2] [3 4]]. We'd like the result to be [[3 4] [1 2]], but with the flatten version we would get [3 4 1 2].

– amalloy
Nov 27 '18 at 19: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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491731%2fclojure-swapping-elements-in-an-input-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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen