Skip to content

Instantly share code, notes, and snippets.

@PEZ
Last active December 30, 2023 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PEZ/d55eddc37d7a08a3440748ddb75c7ec4 to your computer and use it in GitHub Desktop.
Save PEZ/d55eddc37d7a08a3440748ddb75c7ec4 to your computer and use it in GitHub Desktop.
Count a Sequence – Rich 4Clojure Problem 22 – See: https://github.com/PEZ/rich4clojure
(ns rich4clojure.easy.problem-022
(:require [hyperfiddle.rcf :refer [tests]]))
;; = Count a Sequence =
;; By 4Clojure user: dbyrne
;; Difficulty: Easy
;; Tags: [seqs core-functions]
;;
;; Write a function which returns the total number of
;; elements in a sequence.
(def restricted [count])
(def __ :tests-will-fail)
(comment
)
(tests
(__ '(1 2 3 3 1)) := 5
(__ "Hello World") := 11
(__ [[1 2] [3 4] [5 6]]) := 3
(__ '(13)) := 1
(__ '(:a :b :c)) := 3)
;; To participate, fork:
;; https://github.com/PEZ/rich4clojure
;; Post your solution below, please!
@szalai1
Copy link

szalai1 commented Aug 20, 2021

(fn [s]
    (loop [ss s
           n 0]
      (if (empty? ss)
        n
        (recur (rest ss) (inc n)))))

@evedes
Copy link

evedes commented Sep 10, 2021

#(count %)

@AndrewNelis
Copy link

#(reduce (fn [acc _] (inc acc)) 0 %)

@StanTheBear
Copy link

StanTheBear commented Aug 7, 2022

(fn [sq] (last (for [ind (range)
                             :while (get (vec sq) ind)]
                       (inc ind)))))

@ieugen
Copy link

ieugen commented Sep 28, 2022

(defn my-count [coll]
  (let [counter (fn [acc _val]
                  (inc acc))]
    (reduce counter 0 coll)))

Copy link

ghost commented Oct 5, 2023

(fn [coll] 
    (loop [x coll
           n 0]
      (if-not (seq x)
        n
        (recur (rest x) (+ n 1)))))

@aaroncm
Copy link

aaroncm commented Dec 29, 2023

  (def __ #(apply + (for [_ %] 1)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment