Giter VIP home page Giter VIP logo

hashtag's Introduction

hashtag

hashtag is derived from a fork of weavejester/hashp, and allows definition of "tagged literal" functions to aid in debugging. If "hashp (ab)uses data readers to make it easier to get debugging data", hashtag beats them with a stick to achieve the same, but with more flexibility, inspired by this hashp issue.

Usage

NOTE: pre-alpha, not yet published to clojars. Please give it a try by either cloning locally or using deps.edn and :git/url. Expect breakage for now.

Use the defhashtag macro to define your own debugging hashtag. defhashtag requires an (optionally namespaced) name for your hashtag, and a single-argument handler function. The handler will be passed a map with spec :hashtag.core/debug-data.

(ns hashpp
  (:require [hashtag.core :as ht :refer [defhashtag]]
            [clojure.pprint :refer [pprint]]))

(defhashtag pp pprint)

This will define and register the reader tag #pp, which can then be used as

(defn f
  [x]
  (let [a #pp (inc x)
        b #pp (* 2 a)]
    (dec b)))

(defn g
  [x]
  (* 3 #pp (f x)))

user=> (g 5)
{:result 6, :form (inc x)}
{:result 12, :form (* 2 a)}
{:result 11, :form (f x)}
33

Faster to type than (pprint ...), and trivially removed with find/replace. Note that you can use any single-argument function as a handler, so pprint could be replaced by tap>, your own custom logic, etc.

defhashtag also accepts some keyword options.

(defhashtag pp/locals pprint :locals? true)

If we replace #pp with #pp/locals in the defintions of f and g, we get the following output:

user=> (g 5)
{:result 6, :form (inc x), :locals {:x 5}}
{:result 12, :form (* 2 a), :locals {:x 5, :a 6}}
{:result 11, :form (f x), :locals {:x 5}}
33

Setting the :locals? option to true adds a :locals attribute to the debug map, containing a map of keywordized local binding names to their current values (an idea borrowed from athos/postmortem).

The :stacktrace-tx allows specification of a transducer to process a stacktrace sequence as defined by mmcgrana/clj-stacktrace.

(def my-stacktrace (comp (filter :clojure)
                         (filter #(= "hashpp" (:ns %)))
                         (map #(select-keys % [:fn :line]))))
(defhashtag pp/myst pprint :locals? true :stacktrace-tx my-stacktrace)

Using #pp/myst in f and g yields:

user=> (g 5)
{:result 6,
 :form (inc x),
 :locals {:x 5},
 :stacktrace
 ({:fn "f", :line 18}
  {:fn "f", :line 16}
  {:fn "g", :line 24}
  {:fn "g", :line 22}
  {:fn "eval7214", :line 26}
  {:fn "eval7214", :line 26})}
{:result 12,
 :form (* 2 a),
 :locals {:x 5, :a 6},
 :stacktrace
 ({:fn "f", :line 18}
  {:fn "f", :line 16}
  {:fn "g", :line 24}
  {:fn "g", :line 22}
  {:fn "eval7214", :line 26}
  {:fn "eval7214", :line 26})}
{:result 11,
 :form (f x),
 :locals {:x 5},
 :stacktrace
 ({:fn "g", :line 24}
  {:fn "g", :line 22}
  {:fn "eval7214", :line 26}
  {:fn "eval7214", :line 26})}

hashtag.core contains some predefined stacktrace transducers:

  • current-frame - takes only the current stack frame for the instrumented function.
  • clojure-frames - stack frames for Clojure code.
  • all-frames - full stack trace.

Examples

Run clj -A:examples to work with the code in the examples folder. Examples include an implementation of the functionality of hashp using hashtag.

License

Copyright © 2019 Dave Dixon, James Reeves

Released under the MIT license.

hashtag's People

Contributors

dave-dixon-ck avatar weavejester avatar sparkofreason avatar

Stargazers

Darong Mean avatar Luke Snape avatar Siddharth Jain avatar Donavan-Ross Costaras avatar Daniel Kraus avatar Tom Connors avatar dantheobserver avatar Jochen Bedersdorfer avatar Sergey Toropenko avatar Vincent Cantin avatar Oliver George avatar  avatar Michael Salihi avatar Fed Reggiardo avatar

Watchers

James Cloos avatar  avatar

Forkers

darongmean

hashtag's Issues

Loading hashtag automatically in the REPL?

I'm a hobbyist and started messing with #hashp to visualise what's happening in functions. I'm trying out hashtag as it can show local variables.

But, I'm struggling to automate having it in the REPL, and I'm not sure if it's a bug or it's my total lack of understanding of how data reader and macros work.

I've got a user.clj with the following:

    ;; in my ns requirements
     [sparkofreason.hashtag.core :as ht :refer [defhashtag]]
     [puget.printer :as puget]
     [puget.color.ansi :as color]

    (defhashtag pp pprint)

When I start the REPL, it says #pp isn't there:

user=> (take 20 (repeat #pp (+ 1 2 3)))
             java.lang.RuntimeException: No reader function for tag pp
clojure.lang.LispReader$ReaderException: java.lang.RuntimeException: No reader function for tag pp

If I run (defhashtag pp pprint) then it runs fine.

I can see in hashp that it loads a data_reader (https://github.com/weavejester/hashp/blob/master/src/data_readers.clj), and found some references to this on clojure.org. In the root of the project I added a data_readers.clj and added:

{pp user/pp*}

But that's not working. I found a note on https://clojuredocs.org/clojure.core/*data-readers* that "Reader tags without namespace qualifiers are reserved for Clojure" but I can't get it to work on any variation.

Is there a way to automate adding the data reader?

Consider removing stacktrace support

hashtag currently provides a mechanism to process and return the stacktrace.
However, unlike local bindings, stacktrace is available at runtime
(vs. compile-time), and so can be grabbed by a custom handler function.
Benefits:

  • Removes the only dependency (clj-tacktrace) from hashtag
  • Allows handler authors to process stacktraces as required, based
    on the environment, other tooling, whatever, rather than forcing
    default choices (e.g. dropping the first 3 frames by default.)
  • Simplifies extension of hashtag to ClojureScript (#1).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.