scala部分應用函數_Scala中的部分函數

scala部分應用函數

Scala部分功能 (Scala partial functions)

A partial function is a function that returns values only for a specific set of values i.e. this function is not able to return values for some input values. This function is defined so that only some values are allowed in the processing of the code. Like in the case of division by 0, we need to restrict the division to avoid errors.

局部函數是僅針對一組特定值返回值的函數,即該函數無法為某些輸入值返回值。 定義此函數的目的是在處理代碼時僅允許某些值。 就像被0除的情況一樣,我們需要限制除法以避免錯誤。

These are inconsistent functions of Scala programming language and in some cases, they can be of great use.

這些是Scala編程語言的不一致功能,在某些情況下,它們可能會很有用。

The implementation of partial functions in Scala needs other methods too. The methods that are used for implementation are apply() and isDefinedAt(). Also, you use the case statements to implement it.

Scala部分功能的實現也需要其他方法。 用于實現的方法是apply()isDefinedAt() 。 另外,您使用case語句來實現它。

The apply() method is used to show the application of a function.

apply()方法用于顯示函數的應用。

The isDefinedAt() method is used to check if the values are in the range of function or not.

isDefinedAt()方法用于檢查值是否在函數范圍內。

Syntax:

句法:

    var function_name = new PartialFunction[input_type, return_type]

Example 1:

范例1:

Implementing partial function using isdefinedat() and apply() methods.

使用isdefinedat()apply()方法實現部分功能

object MyObject 
{ 
val divide = new PartialFunction[Int, Int] 
{ 
def isDefinedAt(q: Int) = q != 0
def apply(q: Int) = 124 / q 
} 
def main(args: Array[String]) 
{ 
println("The number divided by 12 is " + divide(12))
} 
} 

Output

輸出量

The number divided by 12 is 10

Example 2:

范例2:

Implementation of partial function using orElse statement.

使用orElse語句實現部分功能

object MyObject
{ 
val Case1: PartialFunction[Int, String] =
{ 
case x if (x % 3) != 0 => "Odd"
} 
val Case2: PartialFunction[Int, String] =
{ 
case y if (y % 2) == 0 => "Even"
} 
val evenorodd = Case1 orElse Case2
def main(args: Array[String]) 
{ 
var x= 324
println("The number "+x+" is "+evenorodd(x))
} 
} 

Output

輸出量

The number 324 is Even

Example 3:

范例3:

Implementation of partial function using andThen statement.

使用andThen語句實現部分功能

object MyObject
{ 
def main(args: Array[String]) 
{ 
val operation1: PartialFunction[Int, Int] =
{ 
case x if (x%4)!= 0=> x*42
} 
val operation2=(x: Int)=> x/3
val op = operation1 andThen operation2 
println("Initial value is 34\t and the value after operations is "+op(34)) 
} 
} 

Output

輸出量

Initial value is 34	 and the value after operations is 476

翻譯自: https://www.includehelp.com/scala/partial-functions.aspx

scala部分應用函數

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377187.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377187.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377187.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《MySQL——備庫多線程復制策略。》

目錄備庫并行復制能力MySQL5.6版本 并行復制策略MariaDB 并行復制策略MySQL5.7版本 并行復制策略MySQL5.7.22版本 并行復制策略總結備庫并行復制能力 主要涉及兩個方面的并行度: 1、客戶端寫入主庫的能力 2、備庫上sql_thread執行中轉日志relay log 1的并行能力…

人臉是門大生意

我們正處在一個新時代的入口。人有70%的能量是被大腦消耗,大腦90%的能量用來處理視覺信息,人臉則承載了絕大部分的視覺信息。我們要討論的是一個比Google Glass更酷的世界。文/程苓峰-云科技網易郵箱的用戶已經可以用人臉而不是密碼來驗證登陸。安卓4.0實…

【SQL】sql版Split函數。用于拆分字符串為單列表格

【SQL】sql版Split函數。用于拆分字符串為單列表格 功能與.net版string.Split函數類似,只不過.net返回的是數組,這個返回的是一個單列表格,每個拆分出來的子串占一行。可選是否移除空格子串和重復項。市面上類似的函數不算少,但大…

線描算法

線描算法 (Line drawing algorithms) The equation for a straight line is ymxb 直線方程為y mx b In this m represent a slope of a line which can be calculated by the my2-y1/x2-x1 where (x1, y1) are the starting position of the points and (x2, y2) are the end…

為移動端網頁構造快速響應按鈕

背景 在谷歌,我們不斷地推測手機網頁應用的可能性。像HTML5這樣的技術使我們網頁版的應用以及運行在手機設備上的原生應用。而這些技術的成就之一就是我們開發了一種新的創建按鈕的方法,使按鈕的響應時間遠遠快于一般的HTML按鈕。在此之前的按鈕或者其他…

Red Gate系列之一 SQL Compare 10.4.8.87 Edition 數據庫比較工具 完全破解+使用教程

Red Gate系列之一 SQL Compare 10.4.8.87 Edition 數據庫比較工具 完全破解使用教程 Red Gate系列文章: Red Gate系列之一 SQL Compare 10.4.8.87 Edition 數據庫比較工具 完全破解使用教程 Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 數據庫版本控制…

《MySQL——基于位點orGTID的主備切換協議》

一主多從的設置,用于讀寫分離,主庫負責所有的寫入和一部分讀,其他讀請求則由從庫分擔。 一主多從架構下,主庫故障后的主備切換問題。相比于一主一備,多了從庫指向新主庫的過程。 基于位點的主備切換同步 把節點B設…

數據科學和統計學_數據科學中的統計

數據科學和統計學統計 (Statistics) Statistics are utilized to process complex issues in reality with the goal that Data Scientists and Analysts can search for important patterns and changes in Data. In straightforward words, Statistics can be utilized to ge…

java隨機數生成(固定位數)

隨機生成 a 到 b (不包含b)的整數:(int)(Math.random()*(b-a))a; 隨機生成 a 到 b (包含b)的整數:(int)(Math.random()*(b-a1))a;轉載于:https://www.cnblogs.com/zhwl/p/3624726.html

POJ 3670 Eating Together

POJ_3670 由于遞增和遞減是類似的,下面不妨只討論變成遞增序列的情況。 由于Di只有三個數,所以可以考慮將序列分割成三部分,第一部分全部變成1,第二部分全部變成2,第三部分全部變成3。然后我們枚舉3開始的位置&#xf…

《MySQL——如何解決一主多從的讀寫分離的過期讀問題》

目錄兩種架構兩種架構特點強制走主庫方案Sleep方案判斷主備無延遲方案配合semi-sync等主庫位點方案GTID方案兩種架構 基于一主多從的讀寫分離,如何處理主備延遲導致的讀寫分離問題。 讀寫分離的主要目標:分攤主庫壓力。 有兩種架構: 1、客…

json/ 發送形式_24/7的完整形式是什么?

json/ 發送形式24/7:二十四 (24/7: Twenty-Four Seven) 24/7 or 24-7 service, which generally marked "twenty-four seven" is service that is existing at any time and typically, every day in trade business and industry. Substitute orthograph…

《MySQL tips:并發查詢與并發連接區別》

并發連接與并發查詢,并不是一個概念。 在執行show processlist的結果里,看到了幾千個連接,指的是并發連接。 而"當前正在執行"的語句,才是并發查詢。 并發連接數多影響的是內存。 并發查詢太高對CPU不利。一個機器的…

對上拉下拉電阻的作用作個總結(想了解的過來看看)(轉載)

轉自:http://www.amobbs.com/thread-5475279-1-3.html 一、定義:上拉就是將不確定的信號通過一個電阻嵌位在高電平!電阻同時起限流作用!下拉同理!上拉是對器件注入電流,下拉是輸出電流;弱強只是…

給用戶傳入的變量進行轉義操作

先看代碼實現: /* 對用戶傳入的變量進行轉義操作。*/ if (!get_magic_quotes_gpc()) {if (!empty($_GET)){$_GET addslashes_deep($_GET);}if (!empty($_POST)){$_POST addslashes_deep($_POST);}$_COOKIE addslashes_deep($_COOKIE);$_REQUEST addslashes_…

《MySQL——外部檢測與內部統計 判斷 主庫是否出現問題》

目錄select1判斷查表判斷更新判斷外部檢測弊端內部統計一主一備的雙M架構里,主備切換只需要把客戶端流量切換到備庫。 在一主多從的架構里,主備切換要把客戶端流量切換到備庫,也需要把從庫接到新主庫上。 切換有兩種場景:1、主動…

NIM的完整形式是什么?

NIM:無內部消息 (NIM: No Internal Message) NIM is an abbreviation of "No Internal Message". NIM是“無內部消息”的縮寫。 It is an expression, which is commonly used in the Gmail platform. It is written in the subject of the mail, if the…

[Json] C#ConvertJson|List轉成Json|對象|集合|DataSet|DataTable|DataReader轉成Json (轉載)...

點擊下載 ConvertJson.rar 本類實現了 C#ConvertJson|List轉成Json|對象|集合|DataSet|DataTable|DataReader轉成Json|等功能大家先預覽一下 請看代碼 /// <summary> /// 類說明&#xff1a;Assistant /// 編 碼 人&#xff1a;蘇飛 /// 聯系方式&#xff1a;361983679 …

let 只能在嚴格模式下嗎_LET的完整形式是什么?

let 只能在嚴格模式下嗎LET&#xff1a;今天早早離開 (LET: Leaving Early Today) LET is an abbreviation of "Leaving Early Today". LET是“ Leaveing Today Today”的縮寫 。 It is an expression, which is commonly used in the Gmail platform. It is writt…

js 遮罩層 loading 效果

//調用方法 //關閉事件<button οnclickLayerHide()>關閉</button>&#xff0c;在loadDiv(text)中&#xff0c;剔除出來 //調用LayerShow(text)&#xff0c;text為參數&#xff0c;可以寫入想要寫入的提示語 //本方法在調用時會自動生成一個添加到body的div&#x…