Carp/core/Filepath.carp
rrruko e3af878e2a
Return reference from unsafe-first/unsafe-last (#1027)
* core: return Array.unsafe-first as reference (#970)

* core: return Array.unsafe-last as reference (#970)

* Make examples and tests build
2020-11-27 10:19:06 +01:00

19 lines
713 B
Plaintext

(defmodule Filepath
(use Array)
(doc dir-from-path "removes the file name part of a path to a file, similar to the `basename` utility in Shell scripting.")
(defn dir-from-path [path]
(let [segments (split-by path &[\/])
n (dec (length &segments))
without-last (prefix &segments n)]
(concat &(copy-map &(fn [s] (str* s "/")) &without-last))))
(doc file-from-path "removes the base name part of a path to a file, similar to the `filename` utility in Shell scripting.")
(defn file-from-path [path]
(let [segments (split-by path &[\/])]
; this is actually safe here, because split by will return an array of
; length > 0
@(unsafe-last &segments)))
)