iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 21
0
自我挑戰組

各種筆記系列 第 21

[Kata] Clojure - Day 21

  • 分享至 

  • xImage
  •  

Testing 1-2-3

Your team is writing a fancy new text editor and you've been tasked with implementing the line numbering. Write a function which takes a list of strings and returns each line prepended by the correct number. The numbering starts at 1. The format is n: string. Notice the colon and space in between.

將給予的 list 中的每個 elements 加上序號(從1開始)及:

Example

number(List<string>()) // => List<string>()
number(List<string>{"a", "b", "c"}) // => ["1: a", "2: b", "3: c"]

Solution

(ns line-numbers)

(defn number [lines]
  (map-indexed (fn [i s] (str (inc i) ": " s)) lines)
)

上一篇
[Kata] Clojure - Day 20
下一篇
[Kata] Clojure - Day 22
系列文
各種筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言