Argument ordering: subject first or last? Just pick one and stick with it!

I have decided to use julia for a small script, that changes string ip4 address to integer ip4 address in a .csv with ~20e+6 rows. I have to say the syntax for function chaining is pretty bad. You can’t even put |> at the beginning of line!

function ip4ToNumber(string)
    hexa = split(string, ".") |>
         x -> parse.(x) |>
         x -> hex.(x, 2) |>
         x -> join(x, "")
    parse(Int, hexa, 16) 
end

It seems like julia tries to take little bit subject last approach (e.g. map takes verb (function) as first argument and subject (data) as last argument) but falls back to subject first most of the time (see join, hex).

Now - parse really creeps me out, since it puts subject at the end, but because it has optional argument, the subject ends up in the middle!

It would be so much better, if julia just picked one approach and followed it:

  • subject last & currying - functional languages primarily use it
ip4ToNumber string = 
    hexa = split "." string
        |>  map parse
        |>  map (hex 2)
        |>  join ""
    parse Int 16 hexa
  • subject first & dot notation - OOP languages primarily use it
ip4ToNumber(string) {
    hexa = string
         .split(".")
         .parse.()
         .hex.(2)
         .join("")
    hexa.parse(Int, 16)
}

Not following convention is one thing, but complete lack of it is whole new level.

2 Likes

Try
https://github.com/MikeInnes/Lazy.jl#macros

If you want the chain macros on the next line, you need to delimit the whole expression so that the parser does not stop too early. AFAIK there is no other way around this.

https://github.com/JuliaLang/julia/issues/5571

This doesn’t address your larger concern, but converting string IPv4 addresses to ints is relatively straightforward:

julia> a = "1.2.3.4"
"1.2.3.4"

julia> Int(IPv4(a))
16909060
1 Like

Related: https://github.com/JuliaLang/julia/issues/19150.