Skip to content

Commit

Permalink
Extend readme, filter falsey keyed values in key-by, reloaded repl
Browse files Browse the repository at this point in the history
  • Loading branch information
verberktstan committed Jan 21, 2024
1 parent 37a1e9e commit 796e4ea
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
### Swark
# Swark

SWiss ARmy Knife - Your everyday clojure toolbelt!

https://clojars.org/org.clojars.stanv/swark

## Basic usage

### Clojure CLI/deps.edn

Add this dependency to the :deps map in deps.edn
`org.clojars.stanv/swark {:mvn/version "0.1.0"}`

Require swark.core in your ns form:
`(:require [swark.core :as swark])`

Then you can use the Swark utility functions:
`(swark/key-by :id [{:id 1 :name "one"} {:id 2 :name "two"}])`

## Development

Start a repl with `clojure -M:repl/reloaded`

Create an uberjar with `clj -X:uberjar :jar swark-x.x.x.jar` (replacing x's with version)

Install Swark locally with `clj -X:install`

## License

Swark by Stan Verberkt is marked with CC0 1.0 Universal
19 changes: 18 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
{:aliases
{:deploy
{:repl/reloaded
{:extra-deps {nrepl/nrepl {:mvn/version "1.0.0"}
cider/cider-nrepl {:mvn/version "0.28.7"}
com.bhauman/rebel-readline {:mvn/version "0.1.4"}
djblue/portal {:mvn/version "0.34.2"}
org.clojure/tools.namespace {:mvn/version "1.3.0"}
org.clojure/tools.deps.alpha {:git/url "https://github.com/clojure/tools.deps.alpha"
:git/sha "e4fb92eef724fa39e29b39cc2b1a850567d490dd"}
org.slf4j/slf4j-nop {:mvn/version "2.0.5"}
lambdaisland/kaocha {:mvn/version "1.71.1119"}
org.clojure/test.check {:mvn/version "1.1.1"}
ring/ring-mock {:mvn/version "0.4.0"}}
:main-opts ["-m" "nrepl.cmdline"
"--middleware" "[cider.nrepl/cider-middleware]"
"--interactive"
"-f" "rebel-readline.main/-main"]}

:deploy
{:extra-deps {slipset/deps-deploy {:mvn/version "0.2.2"}}
:exec-fn deps-deploy.deps-deploy/deploy
:exec-args {:installer :remote
Expand Down
5 changes: 4 additions & 1 deletion src/swark/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
;; Regarding collections

(defn key-by
"Returns a map with all items in collection `coll` keyed by the value returned by `(f item)`"
"Returns a map with all items in collection `coll` keyed by the value returned by `(f item)`
When `(f item)` returns a falsey value, it is NOT included in the resulting map."
[f coll]
(when coll
(-> f ifn? assert)
(-> coll coll? assert)
(->> coll
(map (juxt f identity))
(filter key)
(into {}))))

(comment
(key-by :id [{:id 0 :name "ab"} {:id 1 :name "cd"}])
(key-by nil [{:id 0 :name "ab"} {:id 1 :name "cd"}])
(key-by :id nil)
(key-by (partial reduce +) [[1 1] [1 2] [2 3 5] [3 5 8 13]])
(key-by :user/id [#:user{:id 12 :name "u12"} #:user{:id 23 :name "u23"} {:id 34 :name "not-included!"}])
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
3 changes: 0 additions & 3 deletions uberjar.sh

This file was deleted.

0 comments on commit 796e4ea

Please sign in to comment.