On Wed Sep 28 18:52 2011, Daniel Pittman wrote:
> G'day.
>
> I have problem that I have been thrashing back and forth over the best
> design of for a week now, and I can't work out the nicest way to
> handle it. Specifically, I have a collection of functions that return
> a primary result, and might also return a secondary "annotation" about
> that result.
>
> The current implementation is:
>
> (fn [] true) ; primary only
> (fn [] (annotated-return true {:foo 12})) ; with annotation
>
> `annotated-return` actually wraps a record, and then the handling code
> can determine if the result is specifically that type, or if it is
> anything else, to select between the two cases.
>
> In a bunch of other languages I would use a `pair` or `tuple`, but
> Clojure doesn't have a native type that maps closely to that. Worse,
> though, it doesn't have a native type that isn't a valid return from
> one of these methods. (Set, Vector, List, and Map are all used. :)
>
> The alternatively I can think of are limited: I don't like the idea of
> guessing based on a two element vector return or so, since I want this
> to be pretty much impossible to accidentally break.
Why not always return a vector? That is more or less what a tuple is.
(fn [] [true]) ; primary only
(fn [] [true {:foo 12}]) ; with annotation
That way, your data is always (first x) and your metadata is always
(second x).
If you want to use something more typed-like, you could return a map:
(fn [] {:result true})
(fn [] {:result true :metadata {:foo 12}})
For this approach, you could write helper functions to make the code
less painful to read and write. If performance is a concern, you can
try a record.
> I considered using meta-data, but that can't attach to a primitive
> value, and those are definitely valid results.
>
> So, is there a better way to do this? At this point I am tempted to
> step aside from functional code, and use stateful mutation of some
> sort to maintain the annotations:
>
> (fn [] (annotate-by-side-effort {:foo 12}) true)
>
> Daniel
> --
> ♲ Made with 100 percent post-consumer electrons
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
signature.asc
Description: Digital signature
