Skip to content
This repository has been archived by the owner on Jul 27, 2020. It is now read-only.

Commit

Permalink
Invoke Clojure syntax highlighting in readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Oct 26, 2012
1 parent 8505ae0 commit af50bb1
Showing 1 changed file with 41 additions and 32 deletions.
73 changes: 41 additions & 32 deletions README.md
Expand Up @@ -30,31 +30,33 @@ Hooke is inspired by Emacs Lisp's defadvice and clojure.test fixtures.


## Usage ## Usage


(use 'robert.hooke) ```clj
(use 'robert.hooke)


(defn examine [x] (defn examine [x]
(println x)) (println x))


(defn microscope (defn microscope
"The keen powers of observation enabled by Robert Hooke allow "The keen powers of observation enabled by Robert Hooke allow
for a closer look at any object!" for a closer look at any object!"
[f x] [f x]
(f (.toUpperCase x))) (f (.toUpperCase x)))


(defn doubler [f & args] (defn doubler [f & args]
(apply f args) (apply f args)
(apply f args)) (apply f args))


(defn telescope [f x] (defn telescope [f x]
(f (apply str (interpose " " x)))) (f (apply str (interpose " " x))))


(add-hook #'examine #'microscope) (add-hook #'examine #'microscope)
(add-hook #'examine #'doubler) (add-hook #'examine #'doubler)
(add-hook #'examine #'telescope) (add-hook #'examine #'telescope)


(examine "something") (examine "something")
> S O M E T H I N G > S O M E T H I N G
> S O M E T H I N G > S O M E T H I N G
```


Hooks are functions that wrap other functions. They receive the Hooks are functions that wrap other functions. They receive the
original function and its arguments as their arguments. Hook original function and its arguments as their arguments. Hook
Expand All @@ -79,34 +81,41 @@ a function, it will be re-added as a hook, but if you use a var it
will be able to detect that it's the same thing across reloads and will be able to detect that it's the same thing across reloads and
avoid duplication. avoid duplication.


(add-hook #'some.ns/target-var #'hook-function) ```clj
(add-hook #'some.ns/target-var #'hook-function)
```


instead of: instead of:


(add-hook #'some.ns/target-var hook-function) ```clj

(add-hook #'some.ns/target-var hook-function)
```


## Bonus Features ## Bonus Features


Most of the time you'll never need more than just add-hook. But Most of the time you'll never need more than just add-hook. But
there's more! there's more!


If you are using Hooke just to add side-effects to a function, it may If you are using Hooke just to add side-effects to a function, it may
be simpler to use the append or prepend macros: be simpler to use the `append` or `prepend` macros:


(prepend print-name ```
(print "The following person is awesome:")) (prepend print-name
(print "The following person is awesome:"))
(print-name "Gilbert K. Chesterton") (print-name "Gilbert K. Chesterton")
> The following person is awesome: > The following person is awesome:
> Gilbert K. Chesterton > Gilbert K. Chesterton
```


You may also run a block of code with the hooks for a given var You may also run a block of code with the hooks for a given var
stripped out: stripped out:


(with-hooks-disabled print-name ```clj
(print-name "Alan Moore")) (with-hooks-disabled print-name
> Alan Moore (print-name "Alan Moore"))
> Alan Moore
```


The `with-scope` macro provides a scope which records any change to hooks during The `with-scope` macro provides a scope which records any change to hooks during
the dynamic scope of its body, and restores hooks to their original state on the dynamic scope of its body, and restores hooks to their original state on
Expand All @@ -126,6 +135,6 @@ functions above:


## License ## License


Copyright © 2010-2012 Phil Hagelberg and Kevin Downey Copyright © 2010-2012 Phil Hagelberg, Kevin Downey, and contributors.


Distributed under the Eclipse Public License, the same as Clojure. Distributed under the Eclipse Public License, the same as Clojure.

0 comments on commit af50bb1

Please sign in to comment.