Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111716 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 30648 invoked from network); 30 Aug 2020 04:42:47 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Aug 2020 04:42:47 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 933191804DB for ; Sat, 29 Aug 2020 20:46:49 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f173.google.com (mail-il1-f173.google.com [209.85.166.173]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 29 Aug 2020 20:46:49 -0700 (PDT) Received: by mail-il1-f173.google.com with SMTP id w3so3902724ilh.5 for ; Sat, 29 Aug 2020 20:46:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=fA6BpZDVfwXGxb8+sOVxPzwhrpBfSURTn7vBbfFRrrE=; b=aBKq8yBcgNw4Kve3a7Kd0w0x6v0d1Txdme4mtrb93MhEbCkkzqi101dzSYSrIqAFAW IKYZ2oZVPDDlrrSW8DscvsM/AvnzJ8zsbF4BLWQu4+UWwuInrZa6L3ckaoH8YeTOx4Is mQyOMFSJtPKvqtNUCoQWaiyqu9OmmbFlVAAn3e3wEciOaB+mTzJoNVe3PhDUByNoqf8m v9GNX2MdLOKUCCzm3PhdY1Ew//8lDoxEiw/1x0bs0yBpqPASu7KgLD1AAHMzaKPHBffE K+dnR8TdIAYnYQrtLRvzvFG7yjjSIsuWeVW37f5ieGQ+g7SW1CCFb/+RJsvDCKJzSBJD uHjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=fA6BpZDVfwXGxb8+sOVxPzwhrpBfSURTn7vBbfFRrrE=; b=i2+Ecajwfk7uORVTbA8dCTxMomJWrnOc5N1b7WmZGJZS9zc/YU3sluSWi95mSvU0d2 4ulMdeK9Ok7pReAC2CKXuvb7K2CEQSYD3pgsGz2ZmGYn02kkkKPt9ea8X4rvzmRYVxc/ tlt9lITmTZDGuIn+tEPcJM8A0ZwmTmYgUjp7aD6yoIWh29Dw6gNGxGPuTRk6MvkRVrRQ k6kotP36gbS6rkGfmmu0xeU+m31nl4VwTOLmZNRh9FCQl1lV7QyV+ysWDvs4c8Dsh3ui ua1MfcGmDb0btIVJSlSjUNCGGwHsqyba3Ut6CpQXU5xvg6j525QUpFNoibF/SWJZurKW NXkg== X-Gm-Message-State: AOAM530y6fPrbVC7cgrvY9VcH4u4wdS1GZkkBXIfUL2xzXPD9RNKUPJo NnZfeAByksdeZuRjQGHigMrwysG+RDxUm/mdLTM= X-Google-Smtp-Source: ABdhPJxRpn9uesbElqqG61sYQFq7o/vP27C927isNbRb2JhiqJGDVQ95UMWuaRe4pQBflLuOVcDAE72Nrmz/sXv1Db4= X-Received: by 2002:a92:1901:: with SMTP id 1mr4375848ilz.283.1598759208300; Sat, 29 Aug 2020 20:46:48 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 30 Aug 2020 05:46:36 +0200 Message-ID: To: tyson andre Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000b1359505ae10224c" Subject: Re: [PHP-DEV] Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...) From: ocramius@gmail.com (Marco Pivetta) --000000000000b1359505ae10224c Content-Type: text/plain; charset="UTF-8" Hey Tyson, On Sat, Aug 29, 2020 at 10:24 PM tyson andre wrote: > Hi internals, > > The primitives any() and all() are a common part of many programming > languages and help in avoiding verbosity or unnecessary abstractions. > > - > https://hackage.haskell.org/package/base-4.14.0.0/docs/Prelude.html#v:any > - > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some > - https://docs.python.org/3/library/functions.html#all > - > https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#allMatch-java.util.function.Predicate- > > For example, the following code could be shortened significantly > > ``` > // Old version > $satisifes_predicate = false; > foreach ($items as $item) { > if (API::satisfiesCondition($item)) { > $satisfies_predicate = true; > break; > } > } > if (!$satisfies_predicate) { > throw new APIException("No matches found"); > } > > // New version is much shorter and readable > if (!any($items, fn($x) => API::satisfiesCondition($x))) { > throw new APIException("No matches found"); > } > ``` > > That example doesn't have any suitable helpers already in the standard > library. > Using array_filter would unnecessarily call satisfiesCondition even after > the first item was found, > and array_search doesn't take a callback. > > A proposed implementation is https://github.com/php/php-src/pull/6053 - > it takes similar flags and param orders to array_filter(). > > Previous discussion was in https://externals.io/message/103357#103373 > > - New contributors to projects wouldn't know about any() and all() if it > was reimplemented with different semantics and only occasionally used > (e.g. MyArrayUtil::any()) in various projects) > - If this was provided only in userland, there'd be low adoption and code > such as the first example would remain common. > If the standard library provided it, then polyfills would as well, > making cleaner code easier to write. > > Thanks, > - Tyson > Would it make sense, instead of having a third boolean parameter (causing two parameters to be coupled together - already quite messy with existing array functions) for `any()` and `all()` to just detect if the given callback requires >1 parameter? That would make this much simpler. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --000000000000b1359505ae10224c--