• This repository has been archived on 28/Jul/2020
  • Stars
    star
    357
  • Rank 115,553 (Top 3 %)
  • Language
    Clojure
  • License
    Other
  • Created almost 14 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Hooke your Clojure functions!

This place is not a place of honor... no highly esteemed deed is commemorated here... nothing valued is here.

This project is no longer maintained, and you are strongly advised against using it. In every case where it was adopted long-term in a codebase, it created more problems than it solved, causing situations that were difficult to debug and reason about. If you wish to create an extension mechanism for your code, it's recommended to develop something more structured that facilitates debugging and understanding.

This code is best left shunned and unused.

Robert Hooke

Robert Hooke provides a flexible, composable mechanism by which you can extend behaviour of functions after they've been defined. It's named after Robert Hooke FRS, a founding member of the Royal Society who made many important discoveries in the fields of Gravitation, Microscopy, and Astronomy.

Hooks can change the behaviour of the functions they wrap in many ways:

  • binding
  • conditional execution (may decide not to continue or decide to call a different function in some circumstances)
  • modify arguments
  • add side effects
  • return different value

Hooke is inspired by Emacs Lisp's defadvice and clojure.test fixtures.

Usage

(use 'robert.hooke)

(defn examine [x]
  (println x))

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

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

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

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

(examine "something")
> 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 original function and its arguments as their arguments. Hook functions can wrap the target functions in binding, change the argument list, only run the target functions conditionally, or all sorts of other stuff.

Technically the first argument to a hook function is not always the target function; if there is more than one hook then the first hook will receive a function that is a composition of the remaining hooks. (Dare I say a continuation?) But when you're writing hooks, you should act as if it is the target function.

Adding hooks to a defmulti is discouraged as it will make it impossible to add further methods. Hooks are meant to extend functions you don't control; if you own the target function there are obviously better ways to change its behaviour.

When adding hooks it's best to use vars instead of raw functions in order to allow the code to be reloaded interactively. If you recompile 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 avoid duplication.

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

instead of:

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

Bonus Features

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

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

(prepend print-name
  (print "The following person is awesome:"))

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

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

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

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 exit of the scope. Note that all threads share the scope. Using the example functions above:

(examine "something")
> something

(with-scope
  (add-hook #'examine #'microscope)
  (examine "something"))
> SOMETHING

(examine "something")
> something

License

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

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

More Repositories

1

leiningen

Moved to Codeberg; this is a convenience mirror
Clojure
7,284
star
2

emacs-starter-kit

[ARCHIVED] this is ancient history
2,869
star
3

slamhound

Slamhound rips your namespace form apart and reconstructs it.
Clojure
474
star
4

grenchman

Sorry about the name
OCaml
218
star
5

mire

Mire is a simple MUD written in Clojure
Shell
170
star
6

syme

Instant collaboration on GitHub projects.
Clojure
147
star
7

atreus-firmware

Firmware for the Atreus keyboard
Emacs Lisp
91
star
8

serializable-fn

Serializable functions in Clojure
Clojure
86
star
9

clojure-http-client

An HTTP client for Clojure (DEPRECATED)
Clojure
84
star
10

Garrett

A playground for Android Mirah development
Mirah
84
star
11

rinari

Rinari Is Not A Rails IDE (no longer updated; see eschulte's fork)
Emacs Lisp
66
star
12

lein-scalac

Clojure
54
star
13

conspire

A real-time collaborative editing platform built on Git.
Ruby
46
star
14

lein-licenses

List the license of each of your dependencies.
Clojure
44
star
15

skaro

comparative lispology
Scheme
42
star
16

lein-heroku

Experimental Leiningen plugin for managing Heroku apps
Clojure
42
star
17

radagast

Radagast is a simplistic test coverage tool.
Clojure
41
star
18

cooper

HyperCard-ish in Racket
Racket
40
star
19

erythrina

Basically ido for X, I guess?
OCaml
39
star
20

bludgeon

Bludgeon is a tool which will tell you if a given library is so large that you could bludgeon someone to death with a printout of it.
Ruby
37
star
21

bus-scheme

a Scheme written in Ruby, but implemented on the bus!
Ruby
37
star
22

nrepl-discover

proof-of-concept middleware for auto-discovery of nrepl ops
Clojure
36
star
23

lein.el

A no-startup-delay eshell replacement for the `lein` bash script.
Emacs Lisp
31
star
24

zossima

Jump to definition in Ruby driven by a live process
Emacs Lisp
30
star
25

lein-tar

Create tarballs from Leiningen projects.
Clojure
25
star
26

limit-break

Basic REPL breakpoints.
Clojure
25
star
27

calandria

Unix in voxel-world
Lua
25
star
28

javert

inspector
Clojure
24
star
29

orestes

the ultimate yak shave
C
23
star
30

relax.el

Interact with CouchDB databases from within Emacs, with ease!
Emacs Lisp
21
star
31

ri.el

An interface to RI (Ruby documentation) for Emacs
Emacs Lisp
19
star
32

circleci.el

Show build output from CircleCI builds inside Emacs
Emacs Lisp
19
star
33

lein-xml

All those parentheses got you down? How about some XML?
Clojure
17
star
34

ferrante

Locative Android app in Mirah
Shell
16
star
35

harker

Rails deployments via RubyGems. Because a package manager is a terrible thing to waste.
Ruby
14
star
36

lein-release

Tar up a Leiningen project along with some other handy directories
Clojure
13
star
37

chortles

Calculate the magnitude of a given laugh
Clojure
13
star
38

seajure

The web site for Seajure, the Seattle Clojure Group
Clojure
12
star
39

corkscrew

Proof-of-concept build system for Clojure.
Clojure
12
star
40

metaverse

Parallel universes for namespaces
Clojure
12
star
41

lein-play

Play a sound when your tests pass or fail.
Clojure
12
star
42

super-explorer

2D tile exploring game
Racket
12
star
43

lein-precate

You know, the opposite of deprecate.
Clojure
12
star
44

concourse

A web app for coordinating gatherings. Written in Compojure.
Clojure
10
star
45

paredit-screencast

9
star
46

server-socket

Clojure server-socket library spun off from monolithic contrib
Clojure
9
star
47

pindah

Moved to mirah organization
Ruby
7
star
48

lein-retest

Run only the test namespaces which failed last time around.
Clojure
5
star
49

lein-thrush

I guess technically it should be lein-comp; oops
Clojure
4
star
50

prometheus

XMPP heater
Erlang
4
star
51

lein-clean-m2

A Leiningen plugin to clean the local repository of unused artifacts.
Clojure
3
star
52

sokoban

push
Ruby
3
star
53

quickbeam

Access git trees.
Clojure
3
star
54

sketchbook

Some Processing sketches
Clojure
3
star
55

thai-type

Typing tutor for Thai
Racket
3
star
56

lein-blacklist

A Leiningen plugin to blacklist specific namespaces from being required.
Clojure
2
star
57

commodore-night-vision

Emacs Lisp
2
star
58

drainbot

Drain Heroku apps to IRC channels
Clojure
2
star
59

lein-survey

A survey of Leiningen users as a web app
Clojure
2
star
60

rcirc-ucomplete

Unambiguous completion for rcirc
Emacs Lisp
1
star
61

ss-calandria

A game using the minetest engine to teach programming and other technical skills
Lua
1
star
62

lein-standalone-repl

Clojure
1
star
63

swarm-go

Go, written by swarming
Clojure
1
star
64

squisserks

squisserks
Racket
1
star
65

clojars-verify

Verify checksums of Clojars artifacts
Clojure
1
star
66

lein-profiles

Profiles, backported from Leiningen 2 for your enjoyment.
Clojure
1
star
67

rodney-leonard-stubbs

The simplest stubs
Clojure
1
star
68

lein-assoc

A higher-order Leiningen task to run tasks with ad-hoc project keys.
Clojure
1
star