8000 Add frequencies fn by athos · Pull Request #7 · athos/Postmortem · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add frequencies fn #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ A tiny value-oriented debugging tool for Clojure(Script), powered by transducers
- [Basic usage](#basic-usage)
- [`spy>>` / `log-for`](#spy--log-for)
- [`reset-key!` / `completed?` / `keys`](#reset-key--completed--keys)
- [`logs` / `reset!`](#logs--reset)
- [`logs` / `frequencies` / `reset!`](#logs--frequencies--reset)
- [`spy>`](#spy)
- [`dump`](#dump)
- [Integration with transducers](#integration-with-transducers)
Expand Down Expand Up @@ -220,7 +220,7 @@ any log entry, `keys` suits your desire:
;=> false
```

#### `logs` / `reset!`
#### `logs` / `frequencies` / `reset!`

You can also logs some data to more than one log entries at once.
In such a case, `logs` is more useful to look into the whole log data
Expand All @@ -243,8 +243,34 @@ than just calling `log-for` for each log entry:
; :sum [0 1 3 6 10 15]}
```

Similarly, `reset!` is useful to clear the whole log data at a time, rathar than
clearing each individual log entry one by one calling `reset-key!`:
Alternatively, `frequencies` helps you grasp how many log items have been
stored so far for each log entry key, without seeing the actual log data:

```clojure
(defn sum [n]
(loop [i 0 sum 0]
(pm/spy>> :i i)
(if (> i n)
sum
(recur (inc i)
(pm/spy>> :sum (+ i sum))))))

(sum 5) ;=> 15

(pm/frequencies)
;=> {:i 7 :sum 6}

;; As compared to:
;; (pm/logs)
;; => {:i [0 1 2 3 4 5 6],
; :sum [0 1 3 6 10 15]}
```

Note that once you call `frequencies`, all the log entries will be
*completed*, as with the `logs` fn.

Analogous to `logs`, `reset!` is useful to clear the whole log data at a time,
rathar than clearing each individual log entry one by one calling `reset-key!`:

```clojure
(pm/logs)
Expand Down
13 changes: 12 additions & 1 deletion src/postmortem/core.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns postmortem.core
(:refer-clojure :exclude [keys reset!])
(:refer-clojure :exclude [frequencies keys reset!])
(:require [clojure.core :as c]
#?(:clj [net.cgrand.macrovich :as macros])
[postmortem.protocols :as proto]
Expand Down Expand Up @@ -130,6 +130,17 @@
(assert (session? session) "Invalid session specified")
(set (proto/-keys session))))

(defn frequencies
"Returns a frequency map, which is a map of log entry key to a number
that indicates how many log items have been logged for the log entry.
If session is omitted, frequencies for the current session will be
returned."
([] (frequencies (current-session)))
([session]
(assert (session? session) "Invalid session specified")
(->> (logs* session)
(into {} (map (fn [[k xs]] [k (count xs)]))))))

(defn reset-key!
"Resets log entry for the specified key.
If session is omitted, the entries in the current session will be reset."
Expand Down
4 changes: 3 additions & 1 deletion test/postmortem/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
{:n 0 :a 5 :b 8}]}
(pm/logs)))
(is (every? pm/completed? [:add :add-result `fib]))
(is (pm/frequencies) {:add 5, :add-result 6, `fib 6})

(pm/reset-key! :add-result)
(is (= #{:add `fib} (pm/keys)))
Expand All @@ -83,7 +84,8 @@

(pm/reset-keys! #{:add `fib})
(is (= #{} (pm/keys)))
(is (= {} (pm/logs))))
(is (= {} (pm/logs)))
(is (= {} (pm/frequencies))))

;; Assert this function definition compiles
;; cf. https://github.com/athos/postmortem/issues/2
Expand Down
0