首页 > 代码库 > 2.scala控制结构、函数、异常处理

2.scala控制结构、函数、异常处理

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"> <style type="text/css">a:focus { outline: 5px auto -webkit-focus-ring-color } a:hover,a:active { outline: 0 } a { color: #0088cc; text-decoration: none } a:hover { color: #005580; text-decoration: underline } p { margin: 0 0 1.1em } small { font-size: 85% } strong { font-weight: bold } em { font-style: italic } cite { font-style: normal } h1.story_title { margin: 0.7em 0 } .story_tags { margin: 0 0 1.2em } h1,h2,h3,h4,h5,h6 { margin: 1.0em 0 .6em 0; font-family: inherit; font-weight: 500; line-height: 20px; color: inherit } h1,h2,h3 { line-height: 40px } h1 { font-size: 38.5px } h2 { font-size: 31.5px } h3 { font-size: 24.5px } h4 { font-size: 17.5px } h5 { font-size: 14px } h6 { font-size: 11.9px } ul,ol { padding: 0; margin: 0 0 10px 35px } ul ul,ul ol,ol ol,ol ul { margin-bottom: 0 } dl { margin-bottom: 20px } dt,dd { line-height: 20px } dt { font-weight: bold } dd { margin-left: 10px } hr { margin: 20px 0; border: 0; border-top: 1px solid #eeeeee; border-bottom: 1px solid #ffffff } abbr[title],abbr[data-original-title] { cursor: help; border-bottom: 1px dotted #999999 } blockquote { padding: 0 0 0 15px; margin: 0 0 20px; border-left: 5px solid #dddddd } blockquote p { margin-bottom: 0; font-size: 16px; font-weight: 300; line-height: 25px } blockquote small { display: block; line-height: 20px; color: #999999 } blockquote small::before { content: "—?" } q::before,q::after,blockquote::before,blockquote::after { content: "" } address { display: block; margin-bottom: 20px; font-style: normal; line-height: 20px } img { height: auto; max-width: 100%; vertical-align: middle; border: 0 } table { max-width: 100%; background-color: transparent; border-collapse: collapse; border-spacing: 0; border: 1px solid rgba(34,36,38,.15) } .tag.label { display: inline-block; vertical-align: baseline; line-height: 1; margin: 0em 0.125em; background-color: #e8e8e8; border-color: #e8e8e8; background-image: none; padding: 0.6em 0.8em; color: rgba(0, 0, 0, 0.6); text-transform: none; font-weight: bold; font-size: 0.7428rem } .table { width: 100%; margin-bottom: 20px } .table th,.table td { padding: 8px; line-height: 20px; text-align: left; vertical-align: top; border-top: 1px solid #dddddd } .table th { font-weight: bold } .table thead th { vertical-align: bottom } .table caption+thead tr:first-child th,.table caption+thead tr:first-child td,.table colgroup+thead tr:first-child th,.table colgroup+thead tr:first-child td,.table thead:first-child tr:first-child th,.table thead:first-child tr:first-child td { border-top: 0 } .table tbody+tbody { border-top: 2px solid #dddddd } .table .table { background-color: #ffffff } .table-striped tbody>tr:nth-child(2n+1)>td,.table-striped tbody>tr:nth-child(2n+1)>th { background-color: #f9f9f9 } .table-celled.table tr td,.table-celled.table tr th { border-left: 1px solid rgba(34,36,38,.1) } .table-celled.table tr td:first-child,.table-celled.table tr th:first-child { border-left: none } .toc ul { list-style: none } p { line-height: 1.6 } body>p { margin-bottom: 1.8em } body>p>img.center,body>p>a>img.center { display: block; margin: 0 auto; background-color: rgba(128,128,128,0.3) } body { font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, "Hiragino Sans GB", STHeiti, "Microsoft YaHei", "微软雅黑", "WenQuanYi Micro Hei", STXihei, "华文细黑", Heiti, "黑体", SimSun, "宋体", Song, sans-serif; font-size: 1.15em; font-weight: 100; color: #2f2f2f } em.cjk_emphasis { font-style: normal; font-family: Georgia, "Times New Roman", Times, "楷体", "AR PL UKai CN", "NSimSun", "Songti SC", "SimSun", serif !important } body dt,body dd { line-height: 1.35em } body code,body .xiaoshujiang_code { border: 0; font-size: 90% } code { background-color: #D6DBDF; color: #2C3E50; padding: 2px 4px } .xiaoshujiang_code ol { margin-bottom: 0px } body h1 { line-height: 1.6 } body h2 { } body h3 { line-height: 1.1 } .language-treeview.hljs { position: relative } .hljs-folder,.hljs-hiddenfile,.hljs-file { position: relative; vertical-align: top; display: inline-block; height: 16px } .hljs-folder::before,.hljs-file::before,.hljs-hiddenfile::before { top: 0; content: ""; width: 14px; height: 12px; margin-top: 0px; margin-right: 3px; position: relative; display: inline-block; background-repeat: no-repeat } .hljs-file::before,.hljs-hiddenfile::before { height: 14px; margin-left: 1px } .hljs-hiddenfile { opacity: 0.6 } .hljs-file.photo::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-file.plain::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-file.source::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-file.archive::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-file.audio::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-file.video::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-file.pdf::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-file.xls::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-file.doc::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-file.ppt::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-folder::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-hiddenfile::before { font: normal normal normal 14px/1 FontAwesome; content: "?" } .hljs-tvline { margin-left: 6px; position: absolute; text-indent: -99em; padding-bottom: 8px; vertical-align: top; display: inline-block; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(94, 144, 117) } .hljs-folder-branch { width: 8px; height: 8px; margin-top: -1px; margin-left: 6px; text-indent: -99em; position: relative; vertical-align: top; display: inline-block; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(94, 144, 117) } .hljs-folder-branch.win { width: 14px; margin-right: 2px } .hljs-folder-last-branch { height: 7px; width: 7px; margin-left: 6px; text-indent: -99em; position: relative; vertical-align: top; display: inline-block; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(94, 144, 117); border-left-width: 1px; border-left-style: solid; border-left-color: rgb(94, 144, 117) } .hljs-folder-last-branch.win { width: 13px; margin-right: 2px } .mark { border: 0; background-color: #ddf3e7; color: #29754d; font-size: 90%; padding: 2px 4px } .line_mark { border: 1px dashed #3A4C42 } code .mark,.xiaoshujiang_code .mark { font-size: initial; padding: initial } a.attachment { background-color: #ecf0f3; border: 1px solid #bec6cb; display: inline-block; padding: 5px; margin: 2px; min-width: 250px } a.attachment i.fa { font-size: 3em; float: left; margin-right: 0.2em } a.attachment .filename { vertical-align: top; text-align: left; font-weight: bold } a.attachment .filesize { display: flex; opacity: .6; font-size: 80%; white-space: nowrap } .checkbox .checked { text-decoration: line-through } .flow-chart,.sequence-diagram { text-align: center } .plot,.plot-image { text-align: center; min-height: 200px; min-width: 200px } pre,.xiaoshujiang_pre { line-height: initial !important } .xiaoshujiang_code_container pre { margin: 0px } .xiaoshujiang_code_container.xiaoshujiang_code_chunk { margin: 1em 0; padding: 1em } .xiaoshujiang_code_container.xiaoshujiang_code_chunk_hide_code { padding: initial; padding-bottom: 2em } .xiaoshujiang_code_container .out_put { margin: 1em 0; background-color: #FCFFF5; color: #2C662D; padding: 1em } .xiaoshujiang_code_container .out_put_error { background-color: #FFF6F6; color: #9F3A38; margin: 1em 0; padding: 1em } .xiaoshujiang_pre { font-family: monospace; white-space: pre } .story_align_left,.story_align_left .story_image_container { text-align: left } .story_align_right,.story_align_right .story_image_container { text-align: right } .story_align_center,.story_align_center .story_image_container { text-align: center } .story_align_justify,.story_align_justify .story_image_container { text-align: justify } code.hljs.code_linenums,.xiaoshujiang_code.hljs.code_linenums { position: relative } .ol_linenums { padding: 0px; margin-left: 30px; border-left: 1px solid #e0e0e0 } .li_linenum { line-height: 20px; margin-left: 10px; list-style: none; counter-increment: lines 1 } .li_linenum.li_list_style { list-style: inherit; margin-left: 5px } .li_linenum::before,.li_linenum_before_span { content: counter(lines, decimal); position: absolute; left: 0px; text-align: center; width: 2.5em; opacity: 0.5; vertical-align: top } .li_linenum_before_span_hide { display: none } .xiaoshujiang_code .code_line_break_hack { margin: 0; border: 0; border-top: 0; border-bottom: 0 } .mathjax-container { text-align: center } div.mathjax { max-width: 700px; margin: 0 auto; font-size: 14px } text { font-size: 14px } .story_image_container { text-align: center } .story_image_container>.story_image { display: inline-block; position: relative; max-width: 80% } .story_image_caption { border-bottom: 1px solid #d9d9d9; display: inline-block; color: #999; padding: 10px } .story_image_blank_caption { border-bottom: 0px } .story_image>img { } .story_inline_image>img { max-width: 50%; vertical-align: bottom } .story_remote_resource_block { text-align: center } .xiaoshujiang_code_container { margin: 1em 0px; position: relative; padding-bottom: 2em } .xiaoshujiang_code_title_container { font-size: 70%; opacity: 0.5 } .xiaoshujiang_code_title_container>.xiaoshujiang_code_infos { float: right } .task-list-item { list-style: none } .task-list-item>input { margin: 0 0 0 -20px } .video_container { width: 100%; display: table; text-align: center; position: relative; padding-bottom: 56.25% !important } .slideshare_container .inner { position: relative; width: 100% } .slideshare_container .inner iframe { width: 100%; height: 100%; position: absolute; top: 0; bottom: 0; left: 0; right: 0 } .video_container iframe { width: 100%; height: 100%; position: absolute; top: 0; left: 0; z-index: 1; vertical-align: middle } kbd { display: inline-block; padding: 3px 5px; font-size: 11px; line-height: 10px; color: #555; vertical-align: middle; background-color: #fcfcfc; border: solid 1px #ccc; border-bottom-color: #bbb } .blank_anchor_id { float: left } .blank_anchor_id { visibility: hidden } .blank_anchor_id::before { content: "a" } blockquote footer { margin: 1em 0; font-style: italic } blockquote footer cite { margin: 0 1em } .wavedrom_svg text,.wavedrom_svg_defs text { font-size: 11pt; font-style: normal; font-variant: normal; font-weight: normal; text-align: center; font-family: Helvetica } .wavedrom_svg .muted,.wavedrom_svg_defs .muted { } .wavedrom_svg .warning,.wavedrom_svg_defs .warning { } .wavedrom_svg .error,.wavedrom_svg_defs .error { } .wavedrom_svg .info,.wavedrom_svg_defs .info { } .wavedrom_svg .success,.wavedrom_svg_defs .success { } .wavedrom_svg .h1,.wavedrom_svg_defs .h1 { font-size: 33pt; font-weight: bold } .wavedrom_svg .h2,.wavedrom_svg_defs .h2 { font-size: 27pt; font-weight: bold } .wavedrom_svg .h3,.wavedrom_svg_defs .h3 { font-size: 20pt; font-weight: bold } .wavedrom_svg .h4,.wavedrom_svg_defs .h4 { font-size: 14pt; font-weight: bold } .wavedrom_svg .h5,.wavedrom_svg_defs .h5 { font-size: 11pt; font-weight: bold } .wavedrom_svg .h6,.wavedrom_svg_defs .h6 { font-size: 8pt; font-weight: bold } .wavedrom_svg_defs .s1 { } .wavedrom_svg_defs .s2 { } .wavedrom_svg_defs .s3 { color: #000; visibility: visible; display: inline; overflow: visible } .wavedrom_svg_defs .s4 { color: #000; visibility: visible; display: inline; overflow: visible } .wavedrom_svg_defs .s5 { } .wavedrom_svg_defs .s6 { color: #000; visibility: visible; display: inline; overflow: visible } .wavedrom_svg_defs .s7 { color: #000; visibility: visible; display: inline; overflow: visible } .wavedrom_svg_defs .s8 { color: #000; visibility: visible; display: inline; overflow: visible } .wavedrom_svg_defs .s9 { } .wavedrom_svg_defs .s10 { color: #000; visibility: visible; display: inline; overflow: visible } .wavedrom_svg_defs .s11 { } .wavedrom_svg_defs .s12 { } .hljs { display: block; padding: 0.5em; background: #fdf6e3; color: #657b83 } .hljs-comment,.diff .hljs-header,.hljs-doctype,.hljs-pi,.lisp .hljs-string,.hljs-javadoc { color: #93a1a1 } .hljs-keyword,.hljs-winutils,.method,.hljs-addition,.css .hljs-tag,.hljs-request,.hljs-status,.nginx .hljs-title { color: #859900 } .hljs-number,.hljs-command,.hljs-string,.hljs-tag .hljs-value,.hljs-rule .hljs-value,.hljs-phpdoc,.hljs-dartdoc,.tex .hljs-formula,.hljs-regexp,.hljs-hexcolor,.hljs-link_url { color: #2aa198 } .hljs-title,.hljs-localvars,.hljs-chunk,.hljs-decorator,.hljs-built_in,.hljs-identifier,.vhdl .hljs-literal,.hljs-id,.css .hljs-function,.hljs-name { color: #268bd2 } .hljs-attribute,.hljs-variable,.lisp .hljs-body,.smalltalk .hljs-number,.hljs-constant,.hljs-class .hljs-title,.hljs-parent,.hljs-type,.hljs-link_reference { color: #b58900 } .hljs-preprocessor,.hljs-preprocessor .hljs-keyword,.hljs-pragma,.hljs-shebang,.hljs-symbol,.hljs-symbol .hljs-string,.diff .hljs-change,.hljs-special,.hljs-attr_selector,.hljs-subst,.hljs-cdata,.css .hljs-pseudo,.hljs-header { color: #cb4b16 } .hljs-deletion,.hljs-important { color: #dc322f } .hljs-link_label { color: #6c71c4 } .tex .hljs-formula { background: #eee8d5 }</style>

2.scala控制结构、函数、异常处理

scala,大数据

2.1条件表达式

(1)if表达式有返回值,如val result = if(x>0) 0 else -1 ,则result值可能为0或-1
(2)if表达式分支语句返回值的类型不一致时,则表达式返回值的类型是它们共同的父类型Any
val result = if(x>0) 0 else "error"
则result的类型为Any
(3)若if表达式没有分支满足条件,则返回Unit

2.2块表达式

(1)块表达式的值是最后一个语句的返回值,一个块中有多个语句时用逗号隔开。如:
val result = {val x=2;val y=3;pow(x,y)}

2.3循环

(1)while循环的实现方式和java中一样。
(2)scala中的for循环写法如下:
for(i <- 1 to 10){ println(i) }
(3)for推导式。如果for循环的循环体以yield开始,则该循环会构造出一个集合,每次迭代生成集合中的一个值,如:
for(i <- 1 to 10) yield i%3 //得到Vector(1,2,0,1,2,0,1,2,0,1)

2.4函数

一个函数包含了函数名称,参数和函数体等三个元素,如:
def abs(x:Double) = if(x>=0) x else -x
如果不是递归函数,不需要给定返回值类型

2.5参数

(1)默认参数。调用某些函数时可以不用给出参数,此时使用默认参数。如:
def check(path:String,name:String="home")
使用check("local")时传入的两个参数分别为local和默认的home
(2)带名参数。在提供参数值的时候指定参数名,带名参数可以打乱参数列表的顺序,但是带名参数和不带名参数共同使用时,需要将不带名参数放在带名参数之前。
(3)变长参数。参数列表可以为同类型的多个参数。如:
def sum(num:Int*)={ val count = 0 for(i <-num){ count +=i } }
函数得到的是一个序列,但是不代表可以传入一个序列。如sum(1 to 5)这种写法是错误的。

2.6懒值

当val被生命为lazy时,它的初始化会被推迟,直到其被第一次取值时。如:
val a = 0 lazy val b = a*10

2.7异常处理

scala中异常处理与java类似。一般写法如下:
try{ process }catch{ case e:Throwable => some process case _ => other process }finally{ }

2.scala控制结构、函数、异常处理