3 ms·
function slug(string $s) { return pipe($s, fn($s) => strtolower($s), fn($s) => str_replace(" ", "-", $s) ); } slu
by BeefWellington 5y ago
function slug(string $s) {
return pipe($s,
fn($s) => strtolower($s),
fn($s) => str_replace(" ", "-", $s)
);
}
slug("10 WAYS to EAT more HEALTHY"); // 10-ways-to-eat-more-healthy
I'm not sure I see how this example demonstrates the point of something like this.
Surely this is simpler and more succinct?
function slug(string $s) {
return str_replace(" ", "-", strtolower($s));
}
Even if you wanted to break it up into two lines:
function slug(string $s) {
$s = strtolower($s);
return str_replace(" ", "-", $s);
}
A better example might go a long way here.
Edit: Never walk away to refill a coffee before pressing submit or the rest of HN will beat you to it. :P
- trog 5y ago> A better example might go a long way here. I am a Bad Programmer (it's not my day job, though I have managed programmers for many years), so maybe I am just missing the point in examples like this - but I often find this is the case in many code samples for language features. They usually have a trite example that shows how it can work in a really basic case. This is helpful from a purely technical level - as here, I can see what is going on neatly. But they rarely seem to show it being used how the designer expects it to be used in the wild to save time, or increase productivity, or whatever. Like, why would anyone want to do this? I would personally NEVER want to see a function like that in a codebase when it could be written so much more clearly as in your examples. Although I personally would prefer a third style: function slug(string $s) { $s = strtolower($s); $s = str_replace(" ", "-", $s); return $s; } ... more verbose, but I never see the point in trying to save a line if it sacrifices even a small bit of readability. Again, as a Bad Programmer, I like things to be spelled out super clearly whenever possible, so maybe that's just me.
- antifa 5y agoAlso your preference is faster.
- 2000UltraDeluxe 5y agoThis. I too am a bad programmer and readability is so much more important to me than the LOC count. Sure, the pipes example might look nifty, but from a practical viewpoint, it's just not that useful. Next thing you know, someone throws an eval into the mix by mistake, and we end up with unintended consequences.