Advertisement
chemoelectric

cleanpath.scm

Aug 8th, 2018
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 0.92 KB | None | 0 0
  1. #! env -S csi -s
  2.  
  3. ;;
  4. ;; Remove empty and duplicate entries from a colon-separated path.
  5. ;;
  6. ;; For example: ":a:b:b:::c:a::" --> "a:b:c"
  7. ;;
  8.  
  9. (require-extension data-structures)
  10. (require-extension extras)
  11. (require-extension srfi-1)
  12.  
  13. (let ([args (command-line-arguments)])
  14.   (if (not (= (length args) 1))
  15.       (let ([progname (program-name)])
  16.         (write-string progname #f (current-error-port))
  17.         (write-string ": command line error\n" #f (current-error-port))
  18.         (write-string "Usage: "#f (current-error-port))
  19.         (write-string progname #f (current-error-port))
  20.         (write-string " PATH\n" #f (current-error-port))
  21.         (exit 1))
  22.       (let* ([dirty-path (car args)]
  23.              [dirty-entries (string-split dirty-path ":")]
  24.              [clean-entries (delete-duplicates dirty-entries)]
  25.              [clean-path (string-intersperse clean-entries ":")])
  26.         (write-string clean-path))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement