?

Log in

No account? Create an account
patrickwonders
patrickwonders

Looking for the right hammer...

I am working on some CFFI bindings for OpenMPI. Most of the MPI functions return their results with OUT parameters whilst the function itself returns an :int status code. As such, I find myself doing this sort of thing a bunch:

(let ((val (some-function-call with some args)))
     (if (zerop val)
         (do-one-thing)
         (do-another-thing-with val)))

Being a big DRY fan, I am hoping to refactor this a bit. So, I'm looking for the right phrasing.

Some background for the not-so Lispy folks...Collapse )

The binding if doesn't really work for my pattern though. Because zero is true in Lisp, I cannot just do:

(bif (val (some-function-call with some args))
     (do-another-thing-with val)
     (do-one-thing))

So, I need to bind a value to some variable, then run some predicate (a function that returns true or false) on the bound result. I don't want to bind the result of the predicate. I want to bind the thing I am passing to the predicate.

Here are some different ways that I'm kicking around making this go. (Note: the #'zerop is a predicate that returns NIL except when its argument is zero.)

Cut for size...Collapse )

And, I'm just not going to get out of that alive. I could try to recurse, but I wouldn't know to stop recursing at (some-func ...) if it has args that were other function calls. At least not without playing massive introspection games... and even then only if I am sure that the variables I want to bind to values aren't elsewhere bound to functions.

Wherein I ramble...Collapse )

For now, I'm just going to stick with single-argument predicates especially since for my current code, I will probably only need #'zerop as zero is false in C but not in Lisp. In fact, I may just start with:

(bif-zerop (val (some-function-call with some args))
           (do-one-thing)
           (do-another-thing-with val))
Tags: