起航学习网

- 让每个人都能学到最前沿新知识、新技能!
起航学习网
当前位置: 起航学习网 > 职业教育 > reduce是什么

reduce是什么

时间:2019-03-15 16:50:09来源:职业教育网 作者:IT培训网 已有: 名学员访问该课程

  快捷搜索:reduce(1)

前言:Produce a single value from a sequence through repeated application of a reduction function.

reduce

ReQL command: reduce

Command syntax

sequence.reduce(function) → value r.reduce(sequence, function) → value

Description

Produce a single value from a sequence through repeated application of a reduction function.

The reduction function can be called on:

  • two elements of the sequence

  • one element of the sequence and one result of a previous reduction

  • two results of previous reductions

The reduction function can be called on the results of two previous reductions because the reducecommand is distributed and parallelized across shards and CPU cores. A common mistaken when using the reducecommand is to suppose that the reduction is executed from left to right. Read the map-reduce in RethinkDBarticle to see an example.

If the sequence is empty, the server will produce a ReqlRuntimeErrorthat can be caught with default.
If the sequence has only one element, the first element will be returned.

Example:Return the numbers of documents in the table posts.

r.table("posts").map{|doc| 1 }
    .reduce{ |left, right| left+right }
    .default(0).run(conn)

A shorter way to execute this query is to use count.

Example:Suppose that each posthas a field commentsthat is an array of comments.
Return the number of comments for all posts.

r.table("posts").map{|doc|
    doc["comments"].count()
}.reduce{ |left, right|
    left+right
}.default(0).run(conn)

Example:Suppose that each posthas a field commentsthat is an array of comments.
Return the maximum number comments per post.

r.table("posts").map{|doc|
    doc["comments"].count()
}.reduce{ |left, right|
    r.branch(
        left > right,
        left,
        right
    )
}.default(0).run(conn)

A shorter way to execute this query is to use max.

Related commands

  • group

  • map

  • concat_map

  • sum

  • avg

  • min

  • max

Get more help

Couldn't find what you were looking for?

  • Ask a questionon Stack Overflow

  • Chat with usand our community on Slack

  • Talk to the team on IRCon #rethinkdb@freenode.net— via Webchat

  • Ping@rethinkdbon Twitter

  • Post an issueon the documentation issue trackeron GitHub

来源地址:http://www.epx365.cn/qgtk/201947282.html

来源标题:reduce是什么


你也许会喜欢如下的文章?
(责任编辑:IT培训网)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
培训学校
IT培训网 访问该机构站点 报名留言 加为好友 用户等级:注册会员 用户级别:10 机构名称:IT培训网 联 系 人:罗老师 联系电话: 联系手机:13783581536 在线客服:起航学习网客服 在 线 QQ:起航学习网客服 电子邮件:3158895217@qq.com 网站域名:http://www.cnitedu.cn 注册时间:2016-07-18 11:07 最后登录:2019-03-15 17:03
推荐内容