4 ms·
I think Scala is hands down the best collections/stream library, I especially like their _ shorthands: val num = words.count(!_.isEmpty())
by kaba0 3y ago
I think Scala is hands down the best collections/stream library, I especially like their _ shorthands:
val num = words.count(!_.isEmpty())
- Marazan 3y agoI think that is a horrifying crime against man and nature. If I wanted to write PERL I would do so.
- kaba0 3y agoWhat is hard to understand about it? _ is universally used as a placeholder, it also usable in pattern matching. Scala can be used to make some perl-y <!! operators, but it is not like that at all, and makes for very readable, understandable code.
- haspok 3y agoAlso targeted at Java 22: https://openjdk.org/jeps/456 https://openjdk.org/jeps/456 (Unnamed Variables & Patterns)
- skrebbel 3y agoSame, I wish more languages had that shorthand
- layer8 3y agoUnderstanding bird droppings like “!_.()” relies on quite some prerequisites.
- kaba0 3y ago! contradicts a boolean. _.isEmpty() is a lambda, applying the isEmpty function on its string parameter. It’s just the two things together.
- layer8 3y agoI know all this. But as I said, pulling it all together involves quite a number of concepts represented by idiosyncratic punctuation, much more than “count all non-empty elements”.
- kagakuninja 3y agoThen your team can write lambdas in a more verbose, but less cryptic style.
- philipkglass 3y agoWhen I was introducing a team to Scala I wrote everything in a more verbose style just to make it clear, e.g. val num = words.count(word => word.nonEmpty) The "bird droppings" (particularly the clever uses of _ found across Scala) were hard for me to grasp when I first started working in the language. Now they're second nature, but I try to remain cognizant of future maintainers who may be coming in to make contributions or bug fixes without first marinating in the language.
- dionian 3y agoThe nice thing is you can just write it like `x => !x.isEmpty` if you want
- sedro 3y agoOr more succinctly val num = words.count(_.nonEmpty)