Is there an equivalent of Haskell's CHP for Scala? - scala

Clojure has this amazing library implementing Tony Hoare's Communicating Sequential Processes called core.async.
Haskell appears to have an equivalent called chp. (Not sure if it compiles under GHC 7.8).
My question is Is there an equivalent of Haskell's CHP for Scala?

You should have a look at this:
https://groups.google.com/forum/#!msg/scala-user/NljrQ4Mc-aI/3ISm68sqLNAJ
It provides a really interesting list of alternatives for doing CSP in Scala and doesn't recommend JCSP since it's development apparently stopped in 2011.
It also talks about a really interesting paper written by Andrew Bate at Oxford that describes a DSL in Scala for CSP but that whose implementation hasn't been open sourced.
It finally describes Quasar that seems the best alternative. If you're interested in Quasar, this post gives a good description of how Quasar works.

Related

Making sense of Scala FP Libraries

Just for the sake of quick clarity for someone who wants to start working with Scala FP library, on a journey to become better at pure FP.
Would someone clarify the difference/relation between Cats and Cats-Effect, Cats-Effects IO? On top of that, where does Zio, and Monix stand with respect to it? Finally, what would be the relation to ScalaZ 7/8?
So far based on what I have read, a good combination of the library to work with based on the documentation available and what they do would be Cats, then Cats-Effect, and ZIO which could be used with Cats-effects? But I can't quite understand well why and would like to set myself a good path to learn to be better FP programmer while remaining productive, and not having to finish "FP Programming in Scala" before I start making choices.
Scalaz started as an attempt to port to Scala some well-established abstractions from Haskell (like typeclasses for Monad, Functor and much more). Problem with it was, that it doesn't have great documentation, so basically, you needed to use documentation of Haskell libraries in order to understand how to use certain Scalaz resources.
Nowadays, you there's Sam Halliday’s Functional Programming for Mortals which you can use as a learning source for Scalaz.
Cats was created later, as essentially reimplementation what Scalaz provided. Cats has a lot better documentation than Scalaz, there is also great book Scala with Cats.
Scalaz and Cats might have very similar purposes, so they're competing as general purpose FP library for Scala. There are also libraries that serve as compatibility-layer between both libraries.
Cats-Effect is a library, which provides "standard" IO monad for Scala (again idea borrowed from Haskell (?)). It depends on code from Cats core library.
You can read more here why there is a need for IO monad for Scala, when there's standard's library Future.
Monix is another library, which provides an IO monad for Scala, but this time it's called Task. It was meant to be a more high-level abstraction and provide easier interop with code using standard library Future. In reality, it shares a lot of code with Cats-Effect and creator of Monix Alexandru Nedelcu is also one of the main contributors of Cats-Effect.
Here you may find more information about the differences between cats.effect.IO and monix.eval.Task, as well as some of the history of both.
Lastly, there is ZIO which started as an attempt to reimplement IO monad for Scalaz, but ended up as a completely separate library (so it does not depend on Scalaz codebase).
The great thing about all libraries is, they're all implementing typeclasses (like Sync or Concurrent) from Cats-Effect, so using pattern called tagless final you're able to switch between implementation.
That hierarchy of typeclasses also serve as an interpolation library between, many (all (?)) of the IO implementations (as by the time it was created there already were fs2.Task, monix.Task & scalaz.IO). Also, apparently, in a future the IO part may be moved into another module, leaving only the interoperability typeclasses.
If you don't use tagless final you can still use modules that provide interop between certain IO monads, for example zio-interop-cats (between ZIO and Cats-Effect or catnap for Monix-Cats-Effect.
For your information, I recently extended this answer into a full-fledged blog post.

Pure FP in Scala?

I was under the impression that there are folks out there that do write pure applications using Scalaz, but based on this example: [ stacking StateT in scalaz ], it looks like anything real would also be impossibly hairy.
Are there any guidelines or examples of real, modular, loosely-coupled, pure applications in Scala? I'm expecting that this means scalaz.effect.SafeApp and RWST over IO, but I'd like to hear from folks who have done it.
Thanks.
Edit: In the absence of an answer, I've started collecting resources as an answer below. If you have any examples or related links to contribute, please do.
i think you are mixing two different things. one is pure functional programming and second is scala type system. you can do 'pure' programming in any language, even in java. if the language is funvtional than you will have pure functional programming.
does it make your programs work faster? depends on the program - it scales better but for single threaded parts you will rather loose performance.
does it 'save your cognition'? it depends on how good you are in what you are doing. if you work with FP, monads, arrows etc on the daily basis then i assume it may help significantly. if you show the code to the OO developer he probably won't understand anything.
does it save the development time? as previously, i think it may but to be honest it doesn't matter that much. you more often read the code rather than write it
can you do useful stuff in PFP? yes, some companies makes money on haskell
and now, can it be done in scala? for sure. will anyone do it in scala? probably not because it's too easy to break the purity, because type system is too weak and because there are better, 'more pure' tools for it (but currently not on jvm)
I guess I will start collecting resources here, and update as I find more.
Functional Reactive Programming: stefan hoeck's blog, github, examples
Monadic effect worlds for interacting safely with mutable data. (tpolecat)
Mellow database access for Scala (tpolecat)
Dependency Injection without the Gymnastics (tony, rúnar)
Google search for "extends SafeApp"

What well developed iteratee/pipes libraries are available for Scala?

Does Scala have any well developed libraries in the spirit of Haskell's pipes, or at least iteratee?
I found Play's iteratee library first, but I couldn't make it work, and it seems tightly coupled with Play's concurrency primitive Promise, which could be inappropriate in many cases.
Scalaz has some iteratee support (like IterV), but it seems there are only core classes with no additional support functions, predefined iteratees/enumerators etc. Also I couldn't find any documentation, even scaladoc is very sparse, so it's quite difficult to use properly.
And I couldn't find anything similar to pipes.
Building up on comments from Travis, currently there are:
Scalaz 7 iteratee package (iterv, you mentioned, is a compatibility layer with scalaz 6)
A port of Conduit library
Runar's scala-machines library (presentation, haskell version)

Could APL be implemented in Scala as a DSL?

There is a old computer language called APL. Could this be implemented in Scala as a DSL?
http://en.wikipedia.org/wiki/APL_%28programming_language%29
Someone could probably give a better answer than this, but this is my initial thought:
A Scala DSL should in theory be able to implement any programming language because it could build up an arbitrary structure representing the syntax, and then evaluate that.
A Scala DSL could not exactly replicate APL syntax for many reasons, one of which is that
'single quotes'
can denote a string in APL, but not in Scala. Also (from the wikipedia page)
×/2 3 4
wouldn't be valid Scala.
I don't know how close you could get, though...
A Javascript implementation exists here: https://github.com/ngn/apl

How to start on scala [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I am .NET developer and I'd like to broaden my horizons a bit and after checking out modern tendencies decided to try Scala. Can you please advise a good strategy to start on it? Should I learn Java first? What source or handbook should I read? Is there any OS projects to practice Scala and grow on them?
Thanks,
Dominique
You might gain a first impression by visiting Simply Scala where you have an online interpreter available.
An absolute classic is Scala for Java Refugees which was originally written for people coming from Java, but will be quite helpful for you, considering how similar the basics of C#/Java are.
You don't need to learn Java first , but you need to have the Java runtime/development kit installed and working.
Then go to http://www.scala-lang.org/downloads and download the appropriate package for your operating system (I always prefer the nightly builds of Scala, they have more bug-fixes than the latest stable one).
After that, run the Scala REPL which is basically "Simply Scala offline" (Simply Scala uses the Scala REPL behind the covers, too). Even many Java programmers use the Scala REPL to prototype things first.
If you prefer books to learn I can recommend Programming in Scala (2nd edition) by Martin Odersky (if you start from a language design point of view and want the "reference book"). There are others like "Programming Scala" which are more targeted at beginners so to speak, but personally I found "Programming in Scala" excellent and have learned Scala with just that book.
A nice way to start Scala is working with the collection classes. .NET has added something similar lately with LINQ and extension methods, so it will be easy to pick up for you.
A small example to get you started:
//Define a class with some properties
case class Person(name: String, var age: Int, spokenLanguages: String*)
//Create some persons
val joe = Person("Joe", 42, "English","French","Danish")
val doe = Person("Doe", 23, "English","German")
val don = Person("Don", 11, "Italian","French","Polish")
val bob = Person("Bob", 17, "German")
//Access a property
joe.name
//Don had his 12th birthday!
don.age = 12
//Put the persons into a list
val persons = List(joe, doe, don, bob)
//Divide the list into minors and adults
val (minors, adults) = persons.partition(_.age < 18)
//Get the total age of all persons
val personsTotalAge = persons.map(_.age).sum
//Return a list with only those speaking English
val englishSpeakers = persons.filter(_.spokenLanguages.contains("English"))
//Same as the example above.
val englishSpeakers2 =
for{ person <- persons
language <- person.spokenLanguages
if language == "English"
} yield person
I'm not that fluent in C#, but I believe many things might look similar to you.
Some examples of Scala's XML support:
//The shoppingCart for breakfast
val shoppingCart = <list>
<item><name>Tomatoes</name><price>0.30</price><amount>4</amount></item>
<item><name>Eggs</name><price>0.15</price><amount>10</amount></item>
<item><name>Bread</name><price>2.20</price><amount>1</amount></item>
</list>
//How much does it cost?
val total = (shoppingCart \ "item").map(i => (i \ "price").text.toDouble * (i \ "amount").text.toDouble).sum
//This is a Symbol
val sym = 'SomeSymbol
//I'm too lazy to use Strings for XML! (Example for implicits)
implicit def symbol2string(symbol: Symbol) = symbol.name
//Now I can use Symbols too!
val total = (shoppingCart \ 'item).map(i => (i \ 'price).text.toDouble * (i \ 'amount).text.toDouble).sum
You don't need to learn Java first. Are you familiar with functional programming? If you are, you should be able to jump in quite fast. Anyway, here are some thoughts on how you can learn Scala:
Get a good reference book. I recommend Programming In Scala by Odersky, Spoon, and Venners. I find it as one of the most comprehensive Scala books.
As with learning any new language, try writing several small application using Scala. If you're not a functional programmer, you might program it in a different paradigm, but that's okay for now. Try writing your program without using "var, (use val instead)" not using loops, and minimizing state change overall.
Use sbt to build your program. I'm kinda hesitant to recommend this since you have to learn a new tool to write your program. But I find it a great too to write Scala apps with. And many Scala projects use sbt it seems like.
Also check out this comment and that thread overall to help you transition to Scala. Struggle against habits formed by Java when migrating to Scala
Java as a language will not be necessary to start with scala (and anyway java itself is very similar to c#, or actually it's the other way around...).
Once you start doing productive things with scala, though, you will be interacting with a lot of java libraries and learn that java-world is a much broader galaxy of more-or-less standard libraries than .net-world where a lots of the things you need are directly in the standard .NET libraries. You can learn them as you go, but not coming from a java background, the experience might feel overwhelming. It would be the same thing had you started learning java, though....
Other java-specific things you may have to learn are about generics being much less powerful in the JVM and how scala tries to work around this.
As for scala itself as a language, coming from .NET, you may benefit more from reading a few things on functional programming than from learning java. The functional paradigm is the part where I was the most ignorant in my initial approach to scala and that caused me the most trouble in understanding example code from the resources you can find on the scala website.
Building a foundation
For functional programming I would recommend reading SICP (it's online and free).
While you learn java try to take a look at F#. It's a good language, it's well documented and "lives" in the .NET ecosystem
learning scala
Resources on the scala website
Daniel Spiewak's blog
If you want to learn Scala, you'll be much better served by learning Scala and picking up the Java you need as you go. You don't need to learn Java to be able to start using Scala.
A good place to start would be reading through this question, which lists most of the Scala books currently available.
https://stackoverflow.com/questions/3359852/scala-programming-book/3360308#3360308
I don't think you need to know Java to get started on Scala. It's helpful to know the broad strokes though, because most Scala documentation I have read refers back to Java features or bugs.
Regarding Open Source projects, you cna have a look on Github for scala projects. Most open source Scala projects tend to be frameworks, though.
Regarding books, I found both the Artima book, Programming in Scala, and the Pragmatic Programmers, Programming Scala, very good.
In addition to the others: Write simple programs for a while and completely ignore advanced features of Scala. Only move there when you have a good grasp of the basics, the functional paradigm und the type system.
I advise you to take part in Coursera Functional Programming Principles in Scala by Martin Odersky. There are video lectures, assignments and even final exam.
Twitter (one of the scala lovers) recently unveiled their own scala complete tutorial -- Scala School. It is awesome guide which I recommend to all scala beginners.
Scala school was started as a series of lectures at Twitter to prepare
experienced engineers to be productive Scala programmers. ... We
think it makes the most sense to approach teaching Scala not as if
it's an improved Java but as a new language. Experience in Java is not
expected. Focus will be around the interpreter and the
object-functional style as well as the style of programming we do
here. An emphasis will be placed on maintainability, clarity of
expression, and leveraging the type system.
Most of the lessons require no software other than a Scala REPL. The
reader is encouraged to follow along, and go further! Use these
lessons as a starting point to explore the language.
I'm currently developing one which doesn't require prior programming knowledge. It will show the strength of combining functional and imperative programming, and it is very simple to follow. Furthermore, the posts will cover best practice and solve increasingly difficult problems using Scala.
http://vigtig.it/blog/
You should give it a try, part 2 is almost finished :)
Download and install Apache Maven. Use it to create a blank Scala project and write the classic Scala hello world application. This will require you to configure the pom.xml file in the project directory that Maven creates.
Then run mvn compile over and over, correcting errors until it compiles.
Then run mvn package until you pass all the unittests.
And finally, run mvn scala:run
Once you build a real project scala:run has an option to spit out the full Java command needed to run it from a shell script or batch file.