Clojure apply vs map

前端 未结 3 1445
北恋
北恋 2021-01-30 21:48

I have a sequence (foundApps) returned from a function and I want to map a function to all it\'s elements. For some reason, apply and count work for th

3条回答
  •  隐瞒了意图╮
    2021-01-30 22:21

    I have a simple explanation which this post is lacking. Let's imagine an abstract function F and a vector. So,

    (apply F [1 2 3 4 5])
    

    translates to

    (F 1 2 3 4 5)
    

    which means that F has to be at best case variadic.

    While

    (map F [1 2 3 4 5])
    

    translates to

    [(F 1) (F 2) (F 3) (F 4) (F 5)]
    

    which means that F has to be single-variable, or at least behave this way.

    There are some nuances about types, since map actually returns a lazy sequence instead of vector. But for the sake of simplicity, I hope it's pardonable.

提交回复
热议问题