alexr_rwx: (my fandom writes your software)
Alex R ([personal profile] alexr_rwx) wrote2004-03-26 02:36 pm

"Are you asking for a challenge???!!"

For all j00 l337 hackers out there...

Choose your favourite language; how many lines and how many minutes does it take you to write a program that takes in an arbitrary text file, find all the unique words in it, and spit them back?

(I had need to do this, just a few minutes ago, and I did it in like 20 very calm, relaxed lines of LISP, in just a coupla minutes... but I bet if you're good at Perl, you could do it in fewer lines and fewer minutes than that)

Post source :)


(load "ci-utils.lisp") ;; for macros get-hash-keys and dolines

(defvar *words* (make-hash-table :test #'equal))

(defun populate-words (filename)
  (dolines (line filename)
	   (loop for word in (getwords line)
		 do (setf (gethash word *words*) t))))

(defun getwords (line)
  (read-from-string (format nil "(~a)" (strip-period line))))

(defun strip-period (str)
  (string-trim "." str))

(defun list-of-words ()
  (get-hash-keys *words*))

(defun print-all-words ()
  (loop for word in (list-of-words)
	do (format t "~a~%" word)))

ext_110843: (withtux)

[identity profile] oniugnip.livejournal.com 2004-03-26 02:55 pm (UTC)(link)
I'd be impressed :)

I'm trying to conceptualize how you'd do this in 1 line of Perl, because, you know, Everything Can Be Reduced To One Line Of Perl, but it might be a very long one...