Xaritani obyektni loop ichida aniqlash - Clojure

Loop ichida bo'sh xarita yaratadigan loopning takrorini ishlatishga harakat qilaman. Loopdagi har bir kirish uchun (xaritalarning vektoridan o'tib ketadi), yangi yaratilgan xaritada iteratsiya qilingan qiymatlar kalitiga mos keladigan kalit mavjudmi yoki yo'qmi yo'qligini ko'rasiz.

Ushbu kodni yaratdim:

(def meteor-map (json/read-str (clojure.string/lower-case
(slurp "https://data.nasa.gov/resource/y77d-th95.json"))))

(defn most-falls [values]
(loop [values map count-tracker{}]
  (if (empty? values)
    (count-tracker)
    (do
      (def key (keyword (get (first values) "year")))
      (if (contains? (first values) key)
        (do
          (def count-tracker (update count-tracker key inc))
          (recur (rest values) count-tracker)
        )
        (do
          (def count-tracker (assoc count-tracker key 1))
          (recur (rest values) count-tracker)
        )
      )
    )
  )
)
)

(most-falls meteor-map)

Ammo bu funksiyani chaqirganimda va meteor-xaritada o'tsam (bu xaritalarning vektori) men xato so'zlay olaman

noto'g'ri args soni (0) persistentarraymapga o'tkazildi.

Men bu loop yaratishda dastlabki hisoblagich obyektini qanday yaratayotganimga bog'liq bo'lishi mumkin, deb o'ylayman, lekin ishonchim komil emas.

Har qanday fikr bormi?

Rahmat

PS bu savolning biroz noaniq ekanini bilaman, shuning uchun har qanday savollar faqatgina so'raydi!

0
To'g'ri xato nima? Muammo shundaki, sizning kodingiz bir necha nosozlik ( def va [map> loop kodidagi loop dan foydalaning) yordamida aniqlanadi, shuning uchun noto'g'ri ketmoqda.
qo'shib qo'ydi muallif Carcigenicate, manba
(count-tracker) nima qilish kerak? Sizning xatolaringiz shu. Siz xaritani argumentlarsiz chaqirishga harakat qilyapsiz. Buni tuzganingizdan so'ng, map map funksiyasi - map map sizga xatolik beradi. Siz faqatgina qiymatlari qadriyatlarini degan ma'noni anglatadi.
qo'shib qo'ydi muallif Carcigenicate, manba
Lekin (count-tracker) bilan nima qilmoqchisiz? Faqat count-tracker ga qayting? Keyin uning atrofidagi parantezni olib tashlang. Yodingizda tuting, agar siz parantezda biror narsani o'rab qo'ysangiz, uni funksiya deb atayapsiz.
qo'shib qo'ydi muallif Carcigenicate, manba
Men sizga javob yozyapman, chunki siz aytib o'tgan bir nechta narsalar bor. Menga bir necha daqiqa bering.
qo'shib qo'ydi muallif Carcigenicate, manba
@Carcigenicate count-tracker men yaratgan bo'sh xarita va looplarda mavjud bo'lgan qiymatga qarab kalitlarni qo'shish va yangilashni talab qiladi. Masalan, agar birinchi qiymat: "1996" yiliga ega bo'lsa va hisoblagichda mavjud bo'lmasa, bu xarita uchun kalitni qo'shib qo'yadi. Agar u allaqachon mavjud bo'lsa (masalan, 1996 yil 1), o'sish kuchayadi. Umid qilamanki bu mantiqan!
qo'shib qo'ydi muallif broliverparker, manba
@Mening muammomni hal qilib bo'lgach, juda minnatdorman!
qo'shib qo'ydi muallif broliverparker, manba

8 javoblar

Bu yerda ko'p narsalar bo'lishi kerak. Sizning asosiy muammolaringiz (count-tracker) bilan bo'lsa-da. Siz xaritani Qavslar ichiga joylashtirasiz, bu sizning vazifangiz deb nomlashni anglatadi. Boshqa tillarda bo'lgani kabi kodni o'zboshimchalik bilan qo'shib bo'lmaydi; Clojure'da juda ga xos ma'noga ega. (f) har doim funktsiyasi f deb ataladi. Buni qiymatni qaytarish uchun uni count-tracker ga o'zgartiring.

Boshqa narsalar:

  • NEVER use def inside of a function unless it's necessary. In this case though, it's entirely unnecessary. Every use of def creates globals that last for the length of the program (yes, they exist even after the function exits!). Use let instead:

    (let [key (keyword (get (first values) "year")))]
       ... ) ; Use key here
    
  • (loop [values map ...] will cause errors as well. map is a function, so this throws away the argument passed in to most-falls, overwriting it with the map function. This will cause an error when you try to use values as a sequence, since the map function doesn't support empty? or first, or anything else you're trying to use it for. I think you just intended to just rebind the argument to be used in the loop. Just change it to (loop [values values ...]. Arguably, you shouldn't shadow arguments by creating other bindings with the same name, but that's not exceedingly important here.

Yana yaxshilanishi mumkin bo'lgan boshqa kichik narsalar ham bor. Tahrirlashdan foydalangan holda, birinchi va rest ga qo'ng'iroqlarni o'tkazib yuborish va kamaytirish dan foydalanib, pastadir yordamida aniq aylanishni soddalashtirishi mumkin >, lekin bu asosiy masalalardan tushib ketadi. Yuqorida aytib o'tganimni hisobga olib, vazifangizni quyidagicha yozaman:

(defn most-falls [values]
  (loop [values values
         count-tracker {}]
    (if (empty? values)
      count-tracker
      (let [key (keyword (get (first values) "year"))]
        (recur (rest values)
               (if (contains? (first values) key)
                 (update count-tracker key inc)
                 (assoc count-tracker key 1)))))))
3
qo'shib qo'ydi

Bu yerda ko'p narsalar bo'lishi kerak. Sizning asosiy muammolaringiz (count-tracker) bilan bo'lsa-da. Siz xaritani Qavslar ichiga joylashtirasiz, bu sizning vazifangiz deb nomlashni anglatadi. Boshqa tillarda bo'lgani kabi kodni o'zboshimchalik bilan qo'shib bo'lmaydi; Clojure'da juda ga xos ma'noga ega. (f) har doim funktsiyasi f deb ataladi. Buni qiymatni qaytarish uchun uni count-tracker ga o'zgartiring.

Boshqa narsalar:

  • NEVER use def inside of a function unless it's necessary. In this case though, it's entirely unnecessary. Every use of def creates globals that last for the length of the program (yes, they exist even after the function exits!). Use let instead:

    (let [key (keyword (get (first values) "year")))]
       ... ) ; Use key here
    
  • (loop [values map ...] will cause errors as well. map is a function, so this throws away the argument passed in to most-falls, overwriting it with the map function. This will cause an error when you try to use values as a sequence, since the map function doesn't support empty? or first, or anything else you're trying to use it for. I think you just intended to just rebind the argument to be used in the loop. Just change it to (loop [values values ...]. Arguably, you shouldn't shadow arguments by creating other bindings with the same name, but that's not exceedingly important here.

Yana yaxshilanishi mumkin bo'lgan boshqa kichik narsalar ham bor. Tahrirlashdan foydalangan holda, birinchi va rest ga qo'ng'iroqlarni o'tkazib yuborish va kamaytirish dan foydalanib, pastadir yordamida aniq aylanishni soddalashtirishi mumkin >, lekin bu asosiy masalalardan tushib ketadi. Yuqorida aytib o'tganimni hisobga olib, vazifangizni quyidagicha yozaman:

(defn most-falls [values]
  (loop [values values
         count-tracker {}]
    (if (empty? values)
      count-tracker
      (let [key (keyword (get (first values) "year"))]
        (recur (rest values)
               (if (contains? (first values) key)
                 (update count-tracker key inc)
                 (assoc count-tracker key 1)))))))
3
qo'shib qo'ydi

Bu yerda ko'p narsalar bo'lishi kerak. Sizning asosiy muammolaringiz (count-tracker) bilan bo'lsa-da. Siz xaritani Qavslar ichiga joylashtirasiz, bu sizning vazifangiz deb nomlashni anglatadi. Boshqa tillarda bo'lgani kabi kodni o'zboshimchalik bilan qo'shib bo'lmaydi; Clojure'da juda ga xos ma'noga ega. (f) har doim funktsiyasi f deb ataladi. Buni qiymatni qaytarish uchun uni count-tracker ga o'zgartiring.

Boshqa narsalar:

  • NEVER use def inside of a function unless it's necessary. In this case though, it's entirely unnecessary. Every use of def creates globals that last for the length of the program (yes, they exist even after the function exits!). Use let instead:

    (let [key (keyword (get (first values) "year")))]
       ... ) ; Use key here
    
  • (loop [values map ...] will cause errors as well. map is a function, so this throws away the argument passed in to most-falls, overwriting it with the map function. This will cause an error when you try to use values as a sequence, since the map function doesn't support empty? or first, or anything else you're trying to use it for. I think you just intended to just rebind the argument to be used in the loop. Just change it to (loop [values values ...]. Arguably, you shouldn't shadow arguments by creating other bindings with the same name, but that's not exceedingly important here.

Yana yaxshilanishi mumkin bo'lgan boshqa kichik narsalar ham bor. Tahrirlashdan foydalangan holda, birinchi va rest ga qo'ng'iroqlarni o'tkazib yuborish va kamaytirish dan foydalanib, pastadir yordamida aniq aylanishni soddalashtirishi mumkin >, lekin bu asosiy masalalardan tushib ketadi. Yuqorida aytib o'tganimni hisobga olib, vazifangizni quyidagicha yozaman:

(defn most-falls [values]
  (loop [values values
         count-tracker {}]
    (if (empty? values)
      count-tracker
      (let [key (keyword (get (first values) "year"))]
        (recur (rest values)
               (if (contains? (first values) key)
                 (update count-tracker key inc)
                 (assoc count-tracker key 1)))))))
3
qo'shib qo'ydi

Bu yerda ko'p narsalar bo'lishi kerak. Sizning asosiy muammolaringiz (count-tracker) bilan bo'lsa-da. Siz xaritani Qavslar ichiga joylashtirasiz, bu sizning vazifangiz deb nomlashni anglatadi. Boshqa tillarda bo'lgani kabi kodni o'zboshimchalik bilan qo'shib bo'lmaydi; Clojure'da juda ga xos ma'noga ega. (f) har doim funktsiyasi f deb ataladi. Buni qiymatni qaytarish uchun uni count-tracker ga o'zgartiring.

Boshqa narsalar:

  • NEVER use def inside of a function unless it's necessary. In this case though, it's entirely unnecessary. Every use of def creates globals that last for the length of the program (yes, they exist even after the function exits!). Use let instead:

    (let [key (keyword (get (first values) "year")))]
       ... ) ; Use key here
    
  • (loop [values map ...] will cause errors as well. map is a function, so this throws away the argument passed in to most-falls, overwriting it with the map function. This will cause an error when you try to use values as a sequence, since the map function doesn't support empty? or first, or anything else you're trying to use it for. I think you just intended to just rebind the argument to be used in the loop. Just change it to (loop [values values ...]. Arguably, you shouldn't shadow arguments by creating other bindings with the same name, but that's not exceedingly important here.

Yana yaxshilanishi mumkin bo'lgan boshqa kichik narsalar ham bor. Tahrirlashdan foydalangan holda, birinchi va rest ga qo'ng'iroqlarni o'tkazib yuborish va kamaytirish dan foydalanib, pastadir yordamida aniq aylanishni soddalashtirishi mumkin >, lekin bu asosiy masalalardan tushib ketadi. Yuqorida aytib o'tganimni hisobga olib, vazifangizni quyidagicha yozaman:

(defn most-falls [values]
  (loop [values values
         count-tracker {}]
    (if (empty? values)
      count-tracker
      (let [key (keyword (get (first values) "year"))]
        (recur (rest values)
               (if (contains? (first values) key)
                 (update count-tracker key inc)
                 (assoc count-tracker key 1)))))))
3
qo'shib qo'ydi

Birinchisi, ushbu moddada mutlaqo bir masala bor:

(loop [values map count-tracker{}]

Men nima qilmoqchi bo'lganingizni bilmayman, lekin iltimos, ko'rib chiqing.

So'ngra, kodning ichida def yoki defn shakllaridan foydalanmang, faqatgina ism-sharfning eng yuqori darajasidan foydalaning.

Nihoyat, loop/recur juda past darajadagi shakl bo'lib, nima qilayotganingizni yaxshi bilishi kerak. Ko'pincha u ko'proq foydalanuvchilarga mos bo'lgan narsalar bilan almashtirilishi mumkin. kamaytirish , men ishonaman, yaxshi bo'ladi. Bu sizning boshlang'ich qiymatingizni (o'zingizdagi bo'sh xarita), ikkita argumentlarning to'plamini va funktsiyasini oladi, ikkinchisi joriy kolleksiyaning elementi, ikkinchisi - dastlabki qiymat yoki oldingi funksiya chaqiruvining natijasi.

Ushbu funktsiyani ichida siz xaritada muayyan kalit mavjudmi yoki yo'qligini aniqlaysiz.

Qisqa misol:

# here are some data you've read from a file
(def items [{...} {...} {...}])

# reduce process function
(defn process
  [result item]
  (if (:some-key result) ;; here, you check the current map for a key
    result               ;; return the old map if everything is ok
    (assoc result :some-key some-data)))  ;; accumulate a new key into a map

(reduce process {} items)
1
qo'shib qo'ydi

Birinchisi, ushbu moddada mutlaqo bir masala bor:

(loop [values map count-tracker{}]

Men nima qilmoqchi bo'lganingizni bilmayman, lekin iltimos, ko'rib chiqing.

So'ngra, kodning ichida def yoki defn shakllaridan foydalanmang, faqatgina ism-sharfning eng yuqori darajasidan foydalaning.

Nihoyat, loop/recur juda past darajadagi shakl bo'lib, nima qilayotganingizni yaxshi bilishi kerak. Ko'pincha u ko'proq foydalanuvchilarga mos bo'lgan narsalar bilan almashtirilishi mumkin. kamaytirish , men ishonaman, yaxshi bo'ladi. Bu sizning boshlang'ich qiymatingizni (o'zingizdagi bo'sh xarita), ikkita argumentlarning to'plamini va funktsiyasini oladi, ikkinchisi joriy kolleksiyaning elementi, ikkinchisi - dastlabki qiymat yoki oldingi funksiya chaqiruvining natijasi.

Ushbu funktsiyani ichida siz xaritada muayyan kalit mavjudmi yoki yo'qligini aniqlaysiz.

Qisqa misol:

# here are some data you've read from a file
(def items [{...} {...} {...}])

# reduce process function
(defn process
  [result item]
  (if (:some-key result) ;; here, you check the current map for a key
    result               ;; return the old map if everything is ok
    (assoc result :some-key some-data)))  ;; accumulate a new key into a map

(reduce process {} items)
1
qo'shib qo'ydi

Birinchisi, ushbu moddada mutlaqo bir masala bor:

(loop [values map count-tracker{}]

Men nima qilmoqchi bo'lganingizni bilmayman, lekin iltimos, ko'rib chiqing.

So'ngra, kodning ichida def yoki defn shakllaridan foydalanmang, faqatgina ism-sharfning eng yuqori darajasidan foydalaning.

Nihoyat, loop/recur juda past darajadagi shakl bo'lib, nima qilayotganingizni yaxshi bilishi kerak. Ko'pincha u ko'proq foydalanuvchilarga mos bo'lgan narsalar bilan almashtirilishi mumkin. kamaytirish , men ishonaman, yaxshi bo'ladi. Bu sizning boshlang'ich qiymatingizni (o'zingizdagi bo'sh xarita), ikkita argumentlarning to'plamini va funktsiyasini oladi, ikkinchisi joriy kolleksiyaning elementi, ikkinchisi - dastlabki qiymat yoki oldingi funksiya chaqiruvining natijasi.

Ushbu funktsiyani ichida siz xaritada muayyan kalit mavjudmi yoki yo'qligini aniqlaysiz.

Qisqa misol:

# here are some data you've read from a file
(def items [{...} {...} {...}])

# reduce process function
(defn process
  [result item]
  (if (:some-key result) ;; here, you check the current map for a key
    result               ;; return the old map if everything is ok
    (assoc result :some-key some-data)))  ;; accumulate a new key into a map

(reduce process {} items)
1
qo'shib qo'ydi

Birinchisi, ushbu moddada mutlaqo bir masala bor:

(loop [values map count-tracker{}]

Men nima qilmoqchi bo'lganingizni bilmayman, lekin iltimos, ko'rib chiqing.

So'ngra, kodning ichida def yoki defn shakllaridan foydalanmang, faqatgina ism-sharfning eng yuqori darajasidan foydalaning.

Nihoyat, loop/recur juda past darajadagi shakl bo'lib, nima qilayotganingizni yaxshi bilishi kerak. Ko'pincha u ko'proq foydalanuvchilarga mos bo'lgan narsalar bilan almashtirilishi mumkin. kamaytirish , men ishonaman, yaxshi bo'ladi. Bu sizning boshlang'ich qiymatingizni (o'zingizdagi bo'sh xarita), ikkita argumentlarning to'plamini va funktsiyasini oladi, ikkinchisi joriy kolleksiyaning elementi, ikkinchisi - dastlabki qiymat yoki oldingi funksiya chaqiruvining natijasi.

Ushbu funktsiyani ichida siz xaritada muayyan kalit mavjudmi yoki yo'qligini aniqlaysiz.

Qisqa misol:

# here are some data you've read from a file
(def items [{...} {...} {...}])

# reduce process function
(defn process
  [result item]
  (if (:some-key result) ;; here, you check the current map for a key
    result               ;; return the old map if everything is ok
    (assoc result :some-key some-data)))  ;; accumulate a new key into a map

(reduce process {} items)
1
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb