In this course you will learn how to apply the functional programming style in the design of larger applications. You'll get to know important new functional programming concepts, from lazy evaluation to structuring your libraries using monads. We'll work on larger and more involved examples, from state space exploration to random testing to discrete circuit simulators. You’ll also learn some best practices on how to write good Scala code in the real world.
Several parts of this course deal with the question how functional programming interacts with mutable state. We will explore the consequences of combining functions and state. We will also look at purely functional alternatives to mutable state, using infinite data structures or functional reactive programming.
Learning Outcomes. By the end of this course you will be able to:
- recognize and apply design principles of functional programs,
- design functional libraries and their APIs,
- competently combine functions and state in one program,
- understand reasoning techniques for programs that combine
functions and state,
- write simple functional reactive applications.
Recommended background: You should have at least one year programming experience. Proficiency with Java or C# is ideal, but experience with other languages such as C/C++, Python, Javascript or Ruby is also sufficient. You should have some familiarity using the command line. This course is intended to be taken after Functional Programming Principles in Scala: https://www.coursera.org/learn/progfun1.

MM

Picks up where the previous course left off... the last assignment is a bit more challenging than the others, well for me it was. Thanks to the people who put the course together.

从本节课中

For Expressions and Monads

We'll start by revisiting some concepts that we have learned from Principles of Functional Programming in Scala; collections, pattern matching, and functions. We'll then touch on for-comprehensions, a powerful way in Scala to traverse a list, process it, and return a new list. We'll see how to do queries with for-comprehensions as well as how the for-comprehension is "desugared" into calls to higher-order functions by the Scala compiler. Finally, we'll discuss what monads are, and how to verify that the monad laws are satisfied for a number of examples.

教学方

Martin Odersky

Professor

脚本

System check the getting started section in the video lectures part of the class website. Everything that is shown on this video is also explained in text in the tool set up page of the class website. So we are here, and we need to install for our class. The first one is the JDK the Java development kit, which is a run time environment in which Scala and Java programs are executed. This time the assignments that you will submit will be rated with Java, and so we still recommend to use this version over Java 7 and 6. The second one says The main first class. Which we will use when building your program and submitting your assignments through coursera. Last but not least, we need an ID that will help us to go to. In this case, we are going to show you how to There are other options though. You can also use this ID and. Well, installing the JDK. Here there are three subsections for each system. As we're in Ubuntu, let's go to the right section. You have another array separating system you can check which comments are necessary to install Java, either in this website or the official website of the solution. In this tutorial, we are only going to cover Ubuntu deriving systems. But in studying other systems it's pretty straightforward. Usually, this is done with sudo apt-get install openjdk-8-jdk. As we have already installed Java, we're going to skip this process, but we're going to show you how this is done. So we go to the terminal and we type sudo apt-get install openjdk-8-jdk. Add JDK. Power password, we see that and we have erased all this expression. In case if you need to check if you have the right version, you have to do java-version. You type this in terminal you press okay enter. And you will see that the first line the expression that you have is in here. In this case we have. Let's install sbt. In order to install sbt, first we need to download it. So we go to the installing sbt, and we click this link, which will refer to this website. I'll show a list to it. Cool. So we go to this link, and now we have instructions to install sbt on any system. So we go to the Linux link. And we'll see Is here, the commands that we need to execute so that sbt is installed in our system. The first ones are creating a new repository or having a new repository to your system and update it, so that we can install sbt with this simple Simple comments. Let's copy paste these comments. .If we go to terminal and we paste them. Once sbt is installed. Let say there we have the diversion of sbt. For that we need to tie this. This is usually going to take some time, the first time because this has to strap itself. As you see here, we do have the right version. 01311. As a side note, as we [INAUDIBLE] versions are backwards compatible with previous versions of [INAUDIBLE], but not the other way around. That's good to know when we use [INAUDIBLE] in our daily Workflow. So, now [INAUDIBLE] For that, we need to go back to a tutorial and also go here in this thing to [INAUDIBLE] tutorial. First, we need to download IntelliJ IDEA. For that we need to get to the official website and make sure that we are in Linux option. And we don't allow the Inellij Idea. As we have already done this, we just need to go to the downloads folder. Oops. Here, yeah, and we'll see that we have the compressed file that we have just unloaded, and the file, or the folder that is the result of the compressing this file, right? So, usually, you can just decompress this file with V and we're going to do it. Again. [INAUDIBLE] Cool, so we have the folder here, right. And what we need to do is go in and we see that we have several folders. Plugins, et cetera. So what we need to do is to go to the binary folder and we need to execute the script that is this idea. That's called Idea.sh. The next step is to install the scan plug in so that IntelliJ recognizes false SVT and SV5. So we go to configure and now we'll go to plug-ins here. And now we have to go here and install different plug-in and type Usually, there is a green button here that says install. As we have already seen this we don't need to do this, but you will. The following steps for you will be to install Scale and then restart. So, we're going to close this, and now, we're going to create a new project. And this window, we have to make sure that we select this color and this SBT. So go next and now we need to name our name projects so let's name it example. And let's make sure that we have the right versions. 18 here and 0138 and 2118. For SVT and the scalar versions. If you don't have anything here, you will need to go, or to type New, and will automatically detect your system, well, your installed data case. So, you just have to press OK and, that's it. So, we'll finish. We finished this and IntelliJ is going to create the project. And it's going to select everything so that we can start coding. [INAUDIBLE] this So, it's going to create a new source folder here and that folder is usually the one that we use for putting our codes. Once we are ready, let's fool around with this colour This is a feature that gives you a very interactive console to work with Scala. Now share, we need to go to source and select main and go straight to this Scala folder. So we just right click here. And new Scala worksheet here. We need to give it a name, say, Hello World. And now we'll see that the format has changed. The idea is that we can type anything we want in here which is a Scala command or a Scala Think only has syntax balance syntax right? And it's going to compile it and it's going to show the result in the right. So let's create here a word as a string. We'll see that intelligent where we first control it so save and we see the intelligent is going to. This can automatically compile this. And output results which is going to be about Cool, so now let's try to do something slightly more complicated, right. So let's go here and let's create I made an application in this color. In order to do that we need to right click in this column. And we are going to create a class. As our main application is going to be a object. We're going to select here object. And we're going to decide the name. This object is going to be inside the package called example. So we going to type example and then dot, and then the name of our main application, which is going to be example with a capital letter. We press ok. And here we see the result. In order to allow Scalar compiler to run this main application, we need to extend Application or apps, A-P-P. It stands A-P-P. And now this is going to execute the body of this object. So it's putting here a statement that it's going to print Hello world. As you see has recognized this as the main application. And there is a small icon here that we can click. And we see that we have several options. The first one is run example. Second one is the example. And the third one is example with the coverage. Here we're just going to run example. And if it's, everything goes well, we're going to see the output in the bottom part of [INAUDIBLE] Cool So it has compiled. Now it's executing in the program, and we see the output here. So let's go back to the tutorial. I'll see where we are. So when the steps have been right and now we're going to learn how to open a SBT project. So we come back and we're going to close the project. And then we're going to import it. By clicking here on Import Projects. So import the project, we go here and we see that IntelliJ has configured a beta file in the project folder, which is Idea project. So we select the build file and we click OK. And now we see this window. In this window you can set up information about your project. Specifically, we see that we can select the television which is a correct one, one, eight and other options that we will explain later on. So we click okay and now SBT is going to create a new SBT project. Well, it's going to reuse the SBT project that we have already created but it has to detect the source files and also which folders are in this project. We see the recognize folders. We continue and now what we're going to do is, we're going to change the beta file. So we find the beta file here and we click in it. And we're going to add a new dependency called a scala test. As you see there is a notification here. And this notification has three options. The first one is Refresh Project, the second one is Enable Auto Import, the third one is Ignore. Enable Auto Import is a feature of IntelliJ that we refresh SBT every time we change a file. An SBT file or an SBT folder. This is extremely useful and I recommend to enable it. So we're going to enable it here. And we're going to add another dependency. So we go back to tutorial. And copy-paste this command here, the second one. We'd save the file with Ctrl + S. And IntelliJ has automatically selected SBT. Now we are waiting for the result of SBT if there is an error. Just a warning, close and now Intellij is going to index and new dependency we just added to the project which is called method everything went fine. Now if we want to execute any after the colon, we press Alt F12 and this is going to open a terminal in our project folder called Example. Now we are able to execute SBT here. Remember that if you do this in another project folder it's not going to work. It has to be the Work project folder. And we can type now an SBT comment. So let's compare again the project. So that's it. Now you are ready to use this kind of sentence. Good luck.