Is there a guide to use just parts of the Scala language? - scala

Scala is a multi-paradigm language. Maybe because of that, its syntax is very complex. There are frameworks that use just parts of Scala, for instance, cats is more FP. Other frameworks use Scala in a more OOP style (like Java on steroids). Are there any guidelines (or even tools) about using restricted sets of the language? For instance, how to use it in a more FP style?

Twitter published "Effective Scala" in 2012 which is a guide for which parts of Scala they have found useful:
While highly effective, Scala is also a large language, and our experiences have taught us to practice great care in its application. What are its pitfalls? Which features do we embrace, which do we eschew? When do we employ “purely functional style”, and when do we avoid it? In other words: what have we found to be an effective use of the language?

Related

What is simplest reactive programming language?

I teach a course in Programming Paradigms. Last year I used Elm as an example of (Functional) Reactive Programming. This summer I noticed that Elm is no longer FRP (according to http://elm-lang.org/blog/farewell-to-frp). I'm looking for another vehicle, one for which I don't have to first teach a lot of syntax.
Reactive Programming in JavaScript seems way too complex. My students will have learned some Scala, but I haven't found a good tutorial on RP in Scala.
Suggestions, anyone? Requirements: reactive, simple syntax.
You can also check Reactor for Spring ecosystem https://github.com/reactor/reactor-core/blob/master/README.md
I suggest RxJava with Java8. It is well documented and maintained. There is a lot of materials- books, articles, tutorials, conference talks, etc.
'reactive, simple syntax'..
I do not see much in common between syntax and being reactive. Java8 has excellent free IDE support (Intellij or Eclipse). For me, it is important in learning new language than paradigim- more important than language itself.

Good examples of idiomatic scala code

Scala is a large and complex language. In my experience, the best way to learn such a language is to read really good code. Are there any open source examples of such code? What can you recommend?
Scalaz. Be aware that it's written in a far more functional style than most Scala code.
Twitter Util (already mentioned in #axel22's answer).
The Scala standard library should be pretty idiomatic in most places. The Scala Swing library is another example.
Usually people who write various frameworks know what they are doing and are experts in Scala. Frameworks such as Akka or Play are good examples of this. Twitter's projects are another.

Learning Haskell with a view to learning Scala

I've read a few questions such as Scala vs Haskell discussing the merits of both languages or which to learn, but I already know that I'd like to learn Scala. I was a Java programmer at uni and now mainly use PHP.
I want to learn Scala as it looks like an improvement on Java for personal projects and I'd also like to learn a functional language to improve my knowledge as a programmer.
I am wondering if it would be a good idea to learn Haskell as an introduction to functional programming as it is purely functional so I'd properly learn it rather than haphazard using bits of functional in Scala without knowing why?
I'd also like to use Haskell for personal projects etc as it looks great, but I don't really see many real world applications of it, seems more used for academic stuff hence wanting to learn it to get the functional understanding then move on to Scala.
Speaking as someone who came from Java, and for whom Scala was a gateway drug to Haskell, I happen to think this is a great idea (learn Haskell first)!
Haskell is conceptually a much simpler language than Scala, and if your goal is to learn how to program functionally, you can't help but do so if you start with Haskell. By design, Scala supports a kind of "legacy mode" of coding in which you don't really have to change your Java (or PHP) habits too much if you don't want to. I think this is a strategic decision--and a good one!--meant to increase adoption amongst crusty Java stalwarts.
But that's not you! You're actually interested in learning something new... so why not go all-out? Learning functional programming in a pure setting, without the clutter and the temptation to regress into old habits, will crystallize the concepts in your brain.
Then by all means return to Scala and learn how it differs from Haskell; it is both weaker in some respects and stronger in others, but you will then be on a much better foundation to appreciate these differences.
I came to Scala from a Java background. Initially I just focused on Scala as a better Java. As time went on I started to use more and more functional concepts in Scala. Eventually I reached a point where I felt I needed a purer understanding of functional programming. It was at this point that I went away and learnt Haskell.
When I cam back to Scala I found it much easier to use a functional style and some of the the concepts I struggled with previously made much more sense.
You don't have to learn Haskell to become a good Scala developers, but I think once you get to a certain level in Scala that having a deeper understanding of functional programming concepts really helps.
I wouldn't do that. If you go straight to Scala (your final aim) you save time. You'll learn functional programming anyway, but as Scala is "less pure" you'll be able to start writing programs in Scala usign some Java-like constructs, which will make the transition simpler.
Even if you go into Haskell, your first programs will be of less "functional quality" than later ones, as only practice improves that. Starting Scala your first programs will be of "less quality" as your first programs in Java or PHP were, but with practice you'll improve. Spending time in unrelated tasks hoping they MAY help, it can result in you wasting that time.
Focus on your aim. If that's Scala, go for it.
UPDATE: I believe this may be relevant to the question. It raises a point on productivity (deliver software) vs seeking "pure knowledge".
The risk of starting directly from Scala, without having played any purely functional programming language first, is that you'll probably be drawn to the procedural solutions a bit too often.
I wouldn't try Haskell as an intro to functional programming, though: not that it's particularly hard - its syntax is amazingly terse - but it's definitely very peculiar (even in the peculiar world of functional programming) and I think you should try with a language you can port more easily to Scala, like Racket or plain Scheme.
To better understand what I mean, look at list comprehensions in Haskell, Scala and Scheme.
I learned Haskell first and then Scala and never regretted. You will gain a much deeper appreciation of FP if you learn Haskell first. Your understanding of the concepts behind FP and why they are important will be seared in. Much of Scala is taken from Haskell (IMHO). It will make more sense when you have the Haskell background. Don't shy away! Your efforts will be rewarded.
For me, I want to learn Scala and functional programming; and, thus simply, I am learning Scala. My study material is Programming Scala. After I have read the early chapter on syntx and stuff, I have skipped chapter covering OO stuff and jumped to Chapter 8 which cover the functional programming stuff. There I let myself struggle with FP.
I read this scala code, which is written in FP way and analyze it. You want to learn Scala for "...an improvement on Java for personal projects." and Haskell for FP intro. So, it seems that first of all, you want to be productive with your java project and then grasp FP as a side effect. To be productive, you have to master the language. There are much to learn and master the language and the only way to master the language is that you have to write the code in that language. As you learn/use Scala, you can't avoid FP out right even if you want to. I have been reading the Scala blogs(the book too) and the authors always go out of their way to do explanation when it comes to FP concept. You don't have to worry about not grasping FP by going Scala route. Yes, you can draw inspiration from Haskell like this when you are learning Scala. Or as Chris Turner above have pointed out, you can delve into more FP when you are already productive with Scala.
You wanna go to point A, why not go to point A directly? If you go via the route of point B (Haskell), you will have to postpone your productivity as Pere Villega mentioned.

Implementing functional programming in Perl

I am trying to learn pure functional programming language like Haskell as I am from Perl background and read that Perl can also implement functional programming techniques. So few questions came in mind:
Whether it is worth doing it in Perl 5? Does Perl 6 will make a difference?
Can anybody suggest some code/examples implementing functional programming techniques in Perl?
Read Higher-Order Perl. You can buy it or download for free. It provides insights even to experienced Perl programmers.
perl6 is still a work in progress, so even though perl6 has much improved support for functional programming at the language level (see perlgeek.de on currying in perl6, for example), you'll probably want to start now with perl5 so you can get to work with what's out there. i recommend looking into cpan for higher-level library support...Array::Utils and others (there's a lot!)
Functional programming is just programming, you can do it in any language. If you like how the Haskell API is laid out, you might like my Data::Monad module, which provides Moose roles for various Haskell typeclasses including Monad. (Unlike Haskell, though, there is no "fail" in my Monad, and all Monads are Functors.)
Here's an example of exercising the various typeclasses in the context of a data structure that handles success or failure (like Control.Monad.Error):
http://github.com/jrockway/data-monad/blob/master/t/error.t
Note that it has the ability to convert usual Perl computations that can fail with an exception to a pure procedure that you can bind to other procedures. This lets "regular Perl" work inside a program designed to have a more functional control flow.
Probably a bit late to the party but this little book is a very good example of implementing functional programming techniques in Perl: http://billhails.net/Book/
The author builds a scheme interpretor in perl 5 from the ground up (and BTW the testimonials are a pure gold).

Scala for Junior Programmers?

we are considering Scala for a new Project within our company. We have some Junior Programmers with only PHP knowledge, and we are in doubt that they can handle Scala. What are your opinions? Some say: "Scala is a complicated beast!", some say: "It's easy once you got it." Maybe someone has real-world experience?
"My coworkers will not understand Scala" is simultaneously overstating its difficulty and insulting your coworkers.
Scala is not that difficult. It's just another programming language. Any trouble that junior programmers have with Scala is going to be more or less the same trouble they would have with any other language.
Your coworkers are smart. Of course, I don't know them, but it's a pretty safe bet unless your company is the kind of organisation that hires stupid people, in which case, you have bigger problems.
That said, at my company we have some core products developed in Scala, and we don't find that people have any more trouble with it than Java. The code is generally more clear and concise, easier to generalise and reuse, etc.
I guess Scala could be used as a "beginners" language. Even though there are tricky ideas behind it, you dont have to use/explain them in the beginning. If you explain pure OO with Scala, I would say it is straight forward and easily understandable. As Scala reduces a lot of code overhead from other languages it might even be easier to learn concepts with Scala than with Java/C++.
A major drawback I see with Scala as a beginner language is the lack of documentation. Don't get this wrong, the official Scala doc is very good and also the few books that are available are quite useful to get the details of the language, but those have not been written for beginners. For example in Java you find hundreds of books titled something like "Learning OO with Java" you wont find that for Scala which may be a show stopper.
As Hannes mentioned, only do new language introductions within research projects and not productive or even flagship projects. If you have some juniors, that makes the situation even better, take some internal tool, you always wanted and needed and let them create it during a research project. This is also a nice opportunity to experiment with different development-processes. And your juniors most probably like to be challenged and will deliver a good prototype and a very well proofed opinion if Scala can be used as a beginner language.
I believe that most people moving to Scala are experienced and enthusiastic coders. I'd suggest that you get in-house experience with a Scala project with your senior programmers first before forming a strategy for mentoring your junior coders. I'd also suggest that you only involve people who are eager to join in.
I would advocate it. But with the proviso that you have clear guidelines on what language features are acceptable for your team. For example, coding primarily in an imperative style (which is familiar for Java-trained people), or perhaps limiting the employment of recursion or closures.
Also plan for seniors to mentor the juniors. This may take the form of any combination of: pair programming, code reviews, info sessions, regular discussion forums, etc.
The opportunity that scala presents for vastly improved coding on the JVM is too great to pass up. When your seniors get into it I would not be surprised if they find renewed passion for development. When your juniors get into it they will be learning best practice JVM development from the start.
If you choose to go this route, perhaps they'll find easier to use the Scalate framework than a more traditional one like Lift, since it allows mixing HTML with Scala, much in the same way as PHP works.
Scala is a very 'normal' programming language. Any programmer should be able to learn this language. The people that have difficulties learning Scala mostly are experienced with imperative languages and are surprised by the functional concepts. So unexperienced programmers may learn it even faster. In my opinion should be no problem, to assign it to juniors. From a management point of view, I would assign a junior and a senior developer as a team (or more of both, depending on the size of the project).
I think it depends whats more importent for you. If you want to learn as possible about OO Programming and the standard stuff its a bad idea.
But what you really give them is a opportunity to learn something really cool and unique. Witch can be good motivation. Scala has many cool stuff in it. If you can handle Scala you can handle a lot of otherstuff as well.
Talk to the Programmers (all of them) and tell them why you wanne to use scala. Ask if the have to motivation to make and learn something not everybody can do and go the extra mile?
If the are go with it!
My initial thought would be that Scala will be too heavy for them but then I guess because Scala is an OO/Imperitave/Functional hybrid, one could introduce them to the OO/Imperative part of scala until their comfortable, but then again they will probably have bad PHP habits in Scala since scala authors mostly prefer the functional style over the imperative one.
So, it could work, but I would do it for a research project, and definately not for a flagship one.
Edit: Perhaps this should be said also: It seems that functional/OO hybrids like Scala is becoming more popular especially because of how functional languages handles parallel processing as opposed to how we know it in languages like in Java. The amount of cores found in a chip is increasing rapidly, so this is important. However, mentioning PHP, it seems that you are developing web server scripts where threading is less important. PHP doesn't even have threads.
This raises another point. Do you want to develop Scala Web applications i.e. Lift. If so then you have a doubled up learning curve which should also be considered.
Imagine that you would have picked Java and asked whether they could handle Java. If your answer is they could, then they can probably handle Scala.
Scala is only marginally more difficult due to:
No great IDE support. The support ranges from poor to good. Not necessarily an issue for a PHP programmer.
Documentation not as rich as Java
Both Java and Scala have new challenges for a PHP programmer (JVM, new libraries, compiled language, statically typed).
I don't think Scala is a complicated beast, but you do need to understand some of the syntactic sugar and design principles, which would be true of learning Java as well.
Yes, if...
Strategic decision has been made to go with Scala
Company can handle the hit (financial and time) that will come from the steep learning curve.
No, if...
No senior Java, C# or C++ programmers can be put on the project too
Can't find a Scala programmer to act as a lead
Programmers don't have the patience to learn Scala or deal with a language where Jars (libraries) are scattered all over the place, rather than in one or two neat packages like PHP.
*Note: if the junior programmers were C++, C#, or Java Software Engineers rather than PHP, then my answer would be different like, Go for it!
I would not recommend it. My experience of Scala is only from homebrew projects, but I would imagine the currently lousy IDE support, quite frequent API changes and a very flexible syntax (that allows one to hang himself and everybody else participating in the project) would cause a lot of problems in a bigger, more official project.
Give them IntelliJ and throw 'em in the deep end.
Here is a blog post I recently stumbled upon:
http://james-iry.blogspot.com/2008/07/java-is-too-academic.html
It shows that even Java can be too academic to be understood by programmers which have no experience in functional programming. On the other hand, Scala allows to write code the "imperative way", so you can avoid all the FP stuff if you do not understand it. In my opinion, Scala is much more concise than Java, so I guess a "junior programmer" should be able to handle it.