4 ms·
There 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 you
by valenterry 6y ago
There 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".
- tastyminerals2 6y agoLooks good! Thanks for the tip.
- scns 6y agoIf you would call it pipe operator, like it's called in many (Elm, Elixir, F#, OCaml) other languages that have it, people would understand you faster IMHO. It is even discussed for inclusion into JavaScript.
- dwijnand 6y agohttps://www.scala-lang.org/api/current/scala/util/ChainingOps.html#pipe[B](f:A=%3EB):B https://www.scala-lang.org/api/current/scala/util/ChainingOp...
- valenterry 6y agoNice I didn't know that! I still like to add |> on top of it. "foo |> bar |> baz" just visually parses easier than "foo pipe bar pipe baz"
- vvillena 6y ago
- valenterry 6y agoAssuming that your object is a list of tuples, then you can do: val myFun = (a: String, b: Int) => b % 2 == 0 List("a" -> 1, "b" -> 2, "c" -> 3).map(myFun.tupled).forall(identity) // false List("a" -> 2, "b" -> 4, "c" -> 6).map(myFun.tupled).forall(identity) // true But I think that's not what you mean... are you maybe looking for the "thrush" / |> operator? Or do you have some example from another language that does a better job and show how it looks there?
- vvillena 6y agoThere is a feature in Scala 2 that covers the specific thing you want: implicit classes. You can use them to add custom methods. It's clean and straightforward. And it will be even easier in Scala 3, where this pattern is coded into an even simpler feature: extensions.
- esarbe 6y agoIn the Scala 2.x this can be done with an implicit class. It's a bit cumbersome and not very use-case oriented (mechanism over intention) and so Scala 3 has introduced extension methods[0]. This gives you exactly what you want. Since Scala 3 is pretty much completely backwards compatible (and with the use of Tasty you can use Scala 3 code in Scala 2.x projects), so I don't see the migration as a big problem. The Scala team has spent a lot of work to make the transition as painless as possible. [0] https://dotty.epfl.ch/docs/reference/contextual/extension-methods.html https://dotty.epfl.ch/docs/reference/contextual/extension-me...
- ph2082 6y agoThank you for URL. Just when I needed. Any idea when "Scala 3" can be production ready ?
- esarbe 6y agoFrom what I saw in the latest presentation by Martin, we can expect Scala 3 to be released in late Spring/early Summer 2021. Part of the release testing is building the community release[0], which is a large part of the relevant Scala F/OSS code out there. Whether you consider that to be production ready you have to decide for yourself, I myself am pretty happy about it. Keep in mind that for now Scala 2 style code will still be compiled and you can use existing Scala 2 code in your Scala 3 project. The migration should be pretty smooth. [0] https://github.com/scala/community-build https://github.com/scala/community-build