Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101735 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 30985 invoked from network); 30 Jan 2018 20:24:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Jan 2018 20:24:15 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.195 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.128.195 mail-wr0-f195.google.com Received: from [209.85.128.195] ([209.85.128.195:44086] helo=mail-wr0-f195.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E3/04-24062-C64D07A5 for ; Tue, 30 Jan 2018 15:24:13 -0500 Received: by mail-wr0-f195.google.com with SMTP id v31so12589636wrc.11 for ; Tue, 30 Jan 2018 12:24:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=DKLs2MHRMY7NCgQtDmkk6iMs1bSfcrlYZC7jKxY9hLc=; b=oKomOAChpxGdEysuXRlUGTg2ndDbx19SwfZaJ262bZNGCKsJgmlnA+an5oCIg+YJFk 46LndaHIUyzeJpuVBkIaqAFkGRxpjWIqp0I9QVI+7tvnOJkJQOFsecfQ9NErK3rKnUWB ijIe2tBb62OUgGhP6c0gODAtyXajNm/3eVgxXQK8KxJmiEY+mmzo+3AXZKeWXIQtKb+I YMXIOph9E7ac6RiCr35Yqz5wODbKSbmcJc/cvSmTU47r2jV+E6/qREgrVoshpBtkQ1YN I+CXBc/pd5IQS9DATPqqwXvMML6TyYBKUQETwzk/pofDOIF3eV1XYXFXYhwC7OLwZk2w y6qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=DKLs2MHRMY7NCgQtDmkk6iMs1bSfcrlYZC7jKxY9hLc=; b=r9r9eYth18XzMGjACuqFo7IDigy0uDBB0RxhJCm/v+UWEjKgfBQu2vkF8eNObh/Clx kEHFLmNONpuCcnKMAhQzoo2MDu3vMJ3c3qdM9HY1P0yisZzGHUyXN86StCqwgw2dn25m MPrml5vzxZPvb1pXRH8ZYSb8cD/B57j4XMvXVkaLuclOAEtxoRmP50ITIqxAyWmVleNV Ah1vqoDSpsfJxBRbFXZIb8xG2LbD3wDuLM8nZhSBpgubgIkl1DSaGfBZIAh0eHdSBnoa PfDWBOdyJ8/KmdOx2PtINhOJ9/hQvOGVTV2m/OAow3wRIdlMuIFP5SVOPUnBV4YnIWLO ec0g== X-Gm-Message-State: AKwxyte8ZHJwezTjeaHEz1Ez6KXJikU9jRruLiNsjM9ES8ikJGWkzIje 7RoimwKAmRbUEclbsOslo/PRV7tHxzSpuLfbgZtfHN3z X-Google-Smtp-Source: AH8x226n7k13jD1kGpqH3NR3Ybu8e0EYcph6ZM7yqwZPBCdTrqV3k9gkpvlnbDXXw6vftNnR/cafBw+LPE5PLVjlTaI= X-Received: by 10.223.196.6 with SMTP id v6mr10870441wrf.182.1517343849782; Tue, 30 Jan 2018 12:24:09 -0800 (PST) MIME-Version: 1.0 Sender: morrison.levi@gmail.com Received: by 10.28.56.3 with HTTP; Tue, 30 Jan 2018 12:24:08 -0800 (PST) In-Reply-To: <28100667.gh41XH5Xuc@vulcan> References: <4026445.Wp3pesFrPl@vulcan> <28100667.gh41XH5Xuc@vulcan> Date: Tue, 30 Jan 2018 13:24:08 -0700 X-Google-Sender-Auth: UFsU2Bxevz6-cpHsk9pioeEWnjY Message-ID: To: Larry Garfield Cc: internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV][RFC][DISCUSSION] Collection Inspection From: levim@php.net (Levi Morrison) On Tue, Jan 30, 2018 at 11:13 AM, Larry Garfield wrote: > On Monday, January 29, 2018 6:46:10 PM CST Michael Morris wrote: >> On Mon, Jan 29, 2018 at 6:16 PM, Larry Garfield >> >> wrote: >> > Really, these functions would be useful only on arrays, period. To allow >> > them >> > on anything else is just dangerous, and on other iterables there are >> > better, >> > more robust approaches (as discussed elsewhere in this thread). >> > >> > As you've demonstrated they're also quite compact and effective to do in >> > user- >> > space, so unless there's a massive performance difference of moving them >> > to C >> > they don't seem all that appropriate to add to the language directly. >> > >> > --Larry Garfield >> >> Didn't you personally raise the issue of hard dependencies doing this sort >> of functionality creates? Implementable in userland or not, this is pretty >> low level functionality. > > I don't recall doing so in this thread, but I most likely have on some other > issue. It's the sort of comment that I would make. :-) > > However, that's for a very commonly used function where just inlining it is > prohibitive. As you've demonstrated below, this functionality is easily a one > liner, and it's a one-liner in an assert statement most likely. > >> Hmm.. If limited to arrays only then array_filter with count is pretty >> close to what we want. >> >> assert(count(array_filter($a, is_string)) === count($a)); >> >> That's not optimal though - granted assert code doesn't *have* to be >> optimal, but that's still a wordy construction. > > Personally I think that's fine, and doesn't need a language-level utility > function to wrap it any further. > > (Yes, that's a subjective metric. So are most statements about what > should[n't] be included in the language itself. Others are welcome to > disagree, but this falls below my threshold of necessity.) > > --Larry Garfield I agree with this sentiment from Larry. However I do think there is a function we could add: function every(callable $predicate, traversable $input): bool { foreach ($input as $value) { if (!$predicate($value)) { return false; } } return true; } In which case the code would become: assert(every('is_string', $input)); Of course, when passing a traversable the caller must to be careful as it will be consumed. If they don't want that to happen they can convert it to an array (possibly with iterator_to_array). The reason this "every" function is better than "array_test" is because it is more general and works with more things than just types. The name "every" I borrowed from Clojure, but it's known by many names; in Java it is known as "allMatch".