1 minute read

今年真的開始寫一點 clojure 了,稍微刷個題,記錄一下。 放個 2023 年寫到第 23 天斷賽的文章:從零開始的 clojure

先來個 2025 年鐵人賽 30 天的完賽感言和筆記(內容 from day30):

完賽感言

這次終於完賽啦,實際上只寫了 28 題 (扣掉 Day 0 & day 4 番外篇)。 大部分題目是今年四月開始寫,慢慢累積起來的。但後面大概七八題是鐵人賽開始才寫的。

最後。根據這 30 天寫的內容,整理一下幾個我最常用的方法。

處理集合

  • map: transforming a collection and returning a new lazy sequence.
  • reduce: aggregating a collection into a single accumulated value.
  • filter: selecting elements from a collection based on a predicate, returning a new lazy sequence.
  • apply: applying functions to collections (it expands the last collection into multiple arguments).
  • *lazy sequenc: A sequence whose elements are only computed when they are needed.

處理字串

  • split: splits a string into a sequence of substrings, based on a given pattern (usually a regex).
  • join: joins a collection of strings into a single string, optionally with a separator.
  • replace: replaces all instances of a substring or regex match in a string with a new value.

取得元素

  • first: returns the first element of a collection (If the collection is empty, it returns nil).
  • rest: returns a sequence of all elements except the first.

另外,也很常使用 thread last

  • ->>: takes a value and threads it as the last argument into each following form.

特別適合用在集合處理(搭配上面提到的 map、filter、reduce 等等),因為這些 function 的參數通常在最後。就可以很自然地把一個資料流一步步處理。


最後,寫題之餘,放一隻想看的影片,提醒自己之後要看。

“Simple Made Easy” — Rich Hickey (2011)

Clojure 30 days 系列

這是我在 iT 邦幫忙鐵人賽「Clojure 30 Days」系列的完整索引!