*New: *+> and +>> will now thread through try and catch, and leave finally
alone.

packthread

Threading macros for working with globs of state.
<https://github.com/maitria/packthread/blob/master/README.md#why>Why?

Many descriptions about state in Clojure fit into the following form:

State is hard to reason about, and so we use pure functions in Clojure. But
then we have a problem, we need to pass around *all*the application's
state. And that's just too hard, and it's basically just like having all
the global variables anyway, and you've coupled every function in the
system to this big ball of mud. So we need to separate it out and
encapsulate and therefore we've invented $library which does *x*, where *x* ∈
{*OO programming*, *global mutable state*, *...*}.

Packthread is for threading state through programs in a simple, composable
way. It does not compromise the ability to be functionally pure or reason
about one's program. It's pretty similar to the -> and ->> macros, with a
helper macro named in for creating different *projections* of the state to
manipulate with different functions.
<https://github.com/maitria/packthread/blob/master/README.md#>+>

Threads value through forms in much the same way as ->, except for special
handling of the following forms:
<https://github.com/maitria/packthread/blob/master/README.md#if-if-not-if-let-when-when-not-when-let>if,
if-not, if-let, when, when-not, when-let:

The value is threaded through the then and else clauses independently,
leaving the test conditions alone. If an else clause is missing, it is will
be supplied as though the value had been threaded through identity in that
case.

For example,

(+> 42 (if true inc)) ;=> 43(+> 42 (if false inc)) ;=> 42

In when, when-not, and when-let forms, the value is threaded through each
form in the body, not just the last.
<https://github.com/maitria/packthread/blob/master/README.md#cond>cond

The test clauses are left untouched and the value is threaded through the
expr clauses of each condition. If no :else condition was supplied, +> pretends
as though it has been (identity), and threads the value through that.

For example,

(+> 42
  (cond
    (= 1 2)
    inc)) ;=> 42
(+> 42
  (cond
    (= 1 1)
    dec)) ;=> 41

<https://github.com/maitria/packthread/blob/master/README.md#do>do

The current expr is threaded through the body forms of the do.
<https://github.com/maitria/packthread/blob/master/README.md#let>let

The current expression is threaded through the body of the let form, with
the bindings in place. For example:

(+> 42
  (let [x 1]
    (+ x))) ;=> 43

<https://github.com/maitria/packthread/blob/master/README.md#try>try

The current expression is threaded through the body of the try form. The
*same* value is threaded through each catch clause. Anyfinally clauses are
left alone.

(+> 42 (try
         inc
     (catch Exception e
       dec)) ;=> 43
(+> 42 (try
         (+ :foo)
     (catch Exception e
       dec))) ;=> 41
(+> 42 (try
         inc
     (finally dec))) ;=> 42

<https://github.com/maitria/packthread/blob/master/README.md#in>in

Threads the inner expressions through a projection of value.

projector is a function which takes two arguments: a value and a function.
It should apply the function to a *projection* of the value, take the
function's result, and reassemble from that result a value which can be
used again in the outer context.

For example,

(+> 42
    (in (fn [v f]
      (* 2 (f (/ v 2))))
      inc)) ;=> 42.5

This can be thought of as 'lifting' the body expressions into the 'world
where things are twice as large'.

As a special case, if projector is a keyword, in assumes that value is a
map and that sub-key are threaded through the inner expressions.

For example,

(+> {:hello 42}
    (in :hello
      (+ 5))) ;=> {:hello 47}

This macro can only be used inside +> or +>>.
<https://github.com/maitria/packthread/blob/master/README.md#-1>+>>

Threads expressions like ->>, except with the handling of the special forms
above.
<https://github.com/maitria/packthread/blob/master/README.md#installing>
Installing

Leiningen <http://github.com/technomancy/leiningen/> dependency information:

[com.maitria/packthread "0.1.1"]

<https://github.com/maitria/packthread/blob/master/README.md#usage>Usage

(require '[packthread.core :refer :all])
(+> 42
    (if true
      inc)) ;=> 43

See core_test.clj
<https://github.com/maitria/packthread/blob/master/test/packthread/core_test.clj>
for
examples of usage.
<https://github.com/maitria/packthread/blob/master/README.md#license>License

Copyright 2014 Maitria

You have permission to use this in any way you like (modify it, sell it,
republish it), provided you agree to all the following conditions:

   - you don't mislead anyone about it
   - you don't interfere with our ability to use it
   - you release us from any claims of liability if it causes problems for
   you

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to