Logo

Programming-Idioms

History of Idiom 41 > diff from v51 to v52

Edit summary for version 52 by jamesleonis:
New Clojure implementation by user [jamesleonis]

Version 51

2019-09-26, 18:43:23

Version 52

2019-09-26, 18:44:37

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration
Code
(let [s "hello"
      t (apply str (reverse s))]
  t)
Comments bubble
Strings are treated as sequential collections of characters. reverse returns the character list in reverse order, and apply takes this collection and feeds it as arguments into str to return a full reversed string.

As Clojure data structures are immutable, we can guarantee that s is unaltered.
Doc URL
https://clojuredocs.org/clojure.core/reverse
Origin
https://clojuredocs.org/clojure.core/reverse