-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement and test filter-keys and select-namespaced
- Loading branch information
1 parent
1907c13
commit 754d4d5
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
(ns swark.core | ||
(:require [clojure.string :as str])) | ||
(:require [clojure.string :as str]) | ||
(:import [clojure.lang Named])) | ||
|
||
;; SWiss ARmy Knife - Your everyday clojure toolbelt! | ||
;; Copyright 2024 - Stan Verberkt ([email protected]) | ||
|
@@ -32,6 +33,30 @@ | |
(map (juxt key (comp f val))) | ||
(into {})))) | ||
|
||
(defn filter-keys | ||
[map pred] | ||
{:added "0.1.3" | ||
:arglists '([map pred]) | ||
:doc "Returns a map containing only those entries in map whose key return logical true on evaluation of (pred key)." | ||
:static true} | ||
(cond->> map | ||
pred (filter (comp pred key)) | ||
map seq | ||
:always (into {}))) | ||
|
||
(defn select-namespaced | ||
{:added "0.1.3" | ||
:arglist '([map] [map ns]) | ||
:doc "Returns a map containing only those entries in map whose keys' namespace match ns. When ns is nil, returns a map containing only non-namespaced keys." | ||
:static true} | ||
([map] | ||
(select-namespaced map nil)) | ||
([map ns] | ||
(-> map map? assert) | ||
(when-not (string? ns) (some->> ns (instance? Named) assert)) | ||
(let [predicate (if ns #{(name ns)} nil?)] | ||
(filter-keys map (comp predicate namespace))))) | ||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; Regarding strings | ||
|
||
|
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