Git Product home page Git Product logo

lein-shorthand's Introduction

lein-shorthand

A leiningen plugin to create clojure namespaces with short names, so you can easily call utility functions in the REPL using fully qualified symbols.

In the repl, there are functions that you always want to have handy, no matter which namespace you are in. One way of achieving this is to put these functions into a namespace with a short name, like ., so you can refer to them easily. The original idea came from Gary Fredericks, who also wrote dot-slash, which I discovered after writing this. Many thanks to Gary for suggesting lein-shorthand as the name for this plugin.

Usage

Add lein-shorthand to :plugins in the :user profile of ~/.lein/profiles.clj:

{:user
  {…
   :plugins [[com.palletops/lein-shorthand "0.4.0"]]
   …}}

You create shorthand namespaces using the :shorthand project key (again, in the :user profile). For example to define the . namespace with clojure's pprint and alembic's still function and lein macro:

:shorthand {. [clojure.pprint/pprint
               alembic.still/distill
               alembic.still/lein]}

In the repl you will then be able to use (./distill [[clj-http "1.0.0."]]) to add clj-http to your classpath.

You can also rename symbols:

:shorthand {. {pp clojure.pprint/pprint}}

The :shorthand value is a map where the keys are namespace symbols, and the values are either a sequence of fully qualified symbols for vars you want injected into in the namespace, or a map from unqualified symbol, for the name of the function in the target namespace, to the fully qualified symbol of the var it should map to.

By default :shorthand namespaces will eagerly load all the namespaces of vars that you specify, which can increase your REPL start-up time, and increase the number of namespaces that are loaded in your REPL but not used in your projects.

You can make the require of the target namespace happen lazily, on first use, by using the :shorthand-fns and :shorthand-macros keys instead of the :shorthand. Since the vars will not be required until first use, you have to explicitly use the :shorthand-macros key for macros as lein-shorthand has no other way of knowing that it should add :macro metadata to these vars.

If you prefer, you can use :shorthand and add metadata to the symbols that you wish to use lazy require with.

:shorthand {. [clojure.pprint/pprint
               ^:lazy alembic.still/distill
               ^:lazy ^:macro alembic.still/lein]}

How it works

:shorthand defines vars in the target namespace. The vars values are set to the vars resolved by the specified symbols. The namespace of the var is eagerly required when the function is defined. This works with functions, macros and protocol functions.

:shorthand-fns, :shorthand-macros and symbols annotated with :lazy metadata in the :shorthand key, define functions and macros in the target namespace, that resolve the var to inject, and replaces itself with that var. This means that the injected function isn't actually required until it is used. Since the source var is not available when the function is defined, you have to explicitly specify which symbols are macros.

The shothand vars always tracks the target var.

Differences with dot-slash

lein-shorthand does not add anything to your classpath.

dot-slash adds a dependency on potemkin to your project's classpath in order to propagate var updates to the injected vars. lein-shorthand achieves the same effect by using the source var as the value of the injected var, without deref'ing it.

lein-shorthand uses the :injections key and can be composed using multiple profiles, while dot-slash uses [:repl-options :init].

License

Copyright © 2014 Hugo Duncan

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

lein-shorthand's People

Contributors

hugoduncan avatar pbors avatar gfredericks avatar

Watchers

James Cloos avatar Tom Connors avatar

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.