Practice working with Clojure vectors

Vector

A vector in Clojure is an ordered, indexed, immutable collection that allows efficient random access and fast additions at the end. They are immutable.

Vectors are defined using square brackets [] or by using the vector function:


This content originally appeared on DEV Community and was authored by ivan.gavlik

Vector

A vector in Clojure is an ordered, indexed, immutable collection that allows efficient random access and fast additions at the end. They are immutable.

Vectors are defined using square brackets [] or by using the vector function:

;; Creating vectors
[1 2 3 4]          ;; Literal syntax
(vector 1 2 3 4)   ;; Using vector function

Key Characteristics of Vectors:

  • Indexed (O(1) access time): You can retrieve elements using their index.

  • Ordered: Elements maintain the order in which they were inserted.

  • Immutable: Any modification creates a new vector instead of mutating the original.

  • Efficient addition/removal at the end: Adding elements to the end is fast (conj).

Usage & Operations:

Access Elements by Index

(def v [10 20 30 40])
(nth v 2)      ;; => 30
(get v 1)      ;; => 20
(v 3)          ;; => 40 (Vectors can be used as functions for indexing)

Adding Elements

(conj [1 2 3] 4) ;; => [1 2 3 4] (adds to the end)

Updating Elements

(assoc [1 2 3] 1 99) ;; => [1 99 3] (Replaces index 1 with 99)

Removing Elements

Vectors do not have direct remove operations, but you can filter or use subvec:

(vec (remove #(= % 2) [1 2 3])) ;; => [1 3] (Removes 2)
(subvec [1 2 3 4] 1 3)          ;; => [2 3] (Slices vector from index 1 to 2)

Iterating Over a Vector

(map inc [1 2 3]) ;; => (2 3 4)
(filter even? [1 2 3 4]) ;; => (2 4)

Sorting a Vector

(sort [3 1 4 2])  ;; => (1 2 3 4)
(sort-by count ["apple" "kiwi" "banana"]) ;; => ("kiwi" "apple" "banana") (Sort by length)

Task: Managing a Task List with Vector

Scenario:

You are building a simple task management system that stores tasks in a vector. Each task is represented as a map with the following keys:

  • :id (unique task identifier)
  • :title (task description)
  • :priority (one of :low, :medium, :high)
  • :completed? (boolean indicating if the task is done)

Goal:
Implement functions that allow you to manage tasks in the system:

  • Initialize Task List – Create a vector of sample tasks.

  • Add a Task – Insert a new task into the task list.

  • Remove a Task – Remove a task by its :id.

  • Mark a Task as Completed – Update a task’s :completed? status to true.

  • List All Pending Tasks – Return only tasks where :completed? is false.

  • Find Tasks by Priority – Retrieve all tasks with a specific priority (:low, :medium, or :high).

  • Sort Tasks by Priority – Sort the vector so :high priority tasks come first, followed by :medium, then :low.

  • Get Next Task – Return the highest-priority pending task that has not been completed.

Solution

;; Managing a Task

;; Initialize Task List
(defn init-task-list []
  (hash-map {})
  )

(defn valid-priority [priority]
  (or (= :low priority) (= :medium priority) (= :high priority))
  )


(defn create-task [id title desc priority]
  (when (valid-priority priority)
    {:id id :title title :description desc :priority priority :completed? false}
    )
  )

(defn task-not-exist? [task-list title]
  (= 0 (count (filter #(= (:title %)  title) task-list))
  ))

;; Add a Task
(defn add-task [task-list title desc priority]
  (when (task-not-exist? task-list title)
    (let [last-task-id (:id (last task-list) 0)
          next-task-id (inc last-task-id)
          task (create-task next-task-id title desc priority)]
      (conj task-list task)
      )
    )
  )

;; Remove a Task
(defn remove-task [task-list task-id]
  (remove #(= (:id %) task-id) task-list)
  )

;; Mark a Task as Completed
(defn mark-as-completed [task-list task-id]
  (let [task (first (filter #(= (% :id) task-id) task-list))
        index-of-task (index-of task-list task)]
    (when (not (nil? task))
      (assoc task-list index-of-task (assoc task :completed? true))
      )
    )
  )

;; List All Pending Tasks
(defn pending-tasks [task-list]
  (filter #(not (:completed? %)) task-list)
  )


;; Find Tasks by Priority
(defn filter-by-priority [task-list priority]
  (when (valid-priority priority)
    (filter #(= (:priority %) priority) task-list)
    )
  )

(defn priority-count [priority]
  (cond
    (= priority :high) 3
    (= priority :medium) 2
    (= priority :low) 2
    :else 0
    )
  )

(defn priority-higher-comparator [el1 el2] ()
  (let [priority1 (priority-count (el1 :priority))
        priority2 (priority-count (el2 :priority))]
    (compare priority2 priority1)
    )
  )

;; Sort Tasks by Priority
(defn sort-by-priority [task-list]
  (sort priority-higher-comparator task-list)
  )

;; Get Next Task
(defn next-task [task-list]
  (sort-by-priority (pending-tasks task-list))
  )


This content originally appeared on DEV Community and was authored by ivan.gavlik


Print Share Comment Cite Upload Translate Updates
APA

ivan.gavlik | Sciencx (2025-03-23T20:39:23+00:00) Practice working with Clojure vectors. Retrieved from https://www.scien.cx/2025/03/23/practice-working-with-clojure-vectors/

MLA
" » Practice working with Clojure vectors." ivan.gavlik | Sciencx - Sunday March 23, 2025, https://www.scien.cx/2025/03/23/practice-working-with-clojure-vectors/
HARVARD
ivan.gavlik | Sciencx Sunday March 23, 2025 » Practice working with Clojure vectors., viewed ,<https://www.scien.cx/2025/03/23/practice-working-with-clojure-vectors/>
VANCOUVER
ivan.gavlik | Sciencx - » Practice working with Clojure vectors. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/23/practice-working-with-clojure-vectors/
CHICAGO
" » Practice working with Clojure vectors." ivan.gavlik | Sciencx - Accessed . https://www.scien.cx/2025/03/23/practice-working-with-clojure-vectors/
IEEE
" » Practice working with Clojure vectors." ivan.gavlik | Sciencx [Online]. Available: https://www.scien.cx/2025/03/23/practice-working-with-clojure-vectors/. [Accessed: ]
rf:citation
» Practice working with Clojure vectors | ivan.gavlik | Sciencx | https://www.scien.cx/2025/03/23/practice-working-with-clojure-vectors/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.