嗨我想用" for" scala中的map方法。
我该怎么做?
例如,对于每行读取,我想生成一个随机字:
Array[(String, String)] = Array((1,""), (2,""), (3,""), (4,""), (5,""), (6,""), (7,""), (8,""), (9,""), (10,""), (11,""), (12,""), (13,""), (14,""), (15,""), (16,""), (17,""), (18,""), (19,""), (20,""), (21,""), (22,""), (23,""), (24,""), (25,""), (26,""), (27,""), (28,""), (29,""), (30,""), (31,""), (32,""), (33,""), (34,""), (35,""), (36,""), (37,""), (38,""), (39,""), (40,""), (41,""), (42,""), (43,""), (44,""), (45,""), (46,""), (47,""), (48,""), (49,""), (50,""), (51,""), (52,""), (53,""), (54,""), (55,""), (56,""), (57,""), (58,""), (59,""), (60,""), (61,""), (62,""), (63,""), (64,""), (65,""), (66,""), (67,""), (68,""), (69,""), (70,""), (71,""), (72,""), (73,""), (74,""), (75,""), (76,""), (77,""), (78,""), (79,""), (80,""), (81,""), (82,""), (83,""), (84,""), (85,""), (86...
我目前的输出是:
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class CartService {
totalQuantity: Number = 0;
constructor(private _http: Http) { };
getCartItems(userId) {
let headers = new Headers({ 'Authorization': 'JWT ' + localStorage.getItem('currentUserToken') });
let options = new RequestOptions({ headers: headers });
return this._http.get('http://localhost:3000/cart/getitem/' + userId, options)
.map((response: Response) => {
this.totalQuantity = response.json().totalQuantity;
return response.json();
})
.catch(this._handlerError)
}
updateCartItem(item){
console.log(item);
let headers = new Headers({ 'Authorization': 'JWT ' + localStorage.getItem('currentUserToken') });
let options = new RequestOptions({ headers: headers });
return this._http.post('http://localhost:3000/cart/updateitem/', item, options)
.map((response: Response) => {
this.totalQuantity = response.json().totalQuantity;
return response.json();
})
.catch(this._handlerError)
}
removeCartItem(item){
let headers = new Headers({ 'Authorization': 'JWT ' + localStorage.getItem('currentUserToken') });
let options = new RequestOptions({ headers: headers });
return this._http.post('http://localhost:3000/cart/deleteitem/', item, options)
.map((response: Response) => {
this.totalQuantity = response.json().totalQuantity;
return response.json();
})
.catch(this._handlerError)
}
_handlerError(err: any) {
console.log(err);
// throw err;
return Observable.throw(err);
}
}
我不知道为什么右边是空的。
答案 0 :(得分:2)
这里不需要var
。这是一个单线
Seq.fill(len)(chars(rnd.nextInt(51))).mkString
这将通过重复调用len
来创建长度为chars(rnd.nextInt(51))
的Char序列,然后将其变为字符串。
因此,您将获得以下内容:
import org.apache.spark.rdd.RDD
import scala.util.Random
val chars = ('a' to 'z') ++ ('A' to 'Z')
val rdd = file.map(line => {
val randomWord = {
val rnd = new Random
val len = 4 + rnd.nextInt((6 - 4) + 1)
Seq.fill(len)(chars(rnd.nextInt(chars.length-1))).mkString
}
(line, randomWord)
})
答案 1 :(得分:1)
word.concat
不会修改word
但会返回一个新字符串,您可以将word
变为一个变量并为其添加新字符串:
var word = new String
....
for {
...
word += char
...
}