|
I'm using scala professionally at this point, so I thought we should have a thread for it and the stupid questions I constantly have. What is scala? quote:Scala is an acronym for “Scalable Language”. This means that Scala grows with you. You can play with it by typing one-line expressions and observing the results. But you can also rely on it for large mission critical systems, as many companies, including Twitter, LinkedIn, or Intel do. Scala is a type-safe functional language that runs on top of the JVM and so can access the full range of existing Java libraries. Scala calls itself object-functional: it supports common functional programming constructs (functions that take or return functions, monads, efficient recursion, immutable types, etc.) while also having a strong type system with modern OO features. Functions are themselves objects, in that: code:
code:
Where scala seems to really excel is in distributed environments (where immutability prevents race conditions rather than just annoying programmers) and in asynchronous environments where functional constructs like Future allow you to build non-blocking systems without needing to explicitly declare everything a callback method and just let the compiler figure it all out. For example: code:
Places to learn about scala:
Other resources and major libraries:
KernelSlanders fucked around with this message at 23:54 on Jan 10, 2015 |
# ¿ Jan 10, 2015 23:48 |
|
|
# ¿ May 10, 2024 05:53 |
|
PlesantDilemma posted:Scala seems pretty cool. I'm getting into more Java these days and out of the php slums, Scala looks like it could be a fun next language to tackle. As Fullets suggested, I use it for bigdataish tasks predominately: spark jobs that do product or user clustering, large scale ML, some NLP stuff, etc. It's basically used to productize data science tasks that run through cron. We also use it for internal microservice components of our web service. I've also just started using it in script mode for one-off jobs and data exploration that I used to do in python/numpy/pandas, although that's mostly to take advantage of other scala libraries we've written. I haven't done much Java since school, although I did do some C#, which is syntactically quite similar. I fall into the like it a lot more than Java camp. I would say that it's not so much the language is too complex as that the syntax can be cryptic. I think in general it can be pretty expressive if you want it to be, although there are clearly some cases where syntax that shouldn't be needed by the compiler is. I think context bounds are a good example of that. The type-after-variable-name syntax doesn't really bother me. I think of it as a decorator since most of the time the compiler can figure out the type implicitly and you can just leave the declaration off. Python 3 uses the same syntax for type hinting.
|
# ¿ Jan 16, 2015 16:07 |
|
Cryolite posted:I'm learning Scala coming from C# and this is exactly the type of stuff I'd like to eventually be doing too, bonus points if it's in Scala. Is your company by any chance in the Baltimore/DC area? NYC, sorry. I think if you want a non .NET language to break into big data projects with you should probably start with Python, to be honest. Although if you want to stay in the Baltimore/DC/Nova area you should probably look up what BAH uses. You can certainly do a lot worse than scala and I don't agree with triple sulk's suggestion of Haskel or Clojure, but he's right that very few companies are actively hiring scala developers at this point. In fact, we use "get to learn scala" as a recruiting tool since there's a perception that it may be the Next Big Thing. We've recruited a couple experienced scala devs, but one was one of the early contributors to some core language features. That said, scala will get attention on a resume, at least from us.
|
# ¿ Jan 18, 2015 16:13 |
|
A common programming task I run into at work is needing to process all the elements of a list into a reduced list of fewer elements such as converting web log events to sessions. We've been debating the merits of two different design patterns and wanted to see what others thought. As an example I'll use taking a string and converting it to as set of tuples that count the repeated occurrences of a character in a given run. So "foocookie!!!" becomes List((F,1), (o,2), (c,1), (o,2), (k,1), (i,1), (e,1), (s,1), (!,3)). The first pattern is a tail recursive method that takes two arguments: first the list of already processed elements, and second the list of elements yet to be processed. code:
code:
e: my lines were too long or something KernelSlanders fucked around with this message at 21:41 on Jan 31, 2015 |
# ¿ Jan 31, 2015 21:34 |
|
Yeah, foldRight is probably cleaner than foldLeft. I purposely didn't use an anonymous function because in reality the logic of the Implementation method would be substantially more complex.
|
# ¿ Jan 31, 2015 23:20 |
|
That doesn't really work though since foldRight isn't tail recursive (See: LinearSeqOptimized.scala) and using it on a stream will cause the whole stream to be read into memory. Worse yet it will do so one element per stack frame resulting in a stack overflow if the list is sufficiently large.
|
# ¿ Feb 1, 2015 18:40 |
|
Volguus posted:Yea, have fun googling when you have a problem I was trying to see today if there's a subtle difference between ++ and ::: on List today. Google doesn't like ::: as a search query it seems. That said, being able to write val list = item1 :: item2 :: item3 :: subList1 ++ subList2 is pretty convenient.
|
# ¿ Feb 4, 2015 03:49 |
|
sink posted:I get the sense that corporations are making more strong bets on the JVM than the CLR. I don't have numbers to prove that, but this is my reflection from job hunting in NYC and SF. Although I do run into a a surprising number of .NET shops here and there. Stackoverflow and Zocdoc being two headliners. Do you know if they are jumping into F#? My experience has been the same. I think generally startups want to avoid the Microsoft stack since it's considerably more cost up front. An m3 medium instance on AWS running the windows stack is three times the cost of one running linux. Also linux/akka-http/postgres is only one step removed form linux/django/postgres making it somewhat of an easier migration to dabble in it, for example, for micro-services where CLR requires a completely new stack.
|
# ¿ Feb 8, 2015 16:10 |
|
Slick was a long series of headaches for us and we ended up just rolling our own.
|
# ¿ Feb 16, 2015 16:15 |
|
Depending on the complexity of your project, here's something to get you started if you change your mind.code:
|
# ¿ Feb 16, 2015 19:42 |
|
Basically, you need to use MongoDB if you want true reactive DB access. The other option is to wrap your DB access in a Finagle service and then access that in an asynchronous way, although that's really just hiding the problem.
|
# ¿ Feb 18, 2015 01:12 |
|
Spark 1.3 supposedly has an R or Pandas like dataframe. Has anyone used it yet?
|
# ¿ Mar 20, 2015 13:48 |
|
Steve French posted:If my situation is unique to the Bay Area... It's not. It is somewhat unique to tech companies though. When an airline hires programmers they tend to do it through an HR department filled with people who print out forms to fax to people and they like simple rules like "ten years of python experience."
|
# ¿ Mar 21, 2015 04:14 |
|
sink posted:Virtually every operator has an alpha alias. I'm inclined to agree with you in that I don't particularly care for symbolic soup, but I think it's fairly subjective or contextual. I would rather see a method named ~> than naturalTransformation. Why use two characters when you can use one like ↝? def Σ(x: List[Int]): Int = (0 /: x) (_ + _) KernelSlanders fucked around with this message at 05:15 on Mar 31, 2015 |
# ¿ Mar 31, 2015 05:13 |
|
+ isn't really an operator in scala, just an infix notation method call. (_+_) is syntactic sugar for (a, b) => a.+(b)
|
# ¿ Apr 1, 2015 02:36 |
|
jpotts posted:It's the same where I work, but most of the OOP guys are slowly being converted to FP. I can appreciate both sides of the argument, but in our case the existing OOP implementations are horrible, multi-layered abstractions that are incredibly obtuse. That's not to say that OOP is horrible, but holy poo poo do I hate layers...and the cake pattern. Cake pattern is terrible. I don't understand why anyone would chose it over constructor injection.
|
# ¿ May 8, 2015 01:35 |
|
I've been pretty happy with spray for my toy web apps although serving the original html is a bit clunky.
|
# ¿ Jun 10, 2015 05:47 |
|
Has anyone had any success using Spark's DataFrame object? Am I missing something or is the whole thing just a horrendously designed API that can't possibly be useful for anything? Like in pandas you can df['c'] = df['a'] + df['b']. Is there a simple way to do that in spark Dataframes? What about df['idx'] = df.id.map(lambda x: np.where(ids == x)[0][0])?
|
# ¿ Jul 7, 2015 19:11 |
|
Generally, you don't want to user the Java library if there's a Scala library that does the job. The interfaces just tend to be clunky in Scala code. I'm not sure why you feel String.join(",", a) is cleaner than a.mkString(",") but if you really want to do so, you can always define your own function.code:
|
# ¿ Aug 6, 2015 05:45 |
|
Stream is an attempt at a functional approach to something like the C# yield return although it's probably closer to a generator comprehension in python. That's probably the closest to idiomatic of the approaches you listed. Does this example make more sense?code:
By the way, your implementation of fib2 isn't great because that scanLeft will end up recomputing the entire sequence for every element. Try this to see why: code:
|
# ¿ Aug 20, 2015 16:25 |
|
Yeah, square brackets in scala are basically the same as angle brackets in java. asInstanceOf acts as a cast.
|
# ¿ Oct 16, 2015 06:37 |
|
Has anyone used Ammonite? What did you think of it?
|
# ¿ Oct 18, 2015 15:22 |
|
KICK BAMA KICK posted:I think the intentions of this are pretty clear; a case class (more just to convey the intention of immutability and for the free equals and hashCode than for pattern matching) that is only constructed from the companion's parameterless factories. It's hard to know precisely what's going on, but I suspect you overloaded a factory method with the same number of arguments as the one created for you with the case keyword. This code works fine for me: code:
code:
|
# ¿ Oct 27, 2015 14:06 |
|
FamDav posted:might be a coding horror, but is there a programmatic way to get the concrete parent class of an anonymous class? When I first started learning scala I found myself constantly at war with the type system. I've learned to really appreciate it's power since. In general if you find yourself fighting the type checker, you made a mistake earlier on in your design. It's hard to know what exactly the best solution for you is without the use case (implicit ClassTags?). That said, here is almost certainly something better than java reflection, especially given that once your class goes in a container, type erasure breaks reflection. Sedro posted:Plain old Java reflection will work: x.getClass.getSuperclass This works for the simple case FamDav described, but for an ad hoc anonymous class it's likely to be much less useful: code:
|
# ¿ Nov 5, 2015 03:13 |
|
I commonly have to match on an Option of an instance of a sealed trait. It ends up being an awkward pattern and it really seems like there should be a cleaner way to do it. Supposed I have some code like:code:
code:
code:
|
# ¿ Dec 16, 2015 05:15 |
|
Ah of course. I've used @ but didn't realize you could nest it.
|
# ¿ Dec 16, 2015 06:07 |
|
Hughlander posted:Not sure about the Scala component but that also sounds like a prime Kafka use case. Kafka's the queue, not a language in which to write the consumer. If the goal is to be scalable at some point, I'd vote Spark Streaming as the consumer framework.
|
# ¿ Dec 22, 2015 06:01 |
|
sink posted:how embarrassing for them: https://www.lightbend.com/blog/typesafe-changes-name-to-lightbend This was always going to be a problem for them. The companies that hire consulting firms to build bespoke business software for them are not the ones pushing the adoption of new technologies.
|
# ¿ Feb 25, 2016 14:26 |
|
Scala School from Twitter is great, but getting quite dated now. There's a scala for java programmers tutorial on the scala-lang web site that's pretty good even if you aren't a java programmer.
|
# ¿ May 19, 2016 02:52 |
|
I would have:code:
|
# ¿ Jul 23, 2016 16:47 |
|
|
# ¿ May 10, 2024 05:53 |
|
Is baz a List[String] or is fun(q - foo) a List[String]?
|
# ¿ Aug 6, 2016 16:11 |