0 Replies - 2279 Views - Last Post: 07 March 2010 - 03:48 PM

#1 Raynes   User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

[Lisp] Remove duplicate characters from a string in Clojure

Posted 07 March 2010 - 03:48 PM

Description: This function requires clojure.contrib.seq-utils (or clojure.contrib.seq if you're using the github repo version). All it requires is partition-by so you can add this: (:use [clojure.contrib.seq-utils [partition-by]]) to your (ns ..) clause, or (use '[clojure.contrib.seq-utils [partition-by]]) in the REPL or anywhere in the file before the definition of the function. Otherwise, no special instructions.This function takes a string and returns a string in the same order with all duplicate elements removed. For example (running-dups "aaabbcccd") would return "abcd".
(defn running-dups [s] (apply str (map first (partition-by identity s))))


Is This A Good Question/Topic? 0
  • +

Replies To: [Lisp] Remove duplicate characters from a string in Clojure

#2 Raynes   User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: [Lisp] Remove duplicate characters from a string in Clojure

Posted 07 March 2010 - 03:48 PM

Description: This function takes a string and returns a string in the same order with all duplicate elements removed. For example (running-dups "aaabbcccd") would return "abcd".
(defn running-dups [s] (apply str (distinct s)))

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1