From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Patrick McAllister Newsgroups: gmane.emacs.help Subject: Re: count char in string [The Emacs Challenge Competition - round 2, with fallout] Date: Thu, 29 Oct 2020 14:53:12 +0100 Message-ID: <875z6tqh10.fsf@pmatoe> References: <87r1pj41wa.fsf@zoho.eu> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19076"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.4.13; emacs 26.3 To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Oct 29 14:54:33 2020 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kY8Nt-0004sG-EX for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 29 Oct 2020 14:54:33 +0100 Original-Received: from localhost ([::1]:46594 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kY8Ns-0005Bc-GR for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 29 Oct 2020 09:54:32 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43790) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kY8Mn-0004Yb-R2 for help-gnu-emacs@gnu.org; Thu, 29 Oct 2020 09:53:25 -0400 Original-Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]:35850) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kY8Ml-0001TH-M0 for help-gnu-emacs@gnu.org; Thu, 29 Oct 2020 09:53:25 -0400 Original-Received: by mail-wm1-x32a.google.com with SMTP id e2so2508615wme.1 for ; Thu, 29 Oct 2020 06:53:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rdorte-org.20150623.gappssmtp.com; s=20150623; h=references:user-agent:from:to:subject:message-id:in-reply-to:date :mime-version; bh=6wdt6rqpXB00tRA5o1tTvUN1fvBqBElEgHHsEI1+ZYE=; b=djnqfpjuW9EtajzBay+wSSJaUUpYP6QVQpfLNM+vU0KkVen2dSoku+8JjX+Ev2aJMN yrtVkenCrnarN+2CppjE7Rb2Dp3ter41Q0HpSKGEopqUi2LF+mOzR0MmwtyU14D9Zs2J XEAmHFSpq/Xhqc7eX49P6PelRvua1GABYLoOxGFrcAf1iKcB9LVp/YEcPsJC6K1cfFJ2 5fDklDS37dM4eo7+2y7UNpxfDUMXfMAVLCcAMHDqZj/Qx1mpf/fWYCLNU0tPX/amZLJw BRwn3M6gQ1kc/W3+nNx34dgLkI08zV+7pNSchEVpqHb7ycoip+ry8CfOtWDGOzqcAKX4 JDbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:subject:message-id :in-reply-to:date:mime-version; bh=6wdt6rqpXB00tRA5o1tTvUN1fvBqBElEgHHsEI1+ZYE=; b=OakyM1pH3jJh8qpowD2tVKB/LGiauCgj/GoGX6L+J5LUu0IywPaWek/QQ2PTAFqw77 xieuQbO0801ubOY72311JmtaLDfaO8D67VS0HwDlW2TMCs7cEe+4HWzo4smZuIafYjN4 lT1RzZboWW7FToPzbuVPQY9BiB0eShckFzuvPSpEPLBAib8/FEb8wqHfYhQY0UqFLQii 5oDPlkWNr1Uj+xmY7NdADmsbbPUn30CJSDuYoJATdoR9TQocna7kkLjTsIjRVXCWIkvv ewP6tsQ5I2EtpVD9bS8+5JFxKC0aIHtczumb+oHf9a7A8LJ7kVHH9QRqkZ0H4/nvCrbs 4Smw== X-Gm-Message-State: AOAM532wqRjpJi1sScJ4QqX2MWrnZHGahgtqQWn+BDtTsOX852q12nbq mj6p92qBuAidMJ1AKCEXra5O0E7S3rVlasFK X-Google-Smtp-Source: ABdhPJxXjbVGHDYUwbX6P0kNuc7/n5TeBuaNhiVgejrL2xBI4he2V8NXW1eezonm9RDIYeEt9qmgfg== X-Received: by 2002:a1c:c2c5:: with SMTP id s188mr4712774wmf.174.1603979601485; Thu, 29 Oct 2020 06:53:21 -0700 (PDT) Original-Received: from pmatoe ([193.170.91.5]) by smtp.gmail.com with ESMTPSA id h4sm5197785wrp.52.2020.10.29.06.53.19 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 29 Oct 2020 06:53:20 -0700 (PDT) In-reply-to: <87r1pj41wa.fsf@zoho.eu> Received-SPF: none client-ip=2a00:1450:4864:20::32a; envelope-from=pma@rdorte.org; helo=mail-wm1-x32a.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:124795 Archived-At: On Wed, Oct 28 2020, Emanuel Berg via Users list for the GNU Emacs text editor wrote: > round 2: > > Very simple: we are looking for a function that returns the number of > occurrences of a char in a string. > > Emanuel Berg's solution: > > (require 'cl-lib) > (defun count-char-in-string (the-char str) > (let ((c (if (characterp the-char) the-char (string-to-char the-char))) > (cs (string-to-list str)) ) > (cl-count c cs) )) > ;; (count-char-in-string ?a "Emacs skills kills") ; 1 I had this lying around: (defun count-char-in-string (char string) "Count CHARS in STRING (after normalizing it)." (length (cl-remove-if-not (lambda (x) (= char x)) (append (ucs-normalize-NFKC-string string) nil)))) -- Patrick