# 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. 1.kafka的介绍

    kafka是一种高可用,高吞吐量,基于zookeeper协调的分布式发布订阅消息系统. 消息中间件:生产者和消费者 举个例子: 生产者:做馒头,消费者:吃馒头,数据流:馒头 如果消费者宕机了,吃不下去 ...

  2. 【C++】重载

    参考:黄邦勇帅 1.操作符重载函数作为类的成员和友元或者独立于类的区别: 当操作符重载函数作为类的成员函数时,操作符重载函数的参数会比作为友元或者独立于类的操作符重载函数少一个参数,因为操作符重载类成 ...

  3. 服务器老是出现502 Bad Gateway?

    给你的upstream增加keepalive配置没有? upstream backend { server http://127.0.0.1:8888; keepalive 32; } 在nginx和 ...

  4. 山东省第六届省赛 H题:Square Number

    Description In mathematics, a square number is an integer that is the square of an integer. In other ...

  5. 模板—数学—Lucas

    模板—数学—Lucas Code: #include <cstdio> #include <algorithm> using namespace std; #define N ...

  6. 福州三中集训day4

    第6天写第4天的博客….可以说是很弱了…… 讲了一天的高级数据结构,可以说很迷,先是并查集,然后是树状数组,线段树,MAP函数,KMP算法. 很难……确实不是很清楚…但是很重要,回去以后这应该说是优先 ...

  7. Codeforces 855C - Helga Hufflepuff's Cup

    855C - Helga Hufflepuff's Cup 题意 要求构建一棵树,树上至多可以存在 \(x\) 个权值为 \(k\) 的重要点,且与重要点连边的点的权值必须小于 \(k\),问有多少种 ...

  8. ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  9. AOJ 2266 Cache Strategy(费用流)

    [题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2266 [题目大意] 有M个桶,N个球,球编号为1到N,每个球都有重量 ...

  10. 【分块】bzoj3295 [Cqoi2011]动态逆序对

    考虑每次删除pos位置一个数x后,所造成的的影响就是,逆序对的个数少了在1~pos-1中大于x的数的个数加上pos+1~n中小于x的数的个数. 那么我们需要的操作就只有查询区间内比某数大(小)的个数. ...