Clojure中序列中元素的排名

时间:2019-05-22 20:55:55

标签: clojure sequence seq

我对诸如 Read the input image and get the bottom right corner coordinate Read it again. Clone it, fill it with black, draw a round rectangle, blur it, apply -level Then put the result into the alpha channel of the input image wm1=`convert thumbnail.gif -format "%[fx:w-1]" info:` hm1=`convert thumbnail.gif -format "%[fx:h-1]" info:` convert thumbnail.gif \ \( -clone 0 -fill black -colorize 100 \ -fill white -virtual-pixel black -draw "roundrectangle 0,0 $wm1,$hm1 15,15" \ -blur 0x10 -level 50x100% \) \ -alpha off -compose CopyOpacity -composite \ result2.png

,并希望以一种巧妙的方式将此序列转换为类似 ([:Jon 326] [:Mary 233] [:Matthew 255])

:name应该是字符串类型,而:rank必须是整数

排名是通过数字计算的,与序列中其他元素的排名相比,“点”越高,排名越高

1 个答案:

答案 0 :(得分:5)

您可以使用sort-by对输入序列进行排序,然后使用map-indexed将其转换为等级:

(def input '([:Jon 326] [:Mary 233] [:Matthew 255]))

(map-indexed (fn [idx p]
               {:name (name (first p))
                :rank (inc idx)})
             (sort-by second > input))

或者您可以使用线程最后宏:

 (->> input 
      (sort-by second >) 
      (map-indexed (fn [idx p] {:name (name (first p)) :rank (inc idx)})))

您还可以将函数中的货币对解构为map-indexed,而不是使用first

(->> input 
     (sort-by second >) 
     (map-indexed (fn [idx [n _]] {:name (name n) :rank (inc idx)})))