L
L
lllaptemlll2019-01-21 14:48:26
Algorithms
lllaptemlll, 2019-01-21 14:48:26

How to optimize the code and get rid of crutches in recursion at the first and last iteration?

Task: Write a function that takes as input a pipe consisting of a sequence of numbers, the first of which is the number of subsequent elements to be placed in the array, followed by the elements of this array, and returns individual arrays. For example 3, 4, 0, 2, 1, 2, 2, 4, 5 will be turned into [4, 0, 2], [2], [4, 5]
Now it looks like this:
In the main function, I take a vector as input and, using recursion, each value is either placed in Temp if Count is not 0, or otherwise assigned to Count, at the same time as Temp is placed in Result. But it turns out such a thing that as soon as the recursion ends, the last Temp does not go to Result and therefore you have to attribute a crutch to vec-to-chan with the addition of an extra element. Can I somehow find a more correct solution to get rid of the crutch? I tried to add Temp to Result in the if-let, but the else construction itself doesn’t really want to work
And is it possible to somehow get rid of the condition with (if (= i 0) ? It serves to ensure that in the very first Temp with an empty [ ] was not written to Result

(ns untitled1.core
  (:require [promesa.core :as p]
            [clojure.core.async
             :as a
             :refer [>! <! >!! <!! go chan close! go-loop]]))

(defn vec-to-chan [vec]
    (let [c (chan)
          v (conj vec "end")]
      (go (doseq [x v]
                (>! c x))

              (close! c))
  c))

(defn main[c]
  (<!!
    (go-loop [Result [] Temp [] Count 0 i 0]
      (if-let [x (<! c)]
        (if (= i 0)
          (recur Result [] x (inc i))
          (do
            (if (= Count 0)
              (recur (conj Result Temp) [] x (inc i))
              (recur Result (conj Temp x) (dec Count) (inc i))
              ))
          )
        Result)
      )))


(def ch (vec-to-chan [3 4 0 2 1 2 2 4 5]))
(println "Result" (main ch))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2019-01-23
@lllaptemlll

Here, I threw it. It became interesting to myself :-) - https://repl.it/@AlexeyCheremisi/DelightfulValuabl...
And yes, don't scold me strictly! This is my first code in this language, and it seems not the last :-)
PS. A little ennobled through let t brought returned to vector, as in the question!

;; my recursion function
(defn func [inp]
  (loop [ acc [] [cnt & rest] inp ] ;; accumulator, counter from rest, rest
    (let [head (take cnt rest) tail (drop cnt rest)] ;; get portion of data to head, get tail of data
      (if (= (count rest) 0) acc ;; return accumulator if no data
        (recur (conj acc (into [] head)) tail))))) ;; loop with convert list to vector

;; input data
(def indata [3 4 0 2 1 2 2 4 5])

;; test recursion
(func indata) ;; => 

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question