Koenig Lear
Koenig Lear

Reputation: 2436

partial application in clojure

How do you do partial application in Clojure?

I have tried:

(dorun (map println ["1" "2" "3" "4"]))

which works.

(async/send! channel "hello")

works too. But if I try to apply partial application

(dorun (map (async/send! channel) ["1" "2" "3" "4"]))

or

(dorun (map #(async/send! channel) ["1" "2" "3" "4"]))

or

(apply (partial map (async/send! channel) ["1" "2" "3" "4"]))

It says

clojure.lang.ArityException: Wrong number of args (1) passed to: immutant.web.async/send!

What am I doing wrong?

Upvotes: 1

Views: 228

Answers (2)

Alexander Kaplyar
Alexander Kaplyar

Reputation: 36

In Clojure currying is different than in languages like ML, F# or Haskell.

There are 2 ways to do partial application in Clojure:

  1. Making closure, where you can specify the exact order of arguments:

    (fn [coll] (map str coll)) of #(map str %)

  2. Using partial, which will substitute arguments in order they provided:

    (partial map str)

When you call function with less arguments than it requires you'll get ArityException (unless it's a multi-arity function, that can accept different number of arguments).

Upvotes: 2

Koenig Lear
Koenig Lear

Reputation: 2436

Nevermind, this seems to work:

    (dorun (map (partial async/send! channel) ["1" "2" "3" "4"]))

A bit confused why this didn't work

    (dorun (map #(async/send! channel) ["1" "2" "3" "4"]))

Upvotes: 0

Related Questions