Last active
December 30, 2023 03:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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
commented
Aug 20, 2021
•
#(count %)
#(reduce (fn [acc _] (inc acc)) 0 %)
(fn [sq] (last (for [ind (range)
:while (get (vec sq) ind)]
(inc ind)))))
(defn my-count [coll]
(let [counter (fn [acc _val]
(inc acc))]
(reduce counter 0 coll)))
(fn [coll]
(loop [x coll
n 0]
(if-not (seq x)
n
(recur (rest x) (+ n 1)))))
(def __ #(apply + (for [_ %] 1)))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment