2

I would like to the function (string-place-typeII) to return the value from (calculate-distance-matrix) like this: {:distance "5 km" :duration "2 mins"} on each loop or better still pass it to some variable enclosed in {} so I can return all of it at the end of the loop.

List of functions:

(defn get-placetypes
  ""
  []  (into-array (PlaceType/values )) )

(defn get-all-placetypes
  ""
  [x]
  (def my-vector (get-placetypes))
  (let [[& the-rest] my-vector]
    (nth the-rest x) ))

(defn string-place-typeII
  ""
  []
  (doseq [n (get-placetypes)]
    (calculate-distance-matrix 2 (define-context API-KEY) n))
  )
(defn calculate-distance-matrix
  ""
  [property-id context place-type]
  ;(def nearby-search-fucntion  )
  (let [r (. (. (. (DistanceMatrixApi/newRequest
                     context)
                   origins (into-array [(coordinates->keys property-id context)] ))
                destinations (into-array [(m/latlng (do-nearby-search property-id context place-type))]))  await)]

    {:distance (-> r
                   .rows
                   first
                   .elements
                   first
                   .distance
                   .humanReadable)
     :duration (-> r
                   .rows
                   first
                   .elements
                   first
                   .duration
                   .humanReadable)})

How to I make the (string-place-typeII) return the value of (calculate-distance-matrix) like this {:distance "5 km" :duration "2 mins"} on each iteration?

1
  • can't you use map instead of doseq? I use doseq only when 1. the body of doseq produces some side effects, 2. I don't have anything to be returned from there Commented Jan 23, 2017 at 6:19

3 Answers 3

3

Use map.

https://clojuredocs.org/clojure.core/map

(defn string-place-typeII
  []
  (map (fn [n] (calculate-distance-matrix 2 (define-context API-KEY) n))
       (get-place-types)))
Sign up to request clarification or add additional context in comments.

Comments

0

I would try with replacing the doseq with a for loop. It will call calculate-distance-matrix for each n in the (get-placetypes), then the results will be joined as a sequence:

(defn string-place-typeII
  ""
  []
  (for [n (get-placetypes)]
    (calculate-distance-matrix 2 (define-context API-KEY) n))
  )

Comments

0

Please bookmark the Clojure Cheatsheet and always keep a browser tab open to it.

I think you are looking for the for function instead of doseq. doseq is meant for side effects (eg printing, db, etc) and always returns nil. for returns one item for each time through the loop.

Here is an outline of what you can do:

(defn calc_dist [n]
  { :dist (* 2 n) :dur (* 3 n) } )

(defn caller-1 []
  (doseq [n (range 5)]
    (calc_dist n)))

(defn caller-2 []
  (for [n (range 5)]
    (calc_dist n)))

(caller-1) => nil
(caller-2) => [{:dist 0, :dur 0} {:dist 2, :dur 3} {:dist 4, :dur 6} {:dist 6, :dur 9} {:dist 8, :dur 12}]

Regarding your (let [r ...) expression, please see the clojure.org section on Java Interop. I believe you'll find the .. special form is easier to read. You should may also wish to consider the doto function.

1 Comment

Is forv a typo Alan?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.