Map laziness in clojure

I am creating a simple swing GUI in Clojure. I am trying to apply a single function to several GUI components using a map in a let context:

(map #(f % component4) [component1 component2 component3])

Where all components are defined in let.

Problemically, the card is lazy, and the action does not apply to the components, however I can force it by wrapping the above in "take".

Is there an alternative card for lazy cards? Or should I go about it differently?

EDIT: Use counterclockwise in an eclipse. I had different results using (use "Lib: reload") from REPL and using CTRL + Enter from the editor. Rebooting launches the GUI, but the problem described above will occur. The problem did not occur when using CTRL + Enter from the editor, so I think my description of the problem may be inaccurate. Regardless of the fact that the dose is the best alternative map in this scenario.

+6
source share
3 answers

I dispute your claim that the involvement of take has any meaning. If you wrapped it in doall or dorun , it will do what you want, but you should use doseq instead of map for this kind of action for side effects only.

Note

Originally posted as a comment on the question; copied to answer by popular request.

+17
source

doseq is probably the best way to approach this. the dose is roughly equivalent to the โ€œfor everyoneโ€ instruction, which crosses each element of the collection in many other languages. It is guaranteed that he will not be lazy.

 (doseq [comp [component1 component2 component3]] (f comp component4)) 

Some general recommendations:

  • Use the card and its lazy friends (including pick, delete, etc.) when you want the sequence to be the result
  • Use dose , doall , dotimes , etc. when you are more interested in side effects.
+8
source

Wrapping your card in doall will make it appreciate. or a better alternative is the dose that is used for things related to side effects.

+2
source

Source: https://habr.com/ru/post/902343/


All Articles