Guest User

Untitled

a guest
Oct 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. In Clojure.                                                                                                                                                                                                                                  
  2.                                                                                                                                                                                                                                              
  3. [sourcecode lang="css"]                                                                                                                                                                                                                      
  4.                                                                                                                                                                                                                                              
  5. ;; helpers                                                                                                                                                                                                                                  
  6.                                                                                                                                                                                                                                              
  7. (defn build-string-step [search-criteria]                                                                                                                                                                                                    
  8.   (fn [built-so-far next-character]                                                                                                                                                                                                          
  9.     (if (search-criteria built-so-far next-character)                                                                                                                                                                                        
  10.       built-so-far                                                                                                                                                                                                                          
  11.       (conj built-so-far next-character))))                                                                                                                                                                                                  
  12.                                                                                                                                                                                                                                              
  13. (defn make-string-transformer [f]                                                                                                                                                                                                            
  14.   (fn [string]                                                                                                                                                                                                                              
  15.     (apply str                                                                                                                                                                                                                              
  16.            (reduce (build-string-step f) [] string))))                                                                                                                                                                                      
  17.                                                                                                                                                                                                                                              
  18. ;; solve exercise 1                                                                                                                                                                                                                          
  19.                                                                                                                                                                                                                                              
  20. (def rem-dups                                                                                                                                                                                                                                
  21.      (make-string-transformer                                                                                                                                                                                                                
  22.       (fn [built-so-far next-character]                                                                                                                                                                                                      
  23.         (some #(= % next-character) built-so-far))))                                                                                                                                                                                        
  24.                                                                                                                                                                                                                                              
  25. ;; solve exercise 2                                                                                                                                                                                                                          
  26.                                                                                                                                                                                                                                              
  27. (def squash-spaces                                                                                                                                                                                                                          
  28.      (make-string-transformer                                                                                                                                                                                                                
  29.       (fn [built-so-far next-character]                                                                                                                                                                                                      
  30.         (and (= next-character \space) (= (last built-so-far) \space)))))                                                                                                                                                                    
  31.                                                                                                                                                                                                                                              
  32. [/sourcecode]
Add Comment
Please, Sign In to add comment