-
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.
- Loading branch information
1 parent
19c2e1c
commit 3986354
Showing
1 changed file
with
16 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] | ||
[clojure.set :as set])) | ||
|
||
;; SWiss ARmy Knife - Your everyday clojure toolbelt! | ||
;; Copyright 2024 - Stan Verberkt ([email protected]) | ||
|
@@ -71,6 +72,20 @@ | |
predicate (if ns #{ns} nil?)] | ||
(filter-keys map (comp predicate namespace))))) | ||
|
||
;; TODO: Add tests | ||
(defn optional-args | ||
"Returns a function that selects and renames keys, and adds defaults for sequentialargs (as destructured by &). Supply a props map containing permitted input keys as keys, and a map with :default & :rename as val. | ||
`((optional-args {:name {:rename :person/name :default "Unnamed person"} :from {:rename :person/from :default "Nowhere"}}) {:name "Me"}) => #:person{:name "Me" :from "Nowhere"}`" | ||
[props] | ||
(let [renames (map-vals :rename props) | ||
defaults (map-vals :default props)] | ||
(fn optional-args* [args] | ||
(let [m (apply hash-map args)] | ||
(as-> m m' | ||
(select-keys m' (keys props)) | ||
(merge defaults m') | ||
(set/rename-keys m' renames)))))) | ||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; Try and catch | ||
|
||
|