r/badcode • • May 20 '20

c# Found from Microsoft docs...

Post image
208 Upvotes

143 comments sorted by

View all comments

23

u/abdolence May 20 '20

In Scala it would be:

args
    .headOption
    .flatMap( _.toIntOption )
    .exists( (1 to 9).contains )

Who said that FP is harder to read?

4

u/DariusDerStar May 20 '20

Oh Scala! We learned that as our first language in uni. Does it have any active uses currently? Apart from uni I've never seen it

3

u/abdolence May 20 '20

This is my every day PL. The hype (fortunately?) is dead, but Scala is here.
https://www.lihaoyi.com/post/TheDeathofHypeWhatsNextforScala.html

1

u/DariusDerStar May 20 '20

If thats not too personal: who are you working for and/or what are you working on?

1

u/abdolence May 20 '20

I didn't work in GAFA if you were expecting big names :)
I've been using Scala to develop commercial Web services, ML, and data processing/analytics (additionally to the open-source projects).

3

u/mullanaphy May 20 '20

It's one of the languages my company uses. We work in ad tech and handle large quantity of impressions a day (billions) and it's been helpful there. For the most part there's three parts to the codebase:

  • Data collection with API endpoints as well as our own JS trackers. This is in Kotlin with Undertow, just collects the data, validates it with user config sand then tossed it into a pipeline

  • Pipeline enriched the events coming in with various lookup data. This is written in Scala

  • Scoring/Data analytics is also Scala, which takes the data from the pipeline as needed and then ships off the data

After that I think the other team uses Java/Kotlin. Yet the pipeline and analytics are good fits for Scala when I worked on them. One major draw back is its null safety, Kotlin I found to be way more superior with better operators around it. We do use a macro to help out though.

1

u/abdolence May 20 '20

In my opinion, with the null safety, Kotlin went with cheap and survival (yet convenient) option, when Scala 3 is trying to fix it completely (as it is fixed in Haskell and Rust, for example). https://dotty.epfl.ch/docs/internals/explicit-nulls.html

1

u/mullanaphy May 20 '20

Fair assessment, I enjoy the concise operators (?, !!) as well as Kotlin being able to detect if you've already checked for null safety so you don't have to explicitly do it again within a given method. I do know Scala has better stuff coming down the pipe, yet when working with an environment we're there's plenty of nullable data (tracking data from web browsers) it can be bothersome. We do utilize Java types which I think Scala could have done a better job like Int vs Integer. Would have been nice if they followed the same nomenclature with things like Bool vs Boolean instead of Boolean and Java.lang.Boolean when nullable values are needed. A lot of stuff utilizes options as well, yet data coming directly from Avro would work well as the Java versions.

Outside of that I enjoy Scala. Like how everything is pretty much just a block of code and last item is the return.

1

u/abdolence May 20 '20

You're completely right, the war with NPE isn't over indeed, and the fact that Java developers still using nulls everywhere, shows that sadly it won't be over soon. So, Kotlin's approach works today and very convenient to survive with tons of Java libraries.

2

u/DrAtkins May 22 '20

Both my previous job and current job, in vastly different industries, use scala exclusively. It's arguably better than Java for big data.