Contents of this lesson:A thorough explanation of functional programming in 1:scalaScala functional programming in the 2:spark source code3: Cases and jobsFunctional Programming Begins:def fun1 (name:string) {println (name)}Assign a function name to a variable, then this variable is a function.Val Fun1_v = fun1_Visit Fun1_v ("
, whereas Scala contains a while loop. This is because sometimes the imperative language solution is more readable, especially for people with command-language programming backgroundsIn general, we recommend your challenge while loops in your code in theSame the challenge VARs. In fact, while loops and vars often go handIn hand. Because while loops don ' t result in a value, to do any kind ofdifference to y
Scala Tutorial (12) List Operation Advanced Advance combat
1 List Basic Operation
1.1 List Composition Structure
The array is composed of the head tail two parts: head represents the first element, and tail represents the other elements.
Val bigdata = List ("Hadoop", "Spark")
val data = List (1,2,3)
//array consists of head tail two parts: head represents the first element, tail represents
Length
Number of elements
Map (s = + s + "Y")
Return a list resulting from adding a "Y" to each of the string element in the list
Mkstring (",")
Makes a string with the elements of the list
Remove (s = s.length==4)
Return a list containning al elements of the list except those that has length 4
Reverse
Returns a list in reverse order
Sort
Return a list in alpabetical order
When programs get larger, you need some-to divide-them into smaller,More manageable pieces. For dividing up control flow, Scala offers a approach familiar to all experienced programmers:divide the code into FUNCT Ions. In fact, Scala offers several ways to define functions that is notPresent in Java. Besides methods, which is functions that IS membersOf some object, there is also functions nested within fun
effect. It is common practice to place an action in a class that implements an interface, and then pass one instance of the class to another method.To enable this syntax, you need to provide an implicit conversion.8. Assembly function ProgrammingThe function of the List,set collection is mainly the operation of Val list=list ("Scala", "Spark", "Hadoop") List.map (_) List.map (println) mutable (variable), immutable (immutable ) Scala.collection.Map: I
Today we learned the implementation of chained invocation styles in Scala, and in spark programming we often see the following code:Sc.textfile ("hdfs://..."). FlatMap (_.split ("")). Map (_,1). Reducebykey (_ + _) ...This style of programming is called chained invocation, and its implementation is described in the following code:Class Animal {def Breathe:this.ty
About Scala Programming examplesAt the beginning of learning a programming language, always want to write some relatively large programs and projects, but because the foundation is not solid, often make haste. So, only one step at a time, through some classic small examples to practice and exercise, and eventually continue to deepen the skills of
implicit object, then import the function of this type, and then the man can also be used under the function of implicit object in the implicit conversion.
Implicit parameters, which can be used to transmit the parameters for an implied number of variables.First write a function:def talk (name:string) (implicit content:string) = println (name + ":" + content), the 2nd is an implicit reference, and then the talk-side If there are no implicit parameters, the editor will report it! At this poi
Chapter1 and Chapter2
Method definition:
Def methodname (param1: paramtype, param2: paramtype2, [maybe more]): returntype = {
// Method body
}
In functional programming, the conventional saying is,The method returns a value.Otherwise, a method will produce side effects, andAvoid side effectsIt is an original intention of functional programming.
Array access:
The array in
We recommend the responsive programming course on Coursera, an advanced Scala language course. At the beginning of the course, we proposed an Application Scenario: constructing a JSON string. If you do not know the JSON string, you can simply Google it. To do this, we define the following classes
abstract class JSON case class JSeq(elems: List[JSON]) extends JSON case class JObj(bindings: Map[String, JSON]
to elements in a tuple.The type parameter in the TUPLE4 definition is preceded by a + sign, which represents the Covarience parameter and will be explained in depth later2. Set has two types, immutable and mutable, with the same name, creating immutable set objects by default650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/55/46/wKiom1SJYMeBdQOaAAF8Nvro-Is758.jpg "title=" 2014-12-11 17_15_07-programming in
Scala Programming Problem Sets (01)by GaoDongting International Intelligent Hardware testing baseQ-01: How to use Scala 's Singleton mechanism to express class-level data. Answer: NBSP; in object-oriented programming (oop,object-orientedprogramming) concept, attributes ( Attribute) and functions (function) are di
Today, I learned the 73rd lecture of Scala programming by Liaoliang, mainly the use of file selectors. Let's go through the code to try it out for yourself.Import Scala.swing._Import Java.io.FileImport scala.swing.event.ButtonClickedImport Scala.swing.LabelObject Gui_event extends simpleswingapplication{Val filechooser = new Filechooser (New File (".")) Define file selector, path to local filefilechooser.ti
Scala programming. Here is an example of writing to a file:
Import Java.io._
Object Test {
def main (args:array[string]) {
Val writer = new PrintWriter (New File ("Test.txt"))
Writer.write ("Hello Scala")
Writer.close ()
}
}
When the above code is compiled and executed, it creates a file with "Hello Scala"
Here is a typical decision to make if ... Else the general form of the structure is used in most programming languages:
If statement:
The If statement consists of a Boolean expression followed by one or more statements. Grammar:
Syntax for an IF statement:
if (boolean_expression)
{
Statements'll execute if the Boolean expression is true
}
If the value of the Boolean expression is true, then the code module inside the IF statement is executed. If t
1.There ' s a fundamental difference between your own code and libraries ofOther people:you can change or extend your own code as you wish, but ifYou want to use someone else's libraries, you usually has to take them asThey is.You can change or expand your own code, but the code for others can only be accepted as it is.2.A number of constructs has sprung up and programming languages toAlleviate this problem. Ruby has modules, and Smalltalk lets packag
Concurrent programming in Java is mainly implemented by thread, and concurrency is realized through the mechanism of sharing resources, but it faces deadlock problem. In Scala, concurrency is achieved through message delivery, and actor is the implementation of message delivery.
1, actor first knowledge
Package com.yy.enhance
Import scala.actors.Actor
/**
* Scala
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.