Skip to content

Conversation

@rikhuijzer
Copy link
Contributor

Going up multiple directories doesn't seem to be in the Julia base yet. As far as I know, using normpath is the best way, and that is what people typically use for the project root

project_root = normpath(joinpath(@__FILE__, "..", ".."))

from https://stackoverflow.com/questions/35466572. Alternatively, people use dirname multiple times

project_root = dirname(dirname(@__FILE___))

see https://github.com/search?q=filename%3A*.jl+dirname%28dirname%28.

With this PR, I propose to add an integer as second parameter for dirname. Then, we could write

project_root = dirname(@__FILE__, 2)

Or, for longer paths

julia> dirname("/a/b/c/d/e", 3)
"a/b"

"/home"
```
"""
dirname(path::AbstractString, n::Integer) = (repeat([dirname], n)...)(path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just n <= 0 ? path : dirname(dirname(path), n-1)

@stevengj stevengj added speculative Whether the change will be implemented is speculative filesystem Underlying file system and functions that use it labels Dec 29, 2020
@stevengj
Copy link
Member

stevengj commented Dec 29, 2020

Or dirname^n(path)... wasn’t there a general proposal to use exponentiation for function iteration at some point? #34251 (comment)

@rikhuijzer
Copy link
Contributor Author

Or dirname^n(path)... wasn’t there a general proposal to use exponentiation for function iteration at some point? #34251 (comment)

That sounds like a good generalized solution. So, lets close this PR? If not, then I'll change the definition to a recursive one.

@stevengj
Copy link
Member

I'll push a PR to add function^n, and we'll see what the reception is.

@rikhuijzer
Copy link
Contributor Author

👍 Then, I'll close this PR for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

filesystem Underlying file system and functions that use it speculative Whether the change will be implemented is speculative

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants