7 ms·
Scala is weird. Sometimes it feels fluid and effortless but sometimes it simply cannot do things you would expect it to. For example, chaining custom methods. W
by tastyminerals2 6y ago
Scala is weird. Sometimes it feels fluid and effortless but sometimes it simply cannot do things you would expect it to. For example, chaining custom methods. Why is it possible to do "arr.map(_.nonEmpty).filter(a => a % 2)" but not possible "arr.map(_.nonEmpty).myCustomFilter(_)"? In D thanks to UFCS and Properties you can chain all kinds of std and custom stuff together which looks more "efforless" and FP in my eyes.
Syntax aside I find Scala code hard to read due to how much implicitness it allows which stems from its "do FP or die" attitude which is again, weird for a language designed to incorporate both OOP and FP. Should I mention that in this regard D does a better job at serving the both worlds?
And finally, which threw me off completely is a real world case where we had to implement a word-entropy based algorithm to process tons of textual data and it appeared to be not much faster than a Python version despite all the optimization we tried to do. Scala is fast but there are many faster languages including Java itself. Considering the fact that its compiling times force you to leave your seat for a cup of coffee, you'd rather think of investing into another hard FP friendly language like Rust which simply has a wider applicability area. But yeah, this is all speculative but if you are thinking about what next language to pick up, these things start to matter.
Where Scala fits nicely is a small team which needs to build some business logic on top of the existing Java backend stack quickly and have a reliable working system. This is where it really shines. Scala provides a nice layer for JVM devs who have a soft spot for FP, safe and elegant solutions.
- fluffything 6y ago> you'd rather think of investing into another hard FP friendly language like Rust which simply has a wider applicability area. FYI Aaron Turon did their PhD on Concurrency+Scala, and ended up leading the Rust core team for more than enough years.
- valenterry 6y agoThere are no "special" methods in Scala. If you see something like "arr.map(_.nonEmpty).filter(a => a % 2)" in the standard library, you can use that syntax yourself too. E.g. "arr.customlyFiltered()" is easy to do. Or even easier "arr.filter(myCustomFilter)". If something doesn't work out for you, please feel free to use e.g. scalafiddle.io and make it example, then you will be helped! :) Scala is indeed not a high performant language. It is as fast as Java and compiles as fast as Java if you only use Java's features. However, then why use Scala in the beginning? Scala is great for writing very maintainable and reusable code, especially with a big team and a lot of business logic. It is also great to "glue" things together like you can do it with Python. For these things it has more than sufficient performance in my opinion.
- tastyminerals2 6y agoThere is no straighforward elegant way of doing "obj.scalaMethod.myCustomMethod.scalaMethod.myCustomMethod" in Scala. I want to create a method and just inject it into the chain, no fiddling or ducktaping or using some black Scala magic. "def myFun(a: String, b: Double): Boolean = {...}" and then "obj.mapValues.myFun.forall(_)" or just anything similar. And yes, Scala is exactly the language when you need safe and maintainable code with far less unit tests required than in Java. But you have to invest into it and sometimes it is simply not worth it. The Scala2 to Scala3 migration situation makes things even worse unfortunately.
- shim__ 6y agoIt's not really black magic https://docs.scala-lang.org/overviews/core/implicit-classes.html https://docs.scala-lang.org/overviews/core/implicit-classes.... but I don't really see why having a filter as a method should even be encouraged.
- tastyminerals2 6y agoImplicits are not encouraged in our code base and for a good reason. This was just an example. It's not about filter at all but the general method chaining. Here is example from D if I must: real[] fun(int[] arr) { return arr.map!(a => a.to!double / PI).array; } void main() { int[] arr = 100.iota.array; // [0, 1, 2, 3, ...] real[] newArr = arr.map!(a=> a*2).array.fun); // [0, 0.63662, 1.27324, ...] } I don't know how you can chain a custom method "fun" to the output of the "map" in Scala without duck typing. Why is this not possible when all conditions type-wise are met? Why you can chain std methods like map, filter, reduce, fold etc. but not custom ones?
- valenterry 6y agoAh, I understand what you mean now. You are looking indeed for the thrush operator. I think it should be built into Scala's standard library, but until that happens, you can use the mouse library or build it yourself. Here is an example: // Need to define this once somewhere in your project implicit class TrushExtension[A](anything: A) { def |>[B](function: A => B) = function(anything) } // Your application code def fun(arr: Iterable[Int]) = arr.map(_.toDouble / Math.PI) val arr = 0 to 100 val newArr = arr.map(_*2) |> fun newArr.foreach(println) // prints 0, 0.63662, 1.27324, ... Execute or change the code here: https://scalafiddle.io/sf/WAKhZtJ/0 https://scalafiddle.io/sf/WAKhZtJ/0 This is maybe not exactly as convenient, but it comes pretty close. "someA.someB.someC" becomes "someA |> someB |> someC".
- hocuspocus 6y ago> Rust which simply has a wider applicability area Rust is a great language but that's a pretty insane statement. The ecosystem is still tiny compared to the JVM.
- pkolaczk 6y agoRust is a great language, and Scala is a great language, but they are different. They optimize for a different thing. Rust definitely rocks at high-performance, resource management, fine grained control over all the aspects of the program, at the expense of developer's time. Scala rocks at developer productivity and building abstractions, sacrificing a bit of performance. However, I must say that Rust is also a very productive and quite powerful language (albeit IMHO not as powerful as Scala) and Scala is actually not that bad at performance either - probably better than a vast majority of other languages out there (albeit not as powerful as Rust or C++). As far as ecosystems are considered - this is really hard to say. Rust has the whole C (and most of C++) ecosystem at hand plus a few really amazing Rust solutions like Cargo. Scala is limited mostly to JVM and JS, which are great ecosystems, but it is not true they offer everything. And there are still some caveats when using Java tools with Scala (e.g. profilers, debuggers or build systems).
- haspok 6y ago> Scala is limited mostly to JVM... No, it's not. You can call native (C) functions from the JVM.
- esarbe 6y agoYou still need a JVM, which what the GP is trying to point out (I think...) That said; nowadays you have the Native and JS runtimes as possible targets as well. Scala.js works surprisingly good with very few caveats.
- pkolaczk 6y agoOf course you can call C functions, but it is neither ergonomic nor performant. And good luck exchanging more complex data structures. Java can't use C structures directly, Rust can.
- aphexairlines 6y ago> compiling times force you to leave your seat for a cup of coffee This is not the case in a codebase split into multiple modules that compile in parallel.