# map 针对每个element进行变换并返回整个修改后的数组
def map_method
arr1 = ["name2", "class2"]
arr1.map {|num| num + "and"}
print "map ====",arr1, "\n"
end def map1_method
arr1 = ["name2", "class2"]
arr1.map! {|num| num + "and"}
print "map! ==== ", arr1, "\n"
end def map2_method
arr1 = ["name3", "class3"]
# &:表示item
arr2 = arr1.map(&:upcase)
print "map2 ====", arr2, "\n"
end
# reduce 把array变换为一个值后返回
def reduce_method
arr1 = ["a", "b", "c", "d"]
arr2 = arr1.reduce(:+)
print "reduce ====", arr1, "\n"
print "reduce ====", arr2, "\n"
end def reduce_method2
sum1 = (1..100).reduce(:+)
sum2 = (1..100).reduce(0) do |sum, value|
sum + value
end
print "reduce sum1 ====#{sum1}\n"
print "reduce sum2 ====#{sum2}\n"
end # select 根据条件返回一个子集
def select_method
arr = (1..8).select {|x| x % 2 == 0}
print "select ====", arr, "\n"
end #reject 根据条件提出一个子集
def reject_method
arr = (1..8).reject {|x| x % 2 == 0}
print "reject ====", arr, "\n" end #each 遍历数组每个元素,但不生成新的数组
def each_method
arr1 = ["name2", "class2"]
arr2 = arr1.each {|num| num + "and"}
print "each ====", arr2, "\n"
end #collect 同map一样,collect!同map!一样
def collect_method
arr1 = ["name2", "clas2"]
arr2 = arr1.collect { |num| num + "and" }
print "collect ====", arr2, "\n"
end

 

Ruby中map, collect,each,select,reject,reduce的区别的更多相关文章

  1. Ruby中访问控制符public,private,protected区别总结

    重点关注private与protected public 默认即为public,全局都可以访问,这个不解释 private C++, “private” 意为 “private to this cla ...

  2. hadoop中map和reduce的数量设置问题

    转载http://my.oschina.net/Chanthon/blog/150500 map和reduce是hadoop的核心功能,hadoop正是通过多个map和reduce的并行运行来实现任务 ...

  3. 【转】Python 中map、reduce、filter函数

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  4. Python函数式编程中map()、reduce()和filter()函数的用法

    Python中map().reduce()和filter()三个函数均是应用于序列的内置函数,分别对序列进行遍历.递归计算以及过滤操作.这三个内置函数在实际使用过程中常常和“行内函数”lambda函数 ...

  5. hadoop中map和reduce的数量设置

    hadoop中map和reduce的数量设置,有以下几种方式来设置 一.mapred-default.xml 这个文件包含主要的你的站点定制的Hadoop.尽管文件名以mapred开头,通过它可以控制 ...

  6. python中map、reduce函数

    map函数: 接受一个函数 f 和一个 list .格式:map( f , L),对L中的每个元素,进行f(x)的一个操作. 例如,对于list [1, 2, 3, 4, 5, 6, 7, 8, 9] ...

  7. 如何确定Hadoop中map和reduce的个数--map和reduce数量之间的关系是什么?

    一般情况下,在输入源是文件的时候,一个task的map数量由splitSize来决定的,那么splitSize是由以下几个来决定的 goalSize = totalSize / mapred.map. ...

  8. Ruby中数组的遍历

    转自:http://www.jianshu.com/p/8de9b60f9350 Ruby中有几个常用的遍历数组的方法,本人经常容易搞混.这里列出来总结一下. each: 从数组中取出一个元素,作为某 ...

  9. Ruby中Block, Proc, 和Lambda

    Block Blocks就是存放一些可以被执行的代码的块,通常用do...end 或者 {}表示 例如: [1, 2, 3].each do |num| puts num end [1, 2, 3]. ...

随机推荐

  1. QPS、TPS、PV等网站业务关键字释义

    QPS:Query Per Second TPS:Transaction Per Second PV:Page View RT:Response Time UV:Unique Visitor DAU: ...

  2. k8s的持久化存储PV&&PVC

    1.PV和PVC的引入 Volume 提供了非常好的数据持久化方案,不过在可管理性上还有不足. 拿前面 AWS EBS 的例子来说,要使用 Volume,Pod 必须事先知道如下信息: 当前 Volu ...

  3. Django-ContentType

    背景:学位课.专题课.价格策略(每一种课程(学位课和专题课下可分为不同的种类的课程)在不同学习时间内的价格不同) 例如:如何将课程表与价格策略表关联起来: 用外键是可以将课程表和价格策略表关联起来的, ...

  4. 关于因各种原因,造成LINUX主机不能通过域名访问自己的解决办法

    因路由黑洞或其它原因造成自己域名或外网IP的解决办法: vi /etc/hosts 添加一行: 10.28.86.250 www.edusoa.com 保存退出.

  5. redis特性与使用场景

    一.8大特性 1.速度快 数据存储在内存,可达到10万OPS 2.可持久化,断电不丢数据 所有数据保存在内存中,对数据的更新异步的保存在硬盘中 3.多种数据结构 字符串.哈希.列表.集合.有序集 合位 ...

  6. linux:/lib/libc.so.6: version `glibc_2.7′ not found【没有解决】采用新方法达到目的

    1 下载glibc wget http://ftp.gnu.org/pub/gnu/glibc/glibc-2.7.tar.gz 2. tar zxf glibc-2.7.tar.gz 3. cd g ...

  7. (3)webApi postman使用

    https://www.getpostman.com/

  8. POJ 1988 Cube stacking【并查集高级应用+妙用deep数组】

    Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes ...

  9. Codeforces Round 254 (Div. 2)

    layout: post title: Codeforces Round 254 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  10. centos7下扩充swap空间

    命令:swapon -s #查看系统的swap配置命令 创建步骤: 1. 决定SWAP文件的大小,先指定区块大小:bs,再指定区块数量count,则SWAP文件的大小是:count*bs 在root用 ...