What would be julia equivalent of this ruby one liner?

Hello,

I often use following one liner in ruby to see the details of my PATH. What would be the cleanest version of it in julia with less typing.

ruby -e 'puts ENV["PATH"].split(":").sort' or longer version
ruby -e 'ENV["PATH"].split(":").sort.each { |path| puts path }'

This one line code basically splits PATH environment variable based on : which creates a array of strings, sorts it and displays it. I sometimes add .sort.reverse reverse call after sort.

My julia version

julia -e 'foreach(println, split(ENV["PATH"],":") |> sort )' or
julia -e 'foreach(println, sort(split(ENV["PATH"],":")))'

I do not know if function composition can be used in one line code written on terminal.

julia -e 'println.(sort(split(ENV["PATH"],":")))'
6 Likes

If you want to emulate the “chaining-order” (more or less) as in Ruby you can do:

julia> ENV["PATH"] |> x->split(x, ":") |> x->sort(x) |> x->println.(x);

I thought there might be a better way than with anonymous functions, but I can only do slightly shorter:

julia> ENV["PATH"] |> x->split(x, ":") |> sort |> println

It’s good enough to get the info, but slightly different as I couldn’t end with println.

2 Likes

FYI: I could do shorter:

julia> (println ∘ sort ∘ split)(ENV["PATH"], ":")

help?> \circ

Note, to get the circle, meaning function composition, from math, you type that, then press TAB. I just recalled this possibility, that I had never used in Julia, and a long time ago in math… and there are more ways even with it, see the help, but again, didn’t get print. to work. Maybe it should (bug or to-be-implemented) or already can?

2 Likes

Unfortunately this code requires julia console. I wonder if there is a way to write symbolic characters in literal way without needing latex symbols which requires julia aware environment.

At worst, or maybe easiest, you can copy and paste the unicode character in your shell. Your shell probably supports unicode via hex, e.g. shell - How do you echo a 4-digit Unicode character in Bash? - Stack Overflow

1 Like
julia> foldl(ComposedFunction, (println, sort, split))
println ∘ sort ∘ split
4 Likes