Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123071 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id A1C711A009C for ; Tue, 9 Apr 2024 19:49:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712692220; bh=lUg6PzeS2LC/gk9+nSUJFwfXjBJPbcp4x3Bhk+pJ5sE=; h=Date:Subject:To:References:From:In-Reply-To:From; b=mREp7P0pjrhQR/VnvNe8Vs/Ch0NFy9acrsWxxGJkdu7CjSgTdqHItIvtYvqQTforJ yOebpiSDfZGl3ocSP+5BR7xf5A8J8liiahqPAEUmM7UJdQkZq91AvmxMkzr6oGyrjx kbLv/fKCyW+qPrpJ1CdNKdcNTNoDlZIcNz991tdznGf9OZBHGXf8uaBOV1kZY99S5I l0+Bp34PNsJZ7sZzrAcc282zpZgGGqHsSne6WhGOsquKWfXDIuviFI4h5qIIdklJre PqxJAmVHsbnEnH5nUFweBCY0tOxkj7IYTsncEw54e7iSxY39ktujeUuouUZCpqERmA DN3O7LL+Gb07Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 51A6718095E for ; Tue, 9 Apr 2024 19:50:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 9 Apr 2024 19:50:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1712692185; bh=OqkWHVBgNOwXQNH5be1haGewC6LQ0sbAyt9x3Dv8RtE=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=NJE0qT8XGrSuJOSNFw53F6rinjl81wpqMHL5111zV8cIIg++0yW/qGmJ+ODALFfAD lLIVzpKbUPKG/ffRXtjYJQY2rt8BJbcE9hX+/ocVbC5xWf0JNOhXxt6XnL2NmZaUmt BXGXLzpiQZ6yi/uEtOKtCXbWWrHCebOhW28rtKA/jqGebPC3hffACd/n+g2zFTuBJd /QvwBWM733NqAs0VbDy6zwEBQSxeAcAhPBNx9FsMjWBKECo75zvEM4FBIrphJrqV7i wH8kTfMopcOGdB4kwGbmil6hr7+oSWE9Zs+BkB/jX8D+tAlDReGNruBSR1pZwlr24a F30zpHL8DrHzQ== Message-ID: Date: Tue, 9 Apr 2024 21:49:43 +0200 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Subject: Re: [PHP-DEV] RFC [Discussion]: array_find To: =?UTF-8?Q?Joshua_R=C3=BCsweg?= , Larry Garfield , PHP internals References: <24e4529d-0b75-44de-90ef-34de5dfb1c99@wcflabs.de> <278889be-82ab-4827-a9e7-801b5ba2d8f8@app.fastmail.com> <45b726f4-8085-43e2-b701-6b35bc249409@wcflabs.de> Content-Language: en-US In-Reply-To: <45b726f4-8085-43e2-b701-6b35bc249409@wcflabs.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi On 4/7/24 21:10, Joshua Rüsweg wrote: > On 07.04.24 16:35, Larry Garfield wrote: >> 1. Should this work on arrays or iterables? This is a long standing limitation of PHP. The array operations don't work on iterables, even though we've had iterables for 20 years.) > > In the longer term, it definitely makes sense to create a separate API > here that can handle not only arrays, but iterables in general. I have > heard this suggestion in various places (including in the mailing list) For reference: https://externals.io/message/118896#118896 > and had also looked into it in the process of this RFC, but did not > pursue it further after the initial idea, as it would be important for > me that such an API is planned accordingly and has an appropriate > repertoire right from the start (functions such as map, filter, find, > push, pop, …). In my opinion, a single function would be very out of > place, especially if this API is then really soon tackled and then > possibly differs from the implementation of the RFC. It makes sense to me to not make `array_find` the “odd one out” and widening all the array_* functions probably would not work well, because either the Iterator is converted into array, nullifying the benefits of using an Iterator or the return type needs to be changed, making the signature confusing without generics. That said, adding a “find” function makes sense to me and the implementation looks reasonable. However I'm not sure if adding new array functions piecemeal is the right choice at this point. array_any and array_every are conceptually very similar to array_find and are missing as well. In fact array_any($cb, $array) = array_find($cb, $array, true) !== null and array_every($cb, $array) = !array_any($negatedCb, $array), but it would make sense to have them explicitly for clarity of the reader of the code. >> 2. Key handling. It's good that you have looked into this, because I was going to mention it. :-) However, I don't think a boolean is the right answer, since the question is binary, not true/false. (Those are not the same thing.) I think a small return-mode Enum would make more sense here. > > I like the idea, thank you! However, I am unsure whether an additional > enum for the function would not be too much overhead. I feel the same. Adding an enum for each binary parameter that is semantically true and false feels quite unwieldy with how class / enums / interfaces are currently organized in the namespace hierarchy. Some of the array functions have paired function with a _key suffix, but looking at the docs it appears the difference usually is that they *operate* on the keys, instead of returning the keys. So I'm not sure whether adding a array_find_key companion would be confusing or not. Best regards Tim Düsterhus