Clojure yordamida javascript funktsiyasini aylantirish

Lisp/Funktsional/Clojure dunyosida xushmuomala va menda JS funktsiyasi mavjud:

function buildString(someInteger) {

   var question = "Initial text";

   if (someInteger == 1) {
     question += " put this string ";
   } else if(someInteger == 2) {
     question +=  " oh! another string ";
   } else if(someInteger == 3) {
     question +=  " guess what? ";
   }
   return question;
  }

Buni Clojure funktsiyasiga qayta yozishning qanday yaxshi usuli bo'lishi mumkin? "Cond" Clojure makrosini ishlatib, ba'zi bir kod bor, lekin "o'zgaruvchan" mag'lubiyatga nisbatan ishonchim komil emas:

(defn build-string [some-integer]
  (let [question "Initial text"]
    (cond
      (= some-integer 1) (str question "Add string one")
      (= some-integer 2) (str question "Add string two")
      (= some-integer 3) (str question "Add string three"))))
1
Iltimos, allaqachon sinab ko'rgan "cljs" kodini qo'shing.
qo'shib qo'ydi muallif cfrick, manba
Iltimos, allaqachon sinab ko'rgan "cljs" kodini qo'shing.
qo'shib qo'ydi muallif cfrick, manba

6 javoblar

You want the cond-> macro:

(defn build-string [some-integer]
  (let [question "Initial text; "]
    (cond-> question
      (= some-integer 1) (str "Add string one")
      (= some-integer 2) (str "Add string two")
      (= some-integer 3) (str "Add string three"))))

(build-string 1) => "Initial text; Add string one"
(build-string 2) => "Initial text; Add string two"
(build-string 3) => "Initial text; Add string three"

ammo bu holatda oddiy kod cond ishlaydi:

(defn build-string [some-integer]
  (let [question "Initial text; "]
    (cond
      (= some-integer 1) (str question "Add string one")
      (= some-integer 2) (str question "Add string two")
      (= some-integer 3) (str question "Add string three"))))

@cfrick yaxshi fikr beradi:

(defn build-string-map [some-integer]
  (let [question "Initial text; "
        data     {1 "Add string one"
                  2 "Add string two"
                  3 "Add string three"}
        suffix   (get data some-integer)
        result   (str question suffix)]
    result))


(build-string-map 1) => "Initial text; Add string one"
(build-string-map 2) => "Initial text; Add string two"
(build-string-map 3) => "Initial text; Add string three"

Sizga ishonch hosil qiling

2
qo'shib qo'ydi
cond -> , shartli jiringlashni yon tümcesi sifatida istagan paytda foydalidir. Savol bir narsani tanlash uchun edi.
qo'shib qo'ydi muallif Svante, manba

You want the cond-> macro:

(defn build-string [some-integer]
  (let [question "Initial text; "]
    (cond-> question
      (= some-integer 1) (str "Add string one")
      (= some-integer 2) (str "Add string two")
      (= some-integer 3) (str "Add string three"))))

(build-string 1) => "Initial text; Add string one"
(build-string 2) => "Initial text; Add string two"
(build-string 3) => "Initial text; Add string three"

ammo bu holatda oddiy kod cond ishlaydi:

(defn build-string [some-integer]
  (let [question "Initial text; "]
    (cond
      (= some-integer 1) (str question "Add string one")
      (= some-integer 2) (str question "Add string two")
      (= some-integer 3) (str question "Add string three"))))

@cfrick yaxshi fikr beradi:

(defn build-string-map [some-integer]
  (let [question "Initial text; "
        data     {1 "Add string one"
                  2 "Add string two"
                  3 "Add string three"}
        suffix   (get data some-integer)
        result   (str question suffix)]
    result))


(build-string-map 1) => "Initial text; Add string one"
(build-string-map 2) => "Initial text; Add string two"
(build-string-map 3) => "Initial text; Add string three"

Sizga ishonch hosil qiling

2
qo'shib qo'ydi
cond -> , shartli jiringlashni yon tümcesi sifatida istagan paytda foydalidir. Savol bir narsani tanlash uchun edi.
qo'shib qo'ydi muallif Svante, manba

Sizning cond shaklingiz yaxshi, lekin case dan foydalanishingiz mumkin:

(defn build-string [some-integer]
  (str "Initial text"
       (case some-integer
         1 "Add string one"
         2 "Add string two"
         3 "Add string three")))

Sizning "o'zgarmas magistral savol": sizning JavaScript-ni versiyangizdan farqli o'laroq, siz yoki men foydalanadigan operatorlarning hech biri ularning argumentlarini o'zgartirmagan. Masalan, Clojure ning str yangi qatorni tashkil qiladi, lekin JavaScript ning + = o'zgaruvchiga o'zgartiradi. Siz xavotirlanmasangiz kerak: bu Clojuredagi narsalarni o'zgartirish uchun xato »emas, balki sizni diqqat bilan kuzatib borishingiz kerak, aksincha til uni birinchi navbatda qilishni qiyinlashtiradi. Agar standart operatorlar yordamida oddiy funktsiyani ko'rsangiz, unda xavfli narsalar qilish juda qiyin.

2
qo'shib qo'ydi

Sizning cond shaklingiz yaxshi, lekin case dan foydalanishingiz mumkin:

(defn build-string [some-integer]
  (str "Initial text"
       (case some-integer
         1 "Add string one"
         2 "Add string two"
         3 "Add string three")))

Sizning "o'zgarmas magistral savol": sizning JavaScript-ni versiyangizdan farqli o'laroq, siz yoki men foydalanadigan operatorlarning hech biri ularning argumentlarini o'zgartirmagan. Masalan, Clojure ning str yangi qatorni tashkil qiladi, lekin JavaScript ning + = o'zgaruvchiga o'zgartiradi. Siz xavotirlanmasangiz kerak: bu Clojuredagi narsalarni o'zgartirish uchun xato »emas, balki sizni diqqat bilan kuzatib borishingiz kerak, aksincha til uni birinchi navbatda qilishni qiyinlashtiradi. Agar standart operatorlar yordamida oddiy funktsiyani ko'rsangiz, unda xavfli narsalar qilish juda qiyin.

2
qo'shib qo'ydi

Agar sizda faqat "teng raqamli" tekshiruvlar mavjud bo'lsa, men faqatgina xaritaga boraman. Masalan,

(str "Initial text" ({1 "Add string one" 2 "Add string two" 3 "Add string three"} some-integer))

Yoki faqat condp bilan boring. Masalan,

(defn build-string  
  [some-integer]
  (str "Initial text"
       (condp = some-integer
         1 "Add string one"
         2 "Add string two"
         3 "Add string three"
         nil)))

(map build-string (range 4))
; => ("Initial text" "Initial textAdd string one" "Initial textAdd string two" "Initial textAdd string three")

Menimcha, bu erda asosiy nuqta takrorlashni bartaraf etish; faqat "uzunlik" ni yo'qotibgina qolmay, kodingizning "kengligi" ni ham yo'q qil.

1
qo'shib qo'ydi

Agar sizda faqat "teng raqamli" tekshiruvlar mavjud bo'lsa, men faqatgina xaritaga boraman. Masalan,

(str "Initial text" ({1 "Add string one" 2 "Add string two" 3 "Add string three"} some-integer))

Yoki faqat condp bilan boring. Masalan,

(defn build-string  
  [some-integer]
  (str "Initial text"
       (condp = some-integer
         1 "Add string one"
         2 "Add string two"
         3 "Add string three"
         nil)))

(map build-string (range 4))
; => ("Initial text" "Initial textAdd string one" "Initial textAdd string two" "Initial textAdd string three")

Menimcha, bu erda asosiy nuqta takrorlashni bartaraf etish; faqat "uzunlik" ni yo'qotibgina qolmay, kodingizning "kengligi" ni ham yo'q qil.

1
qo'shib qo'ydi