Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal -<< (distribute) #407

Open
emacksnotes opened this issue Aug 15, 2023 · 4 comments
Open

Proposal -<< (distribute) #407

emacksnotes opened this issue Aug 15, 2023 · 4 comments

Comments

@emacksnotes
Copy link

emacksnotes commented Aug 15, 2023

Proposal to introduce -<< (distribute)

-<< looks like a fork in the pipeline, with the same arg getting fed in to multiple forms.

(defmacro -<< (x forms)
  `(list ,@(->> forms
                (--map `(->> ,x ,it)))))

With the above definition, the form

(-<< '(H S L)
     ((--map (intern (format "INDEX-OF-%s-IN-HSLUV" it)))
      (funcall (-compose (-cut -iota <> 0) #'length))))

expands to

(list
 (->> '(H S L) (--map (intern (format "INDEX-OF-%s-IN-HSLUV" it))))
 (->> '(H S L) (funcall (-compose (-cut -iota <> 0) #'length))))

and evaluates to

((INDEX-OF-H-IN-HSLUV INDEX-OF-S-IN-HSLUV INDEX-OF-L-IN-HSLUV)
 (0 1 2))

Here is a non-trivial example,

(->> (-<< '(H S L)
          ((--map (intern (format "INDEX-OF-%s-IN-HSLUV" it)))
           (funcall (-compose (-cut -iota <> 0) #'length))))
     (apply #'-zip-lists))

evaluates to

((INDEX-OF-H-IN-HSLUV 0) (INDEX-OF-S-IN-HSLUV 1)
 (INDEX-OF-L-IN-HSLUV 2))

Two << in -<< signifies that two >>-s are used in it macro expansion. So, -<< distribute at the last position.

@emacksnotes
Copy link
Author

emacksnotes commented Aug 15, 2023

Few more examples

(defun -avg (values)
  (->> (-<< values
	    ((apply #'+)
	     (funcall (-compose #'float #'length))))
       (apply #'/)))
(-avg (-iota 10))
4.5
(defun -min-avg-and-max (values)
  (-<< values
       (-min -avg -max)))
(-min-avg-and-max (-iota 10))
(0 4.5 9)

@emacksnotes
Copy link
Author

If there is an existing elegant way of doing -<<, I will be happy to hear about it, and use it.

@emacksnotes
Copy link
Author

emacksnotes commented Aug 15, 2023

May be it shouldn't be a macro

(defmacro -<< (x forms)
  `(list ,@(->> forms
                (--map `(->> ,x ,it)))))

just a defun like so

(defun -<< (x fs)
  (->> fs
       (--map (funcall it x))))

You can close this bug if you want (or leave it open), I may keep adding note as I refine my thinking. For now, this distribute is just an "intuition"; I wanted to get the idea out of my head ... so that I am not stuck.

The idea of distribute is

  • it transforms a value, in to a list of values
  • it is parallel, a value gets distributed in to multiple assembly lines.

I have used this variation of -distribute (-<<) in Proposal add -which -- Return indices where the list entry is non-nil

@kjambunathan
Copy link

kjambunathan commented Aug 16, 2023

There is a similar request to this one at -<< / swiss-arrows?. This request seems to be independent of that note ...

( I don't use Haskell, or Clojure or Common Lisp even, so my comments here are not influenced by (or un-informed about) what passes for norm in those languages.)

May be it shouldn't be a macro

I think -<< it should be a defmacro. And it can be used for iterators on multiple dimension.

-<< as intended here, builds child nodes and extends the levels of tree. Threading macros ->> add siblings nodes.

-<< as intended could be used for iterations, on multiple dimensions. The nodes one level deep are range of values on first dimension, The nodes that are two level deep are range of values on two dimesions.

For a concrete use case, think of generating colors in HSLUV space. First level of -<< will generate a single dimensional vector of all possible values of H. The second level application of -<< will generate a two dimensional vector where the H value is fixed by its parent node, and S values spans the entire spectrum; so on and so forth

In other words, when you nest -<<, the values that are generated exponentially blow up.

As a side-note, I "vaguely" recollect there being a counterpart to -<< in R. Since I don't use R for day to day use, I have to consult the R-docs or dig in to my notes to substantiate this remark.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants