surf-history-unique.janet 800 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env janet
  2. (defn get-lines
  3. [file-path]
  4. (let [rfile (file/open file-path :r)
  5. file-contents (string/split "\n" (file/read rfile :all))]
  6. (file/close rfile)
  7. file-contents))
  8. (defn write-to-file
  9. [file-path str]
  10. (let [wfile (file/open file-path :w)]
  11. (file/write wfile str)
  12. (file/close wfile)))
  13. (defn main
  14. [& args]
  15. (let [file-path (get args 1)
  16. lines (sort (get-lines file-path) <)
  17. uniq-list @[]]
  18. (var new-string @"")
  19. (loop [line :in lines :when (-> line empty? not)]
  20. (let [split-lines (string/split " " line)
  21. url (get split-lines 1)]
  22. (unless (find |(= url $) uniq-list)
  23. (array/push uniq-list url)
  24. (set new-string (string new-string line "\n")))))
  25. (write-to-file file-path new-string)))