How To Supply Multiple Values To Function In Map

2,363 views
Skip to first unread message

octopusgrabbus

unread,
Jun 28, 2011, 2:58:35 PM6/28/11
to Clojure
Given this sample:

(ns test-csv
(:gen-class)
(:use clojure.contrib.command-line)
(:use clojure-csv.core))

(defn x1
[val1 val2]
(println val1 val2))

(defn process-file
"Process csv file and prints a column in every row"
[file-name]
(let [data (slurp file-name)
rows (parse-csv data)]
(dorun (map #(println ( nth % 11 nil)) rows))))

(defn -main [& args]
(with-command-line args
"Get csv file name"
[[file-name ".csv file name" "resultset.csv"]]
[[file-name ".csv file name" 1]]
(println "file-name:", file-name)
(process-file file-name)))

I would like to print out two or more values, or creating a vector of
those values would be helpful. I have been experimenting, but either
get errors or nothing printed out.

I am just not sure how to approach this.
Thanks.
cmn

Mark Rathwell

unread,
Jun 28, 2011, 3:11:15 PM6/28/11
to clo...@googlegroups.com

map takes a function and a collection.  So, that function can be:

 1. named:
   - define the function on it own, using the defn macro
   - pass it as first argument to map

   (def coll [[1 2] [3 4] [4 5]])

   (defn foo [x y]
     (println x y))

   (map foo coll)

 2. anonymous:
  - use shorthand notation #(...)
    -- for one argument function, % represents the argument
    -- for multiple args, use %1, %2, %3, etc., which represent arg in specified position
  - use the fn special form (fn [x y] ...)
  - specify these inline in map call

   (def coll [[1 2] [3 4] [4 5]])
 
   (map #(println %1 %2) coll)

   (map (fn [x y] (println x y)) coll)



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

octopusgrabbus

unread,
Jun 28, 2011, 3:20:21 PM6/28/11
to Clojure
Thanks. I've followed most of what you've written except how the
symbol col fits into this, unless you're using it as map values.

On Jun 28, 3:11 pm, Mark Rathwell <mark.rathw...@gmail.com> wrote:
> map takes a function and a collection.  So, that function can be:
>
>  1. named:
>    - define the function on it own, using the defn macro
>    - pass it as first argument to map
>
>    (def coll [[1 2] [3 4] [4 5]])
>
>    (defn foo [x y]
>      (println x y))
>
>    (map foo coll)
>
>  2. anonymous:
>   - use shorthand notation #(...)
>     -- for one argument function, % represents the argument
>     -- for multiple args, use %1, %2, %3, etc., which represent arg in
> specified position
>   - use the fn special form (fn [x y] ...)
>   - specify these inline in map call
>
>    (def coll [[1 2] [3 4] [4 5]])
>
>    (map #(println %1 %2) coll)
>
>    (map (fn [x y] (println x y)) coll)
>

Mark Rathwell

unread,
Jun 28, 2011, 3:25:53 PM6/28/11
to clo...@googlegroups.com
coll is the vector defined at the top of each code listing in the examples given, and is used as the second argument to map in all examples.

Sent from my iPhone

John Palgut

unread,
Jul 1, 2011, 1:38:51 PM7/1/11
to clo...@googlegroups.com
map actually accepts any number of collections, as show in the official documentation. Your function passed to map just needs to accept parameters equal to the number of collections passed. So you could write something like the following:

Clojure 1.2.0
user=> (dorun (map #(println %1 %2) [1 2 3 4] [2 3 4 5]))
1 2
2 3
3 4
4 5
nil

The advantage to the above code is you don't need to create a vector of subvectors.
Reply all
Reply to author
Forward
0 new messages