在Scala的溪流

Scala | 流 (Scala | Streams)

Stream in Scala is a type of lazy val. It is a lazy val whose elements are evaluated only when they are used in the program. Lazy initialization is a feature of Scala that increases the performance of the program.

Scala中的Stream是一種惰性val 。 這是一個懶惰的val,其元素僅在程序中使用時才被評估。 延遲初始化是Scala的一項功能,可以提高程序的性能。

Syntax:

句法:

    val str = 1 #:: 2 #:: 3 #:: Stream.empty

Elements of a stream are created using #:: operator using Stream.empty at the end of initialization.

在初始化結束時,使用#::運算符使用Stream.empty創建流的元素。

Program to show the creation of a stream

顯示流創建的程序

object myObject 
{ 
def main(args:Array[String]) 
{ 
val myStream =  2 #:: 4 #:: 6 #:: Stream.empty; 
println("New Stream created...")
println(myStream)
} 
}

Output

輸出量

New Stream created...
Stream(2, <not computed>)

Creating stream using stream.cons

使用stream.cons創建流

You can create a stream using stream.cons. It will create an immutable stream and needs an import statement Scala.collection.immutable.Stream.cons.

您可以使用stream.cons創建流。 它將創建一個不可變的流,并且需要一個導入語句Scala.collection.immutable.Stream.cons 。

import scala.collection.immutable.Stream.cons 
object myObject 
{ 
def main(args:Array[String]) 
{ 
val myStream: Stream[Int] = cons(2, cons(4, cons(6, Stream.empty)))
println("New Stream created...")
println(myStream)
} 
}

Output

輸出量

New Stream created...
Stream(2, <not computed>)

Accessing elements of the stream

訪問流中的元素

In streams, take method is used to take/access elements.

在流中, take方法用于獲取/訪問元素。

Program to access elements using take method

程序使用take方法訪問元素

import scala.collection.immutable.Stream.cons
object myObject 
{ 
def main(args:Array[String]) 
{ 
val myStream: Stream[Int] = cons(2, cons(4, cons(6, cons(9, Stream.empty))))
print("Accessing Stream : ")
print(myStream) 
print("\nTake first 2 elements of stream : ") 
myStream.take(2).print
print("\nTake all elements from stream : ") 
myStream.take(5).print
} 
} 

Output

輸出量

Accessing Stream : Stream(2, <not computed>)
Take first 2 elements of stream : 2, 4
Take all elements from stream : 2, 4, 6, 9

Creating an empty Stream

創建一個空流

You can create an empty stream in Scala using Stream.empty.

您可以使用Stream.empty在Scala中創建一個空流。

object myObject 
{ 
def main(args:Array[String]) 
{ 
val myStream: Stream[Int] = Stream.empty[Int] 
print("This is an empty Stream : ") 
println(myStream)
} 
} 

Output

輸出量

This is an empty Stream : Stream()

翻譯自: https://www.includehelp.com/scala/streams.aspx

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

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

相關文章

適合高速驅動電路的推挽電路

http://www.dzsc.com/data/html/2008-9-10/69023.html 圖1是使用NPN/PNP型晶體管的互補推挽電路&#xff0c;適于驅動功率MOSFET的門極。此電路雖然具有門極電流的驅動能力&#xff0c;但射極輸出波形不能比輸人信號快。 圖2是此電路的開關波形。它表示出tf、tr都快&#xff0c…

cholesky分解

接著LU分解繼續往下&#xff0c;就會發展出很多相關但是并不完全一樣的矩陣分解&#xff0c;最后對于對稱正定矩陣&#xff0c;我們則可以給出非常有用的cholesky分解。這些分解的來源就在于矩陣本身存在的特殊的 結構。對于矩陣A&#xff0c;如果沒有任何的特殊結構&#xff0…

socket編程常見函數使用方法

socket知識 有了IP地址&#xff0c;socket可知道是與哪一臺主機的哪一個進程通信 有了端口號&#xff0c;就知道是這個進程的哪一個套接字進行傳輸 應用進程使用描述符與它的套接字進行通信&#xff0c;也就是說一個進程創建一個套接字時就會返回一個套接字描述符 socket的…

需求變更流程不規范,項目早晚得完蛋

很多人&#xff0c;做的項目不少&#xff0c;但成功的不多。這是一個值得深思的問題。 項目為什么這么難做&#xff1f;需求蔓延&#xff0c;客戶難搞是基本原因。 如何解決上述問題&#xff1a; 1&#xff09;強化需求調研和項目設計在整個項目中的重要性 一般地&#xff0c;需…

html 表格套表格_HTML表格

html 表格套表格A table is a set of rows and columns, which could be created on a webpage in HTML, by <table> tag. The tabular representation of complex data makes it readable. 表格是一組行和列&#xff0c;可以通過<table>標簽在HTML網頁上創建。 復…

Android判斷界面

仿造微信&#xff0c;第一次進入去引導界面&#xff0c;否則進啟動界面。 package edu.hpu.init;import edu.hpu.logic.R;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.os.H…

HDU計算機網絡系統2021復習提綱

目錄計算機網絡系統的主要功能TCP/IP模型與OSI模型的層次結構及各層功能。&#xff08;掌握&#xff09;TCP/IP參考模型各層次所對應的主要設備局域網的體系結構與IEEE.802標準數據鏈路層的編址方式和主要設備原理數據鏈路層CSMA/CD的技術原理交換機VLAN原理與劃分方法數據鏈路…

ruby 線程id_Ruby中的線程

ruby 線程idRuby線程 (Ruby Threads) In Ruby, with the help of threads, you can implement more than one process at the same time or it can be said that Thread supports concurrent programming model. Apart from the main thread, you can create your thread with …

Dynamic web project --- AspectJ Project

本來想今天晚上 直接轉到 以前的web項目 做測試。。。可惜在eclipse 添加 aspectj的時候 提示我不是 aspectj項目。。于是我就百度了好久&#xff0c;發現好多人都和我一樣 &#xff0c; 不過我也發現了一些可以的 比如右鍵 AJDTtools --> convert to Aspectj Project ,可惜…

2013 南京邀請賽 A play the dice 求概率

1 /**2 大意&#xff1a;給定一個色子&#xff0c;有n個面&#xff0c;每一個面上有一個數字&#xff0c;在其中的m個面上有特殊的顏色&#xff0c;當擲出的色子出現這m個顏色之一時&#xff0c;可以再擲一次。。求其最后的期望3 思路&#xff1a;假設 期望為ans4 ans 1/…

擲骰子

Description: 描述&#xff1a; In this article, we are going to see a dynamic programing problem which can be featured in any interview rounds. 在本文中&#xff0c;我們將看到一個動態的編程問題&#xff0c;該問題可以在任何采訪回合中體現。 Problem statement:…

《YOLO算法筆記》(草稿)

檢測算法回顧 5、6年前的檢測算法大體如下&#xff1a; 手動涉及特征時應該考慮的因素&#xff1a; 1、尺度不變性 2、光照不變性 3、旋轉不變性 這一步驟稱為特征工程&#xff0c;最重要的一個算法稱為sift&#xff0c;(回顧SIFT講解)體現了上述所有的觀點。 在分類的過程中…

U盤安裝Centos6.3

一 首先下載Centos6.3的光盤鏡像文件&#xff0c;網上到鏡像實在是太多了。 CentOS-6.3-i386-bin-DVD1.iso CentOS-6.3-i386-bin-DVD2.iso 二 下載個新版本的UltraISO, 在其菜單“啟動”下有“寫入硬盤鏡像“功能到&#xff0c;原來用到綠色版本是8.6.2.2011不支持&#xff0c;…

[轉]粵語固有辭彙與漢語北方話辭彙對照

本文轉自&#xff1a;http://beta.wikiversity.org/wiki/%E7%B2%B5%E8%AA%9E%E5%9B%BA%E6%9C%89%E8%BE%AD%E5%BD%99%E8%88%87%E6%BC%A2%E8%AA%9E%E5%8C%97%E6%96%B9%E8%A9%B1%E8%BE%AD%E5%BD%99%E5%B0%8D%E7%85%A7 粵語固有辭彙與漢語北方話辭彙對照 「粵語」&#xff08;或稱「…

openlayer調用geoserver發布的地圖實現地圖的基本功能

轉自&#xff1a;http://starting.iteye.com/blog/1039809 主要實現的功能有放大&#xff0c;縮小&#xff0c;獲取地圖大小&#xff0c;平移&#xff0c;線路測量&#xff0c;面積測量&#xff0c;拉寬功能&#xff0c;顯示標注&#xff0c;移除標注&#xff0c;畫多邊形獲取經…

LLVM與Codegen技術

LLVM 百度百科 LLVM是構架編譯器(compiler)的框架系統&#xff0c;以C編寫而成&#xff0c;用于優化以任意程序語言編寫的程序的編譯時間(compile-time)、鏈接時間(link-time)、運行時間(run-time)以及空閑時間(idle-time)&#xff0c;對開發者保持開放&#xff0c;并兼容已有…

跟烏克蘭人學編程1

今天要Disable一個菜單&#xff0c;工程項目多&#xff0c;不容易找。 烏克蘭人建議我用Spy&#xff0c;將靶拖到目標窗體上就可以看到類名。轉載于:https://www.cnblogs.com/SunWentao/archive/2012/12/19/2825220.html

html網頁轉圖片_HTML圖片

html網頁轉圖片HTML圖片 (HTML Images) Images are visuals of something that look elegant. In web pages, images are used to create a good and appealing design. 圖像是外觀精美的視覺效果。 在網頁中&#xff0c;圖像用于創建良好且吸引人的設計。 The <img> ta…

Android學習拾遺

1. java中的flush()作用&#xff1a;強制將輸出流緩沖區的數據送出。 2. 文件存儲&#xff1a; 存儲到內部&#xff1a;另外使用一個class實現&#xff0c;最開始初始化用了this,后來放在這里不合適&#xff0c;改成了帶參數的構造方法。 包括存儲、讀取、追加 讀取&#xff1a…

OLAP 技術之列式存儲與數據壓縮(快查詢方法之一)

前言 列式存儲和數據壓縮&#xff0c;對于一款高性能數據庫來說是必不可少的特性。一個非常流行的觀點認為&#xff0c;如果你想讓查詢變得更快&#xff0c;最簡單且有效的方法是減少數據掃描范圍和數據傳輸時的大小&#xff0c;而列式存儲和數據壓縮就可以幫助我們實現上述兩…