From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 083F31F5AE for ; Sat, 11 Jul 2020 17:48:14 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 466BA120A5A; Sun, 12 Jul 2020 02:47:41 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (unknown [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id F25B8120A41 for ; Sun, 12 Jul 2020 02:47:38 +0900 (JST) Received: by filterdrecv-p3las1-7754f7d4cc-zg8r5 with SMTP id filterdrecv-p3las1-7754f7d4cc-zg8r5-20-5F09FB4A-3D 2020-07-11 17:47:54.72462747 +0000 UTC m=+1380852.586801151 Received: from herokuapp.com (unknown) by geopod-ismtpd-3-2 (SG) with ESMTP id CYsTn7XuSyiiMGhMrWV-0A for ; Sat, 11 Jul 2020 17:47:54.542 +0000 (UTC) Date: Sat, 11 Jul 2020 17:47:54 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 74919 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17016 X-Redmine-Issue-Author: parker X-Redmine-Sender: Eregon X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr1tb408aXvGcug560oa4H?= =?us-ascii?Q?swa9UB2TDEtxbb6F0VVqP1ngnuAbDzh1UBOKYpU?= =?us-ascii?Q?wLPvg7QkxJ3yQaSkNM=2F5NZk3ivSeoRjjDdrsSML?= =?us-ascii?Q?bNWVMr5dTfeWc3fq0pFol8oMOu0=2FSApTQe1XZqY?= =?us-ascii?Q?ELgBP5MINaFCvQ9eEWRE9Bdlf0JeOfaEpyu2N=2FA?= =?us-ascii?Q?VEdYFrWVJiTTZVIC0=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 99129 Subject: [ruby-core:99129] [Ruby master Feature#17016] Enumerable#scan_left X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #17016 has been updated by Eregon (Benoit Daloze). parker (Parker Finch) wrote in #note-4: Thanks for your reply, I think it will help to discuss this issue at the dev meeting. Maybe `prefix_sum` or just `prefix` or something like that would work? Having `sum` in it is kind of confusing though as it can be any "operation" not just `+`-ing numbers, but it seems an official "term" for it (https://en.wikipedia.org/wiki/Prefix_sum#Scan_higher_order_function). > But assigning to a variable inside of the block passed to `map` doesn't feel very Ruby-ish to me: > ```ruby > val = 0 > collection.map { |x| val = val + x } > ``` It's just my opinion, but I see nothing wrong with that (details: it could be `val += x`). I'd even go as far as saying `each_with_object` is often less readable than using a captured variable. I think "purely functional, not a single re-assigned variable" often introduces significant extra complexity, when Ruby is a language that embraces both functional and imperative programming. And anyway `each_with_object` is only useful if mutating some object (typically an Array or Hash). Again, it's just my opinion :) ---------------------------------------- Feature #17016: Enumerable#scan_left https://bugs.ruby-lang.org/issues/17016#change-86505 * Author: parker (Parker Finch) * Status: Open * Priority: Normal ---------------------------------------- ## Proposal Add a `#scan_left` method to `Enumerable`. (The name "scan_left" is based on Scala's scanLeft and Haskell's scanl. It seems like "scan_left" would be a ruby-ish name for this concept, but I'm curious if there are other thoughts on naming here!) ## Background `#scan_left` is similar to `#inject`, but it accumulates the partial results that are computed. As a comparison: ``` [1, 2, 3].inject(0, &:+) => 6 [1, 2, 3].scan_left(0, &:+) => [0, 1, 3, 6] ``` Notably, the `scan_left` operation can be done lazily since it doesn't require processing the entire collection before computing a value. I recently described `#scan_left`, and its relationship to `#inject`, more thoroughly in [this blog post](https://medium.com/building-panorama-education/scan-left-a-lazy-incremental-alternative-to-inject-f6e946f74c00). ## Reasoning We heavily rely on the scan operation. We use an [event-sourcing](https://martinfowler.com/eaaDev/EventSourcing.html) pattern, which means that we are scanning over individual "events" and building up the corresponding state. We rely on the history of states and need to do this lazily (we stream events because they cannot fit in memory). Thus the scan operation is much more applicable than the inject operation. We suspect that there are many applications that could leverage the scan operation. [This question](https://stackoverflow.com/questions/1475808/cumulative-array-sum-in-ruby) would be more easily answered by `#scan_left`. It is a natural fit for any application that needs to store the incrementally-computed values of an `#inject`, and a requirement for an application that needs to use `#inject` while maintaining laziness. ## Implementation There is a Ruby implementation of this functionality [here](https://github.com/panorama-ed/scan_left/) and an implementation in C [here](https://github.com/ruby/ruby/pull/3078). ## Counterarguments Introducing a new public method is committing to maintenance going forward and expands the size of the Ruby codebase -- it should not be done lightly. I think that providing the functionality here is worth the tradeoff, but I understand any hesitation to add yet more to Ruby! ---Files-------------------------------- scan_left_example.rb (2.93 KB) -- https://bugs.ruby-lang.org/