将List [Int]的部分和加入List [Long]

时间:2017-06-15 12:42:07

标签: scala list

我从输入中读到List[Int]。 我想将该列表的部分总和变为List[Long]

val a: List[Int] = readLine().split(" ").map(_.toInt).toList
val sumList: List[Long] = a.scanLeft(0)(_.toLong + _.toLong).tail.toList

但是,我收到此错误:

  Solution.scala:54: error: type mismatch;
 found   : Long
 required: Int
            val sumList: List[Long] = a.scanLeft(0)(_.toLong + _.toLong).tail.toList
                                                             ^
Solution.scala:54: error: type mismatch;
 found   : List[Int]
 required: List[Long]
            val sumList: List[Long] = a.scanLeft(0)(_.toLong + _.toLong).tail.toList
                                                                              ^

知道我做错了什么吗?

2 个答案:

答案 0 :(得分:2)

(function() { 'use strict'; var filesToCache = [ '.', 'index.html', 'pwa-stylesheets/css/styles-new.css' ]; var staticCacheName = 'cache-v3'; self.addEventListener( 'install', function(event) { event.waitUntil( caches.open(staticCacheName) .then(function(cache) { return cache.addAll(filesToCache); }).then( function() { self.skipWaiting(); }) ); }); self.addEventListener( 'fetch', function(event) { var url = event.request.url; if( url.indexOf( "http://localhost:8080" ) > -1 && url.indexOf( "http://localhost:8080/api/" ) == -1 && url.indexOf( "http://localhost:8080/pwa-" ) == -1 ) { // event.request.url = "http://localhost:8080/"; // not allowed event.respondWith( caches.match(event.request).then( function( response ) { if( response ) { console.log( 'Found ', url, ' in cache' ); return response; } console.log( 'Network request for ', url ); return fetch( event.request ).then( function( response ) { if( ! response.ok ) { console.log( "Network request failed." ); return null; } return caches.open( staticCacheName ).then( function(cache) { cache.put(event.request.url, response.clone()); return response; }); }); }) ); } }); self.addEventListener('activate', function(event) { var cacheWhitelist = [staticCacheName]; event.waitUntil( caches.keys().then(function(cacheNames) { return Promise.all( cacheNames.map(function(cacheName) { if( cacheWhitelist.indexOf(cacheName) === -1 ) { return caches.delete(cacheName); } }) ); }) ); }); })(); 应为0。 "零值"大多数功能操作似乎都是类型推断的基础。

答案 1 :(得分:2)

为什么不将最初的List设为List[Long],因为您无论如何都要将Int转换为Long

val a: List[Long] = readLine().split(" ").map(_.toLong).toList
val sumList: List[Long] = a.scanLeft(0L)(_ + _).tail.toList