Quite new to Elixir, coming from JS - really enjoying the functional paradigms and very readable and maintainable syntax.
One of the features I like the most is the ability to pipe as it makes the code so much easier to read and reason about, and I feel like it’s changing my mental models in a very positive way.
I was curious whether all functions are intentionally created make their first argument the thing that would intuitively be piped into it; having recently tried to use File.write/3 it seems maybe not, since the first arg is the filename rather than the data to write.
I intuited that this would pipe the markdown to a file called transcript.md but what it’s actually doing is writing the text transcript.md to a file using the entire transcript as the filename.
Is there any way to override which arg the piped result from the previous function is passed to? Am I using this wrong somehow?
I have a similar feeling about the argument order of that function and instinctively try to pipe to it. You can pipe to then if you really want to keep it in a pipeline. I tend to assign to a variable called content and put write on its own line. To each their own.
This has been talked about to death in the past and will never a thing. As mentioned by @cmo, you can use then which was essentially introduced for this purpose.
File.write is a weird function and I’ll add my name to the list of people who have instinctively tried to pipe into it. The majority of the time I’m not piping into it and instinctively give the file name first and I’m even generally in the habit of calling functions with side effects on their own line, so I’m happy with status quo here. I also just assign to a variable then pass it.
When you put it like that, it actually sounds really intuitive and I totally get why piping doesn’t work in the way I previously thought was intuitive.
I like the pattern of putting a function with side effects on its own line too though, which obviously isn’t hard - I think I might’ve been a bit drunk on the pipe option.
Thanks for the warm welcome, lovely to see intelligent and encouraging replies on my first post, despite it having been asked many times before (and apologies for that, my search hadn’t turned up any results I could see).
This is a very common affliction It’s a big a sticking point for me and I could complaining about it for many hours, so it’s always nice to hear someone identifying it in themselves The pipe operator is awesome but you shouldn’t go out of your way to use it. It’s useful a lot of the time anyway so there is no need to force it when it doesn’t fit. I actually really dislike then but there was enough noise wanting a positional pipe that I believe this was the compromise. Don’t quote me on this but that’s what I get from reading between the lines.
Don’t worry about asking questions that are probably already answered, especially when you are new and not too sure what to search for. People here are generally cool and just scroll past topics they aren’t interested in. You’ll only potentially get scolded if it’s clear that you’ve made no effort on your own and given absolutely not thought before posting, but usually that’s just in the form of a simple, “What have you tried?” answer