clojure

clojure map transformation with Input values from sequence


Need Help in transforming data from map using a function

(defn transform-map-1 [past-step dir]
  (cond (= dir "v")
        (list (first (first past-step))
              (+ -1 (first (first past-step))))
        (= dir "^")
        (list (first (first past-step))
              (+ 1 (first (first past-step))))
        (= dir ">")
        (list (+ 1 (first (first past-step)))
              (first (first past-step)))
        (= dir "<")
        (list (+ -1 (first (first past-step)))
              (first (first past-step)))))

This function takes two input co-ordinates in x,y format as past step and dir up down left right to give the updates co-ordinates

what I would like to do have a series of directions and for each dir return updated co-ordinates

Input Sting "^>v<" , output should be ((0,1) (1,1) (1,0) (0,0)

we start with co-ordinates (0.0) Output from the first transformation becomes the input for the second char

The best I could come for is but it gives me an error

(map (transform-map-1 ['(0, 0)] %1)(char-array string-test))

Solution

  • You were close with your implementation.

    The issues I see are:

    With those two changes, the call to your function will look like this:

    (def test-string "^>v<")
    
    (map #(transform-map-1 ['(0, 0)] %1) (map str test-string))
    ;; returns: ((0 1) (1 0) (0 -1) (-1 0))
    

    There are other things you can learn later on that could help you solving your puzzle: