Passing list of variables individually to clojure function

前端 未结 2 1914
北海茫月
北海茫月 2021-01-13 13:52

I have been playing around with clojure, and decided to make a higher order function that combines mapcat and list to emulate this behavior:

Clojure> (ma         


        
相关标签:
2条回答
  • 2021-01-13 14:31

    You are looking for apply. This calls a function with the arguments supplied in a sequence.

    But are you aware that there's a function interleave that does exactly what your mapcatList tries to do?

    0 讨论(0)
  • 2021-01-13 14:46

    You're right, the arguments are wrapped in a list as a result of the vararg declaration. You need to apply the arguments in order to unwrap the list of arguments:

    (defn mapcatList[& more]
      (apply mapcat list more))
    
    user=> (mapcatList '(1 2 3 4) '(5 6 7 8))               
    (1 5 2 6 3 7 4 8)
    user=> (mapcatList '(1 2 3 4) '(5 6 7 8) '(\a \b \c \d))
    (1 5 \a 2 6 \b 3 7 \c 4 8 \d)
    
    0 讨论(0)
提交回复
热议问题