Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123106 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 B9E5A1A009C for ; Thu, 11 Apr 2024 01:03:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712797436; bh=+C84asKyn6lv7Y37ajrGlR1luPhGiMkXTE1LRtE6K1o=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Uz8Ig99amOpAmmTCtkaEdoghOlHS3xcOsUPWe66PuBS6ctjQsaBsDNwD78J+Ie6C/ O4CBtNu3ehCiZoozL87oZCGW3fPrWTMaXDSY/Cyd2SCfu6imQmWqw00OSeCy7vcQ2f empwEsSt79LkRvv2Pcr5k2RGoXehvaheWCL8Iy7PR+ExMUO/Fv3wpMaOfBCMndM0BF g+nFM7/1W82/PNzMl6hnhOSbrghluCLSn5RE8l7chdl7XBAnLpM6y7EoW+NGSOguSq SqpfVUKkrPLT/t0Lbpzw3IyFHMZfnnK9z2LSIo0lEGwrW+DMwpOwEDb8rjBSGVQk20 HW3wd4zbrbGnA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 560E9180044 for ; Thu, 11 Apr 2024 01:03:54 +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.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,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 mail-io1-f54.google.com (mail-io1-f54.google.com [209.85.166.54]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 11 Apr 2024 01:03:53 +0000 (UTC) Received: by mail-io1-f54.google.com with SMTP id ca18e2360f4ac-7cf092f6bb7so323440039f.3 for ; Wed, 10 Apr 2024 18:03:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=datadoghq.com; s=google; t=1712797400; x=1713402200; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=e3UvWMdXHjACZL0MvqdbYLaLcoHxXpYHMyO2EcUnI9I=; b=KvJoe5n92mFoa0GTiwCyXhD7iMMp2UClPeDaq4jeVfJU445LEF8fLbVRRyGaNyoNW7 tSQE/mMn63939LBTKluBO3PlsSemgMypuJOOZOcxeGqn91b3z2n6sJm8uJQFcYcjLGKi t1vDF9D3hERhXewsc5JyhAsyT3Oa+cux2vJP8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712797400; x=1713402200; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=e3UvWMdXHjACZL0MvqdbYLaLcoHxXpYHMyO2EcUnI9I=; b=pY5bNT24TRxRtSvgUeeVDtaUXzlIWPpr68xygHKJ/Hdxf6YYfLyL9xL8eiZU6dPd5U G3uwpGcqSO425/JwJ/0Yxtx5M5Ri0B6esh9J3g4a6OGvbEq3yxPRQrdIOZgqwk0/m/yJ 265OeOmnhgQUyWxV/gWGVl/qidCu2k/dXniU9QCKX35khR9p4Ta9YCHFOvpXR8T+83Z8 GZMxaArQ3YGG2WKHKyfRIZRa3dNQBx8bsJ5y06HlzK+DPSU6MvLa10v6u9KK0hxBwKoR cPFAphjaGy0zNg3dGpiTk5vL+m4aeJSd+9MffOimoD7IliU6C2B+4nhpISGamDbbkvJD H45Q== X-Gm-Message-State: AOJu0YwxtflRvJ7OK2BedsQ5AoVOM19n6TgMs4yHqd1mQBWybdD8j4uS cReRDQ6pfS8Qjw7xxB6E+Yjh6QPRfS0cQFRBvCdD92v8dOcsywT8B4xnjzsLt7Bzf+AuoGvaH/K 1wyiWHMzaDDhTfmye2ScBuuppIrp6RrTBHXoSQ3AvGBF03bmvy20= X-Google-Smtp-Source: AGHT+IEydxRn2sFFplYWbN/tKno83jlaxwg+CAacKv4tKj6LqFswCkxAawLnX290zLum7PsJ7qV5t+iqINCK/WnUCEc= X-Received: by 2002:a05:6602:3f07:b0:7d5:df1d:2562 with SMTP id em7-20020a0566023f0700b007d5df1d2562mr5770092iob.14.1712797400019; Wed, 10 Apr 2024 18:03:20 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <24e4529d-0b75-44de-90ef-34de5dfb1c99@wcflabs.de> <278889be-82ab-4827-a9e7-801b5ba2d8f8@app.fastmail.com> In-Reply-To: <278889be-82ab-4827-a9e7-801b5ba2d8f8@app.fastmail.com> Date: Wed, 10 Apr 2024 19:03:09 -0600 Message-ID: Subject: Re: [PHP-DEV] RFC [Discussion]: array_find To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: levi.morrison@datadoghq.com (Levi Morrison) On Sun, Apr 7, 2024 at 8:38=E2=80=AFAM Larry Garfield wrote: > > On Sun, Apr 7, 2024, at 10:20 AM, Joshua R=C3=BCsweg wrote: > > Hi > > I have created an RFC to add the function array_find which returns the > > first element for which a predicate callback returns true. This is a > > function which I missed often. Furthermore this type of function is > > implemented with other programming languages like C++, JavaScript and > > Rust, too. > > You can find the RFC at: > > https://wiki.php.net/rfc/array_find > > Proof of concept implementation is in: > > https://github.com/joshuaruesweg/php-src/commit/9f3fc252b92f534d498e5f1= e6a463e15f45da208 > > I'm looking forward to your feedback. > > Cheers > > Joshua R=C3=BCsweg > > I'm open to this, but two points that I'm sure someone will bring up: > > 1. Should this work on arrays or iterables? This is a long standing limi= tation of PHP. The array operations don't work on iterables, even though w= e've had iterables for 20 years.) > > 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 an= swer, since the question is binary, not true/false. (Those are not the sam= e thing.) I think a small return-mode Enum would make more sense here. IMO, it's better to separate it into two functions because its type is stable without control flow. For instance: // Returns K if $b is true, V otherwise. function array_find( array $array, callable(V, K): bool $callback, bool $b =3D false ) -> K|V; This isn't stable and requires control-flow to understand the type. These are simpler: function array_find( array $array, callable(V, K): bool $callback ) -> V; function array_find_key( array $array, callable(V, K): bool $callback ) -> K; Naming bikeshedding aside, it's better to have types that are inferrable without function-specific knowledge of control flow. It doesn't matter if it's a bool or an enum, it still has problems. Better to just separate them to different functions.