以功能性风格将elem与下一个加入 [英] Join elem with next one in a functional style

查看:68
本文介绍了以功能性风格将elem与下一个加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来加入"/分组"列表中的2个元素,如下所示:

I'm trying to find a way to "join"/"groupby" 2 elements in a list as following :

List("a","b","c","d")  -> List("ab","bc","cd")

具有实用风格.

有人知道该怎么做吗?

Would someone know how to do this?

需要使用缩小器,折叠,扫描和其他高阶功能吗?

Need I use reducer, fold, scan, other higher-order function?

推荐答案

Sliding creates subcollections with sliding window, then you just need to map this sublists to strings:

List("a","b","c","d").sliding(2,1).map{case List(a,b) => a+b}

这篇关于以功能性风格将elem与下一个加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆