3 ms·
I'm not sure what `inspect` does, but can you clarify what you mean by proper pipes? julia> x = [3,2,4,5] 4-element Array{Int64,1}: 3 2 4 5
by jessebett 6y ago
I'm not sure what `inspect` does, but can you clarify what you mean by proper pipes?
julia> x = [3,2,4,5]
4-element Array{Int64,1}:
3
2
4
5
julia> x |> sort |> x->reshape(x,(2,2))
2×2 Array{Int64,2}:
2 4
3 5
works.
It gives a syntax error if the pipes are on the newline. Is that your gripe?
Or is it that it doesn't automatically partially apply the function, requiring you to use an anonymous function, like `x->reshape(x,(2,2))` instead of as you have it `resize(100,100)` implicitly. I prefer the explicitness.
- dnautics 6y agoYes, by "proper pipes" I mean partial application. (I used to code julia but now I'm mostly living in elixir because that pays the bills) Yes, the explicitness of what you wrote is a marginal gain in preferability from an idealized point of view, but I promise you your eyes and brain will be more tired from that explcit form than if you just learn to read pipelines as passing into the first value (in elixirland we have lambdas that you can pipe into, too. Nobody does this.) You don't know what you're missing by not having an inspect function. In elixir it's styled "IO.inspect", and it pretty-prints the value as a side-effect, and returns the value unchanged. You can pass a "label" option as your second parameter, and that will add a label to the pretty print. For example, assume I have a module M with the expected functions: value = 10 |> M.add_3 |> IO.inspect(label: "A") |> M.times(4) |> IO.inspect(label: "B") |> M.minus_2 |> IO.inspect(label: "C") will print: A: 13 B: 52 C: 50 and value will be assigned 50. Now, the great thing about that is that you can blat a bunch of IO.inspects simultaneously using multiline cursors. That low friction 1) causes you to actually use it, and 2) cause you to also set your code up so that it's easy to IO.inspect, which typically also means cleaner, and easier to read code. It is IMO the most powerful debugging tool that I've ever used. Example: https://github.com/ityonemo/ex_dhcp/blob/master/lib/ex_dhcp.ex#L355 https://github.com/ityonemo/ex_dhcp/blob/master/lib/ex_dhcp.... That small pipeline does a pretty complex set of actions - deserialize a raw binary to a structured datatype, then call a handler, then process a handler return result. There are four datatypes that run through that pipeline. Nonetheless, I can inject IO.inspects into that with six keystrokes (I have a vscode snippet), run the code, and watch as the data are transformed, and immediately identify what corner case I missed and write a proper test for it. And yes, it is far less powerful if the pipes can't tolerate the newline. (though you usually can still multicursor on a single line). Anyways, I'm not saying this to rag on Julia. I'm just saying I think julia can and SHOULD have this, and it would be awesome. Also, elixir needs to learn a thing or two from julia's repl.