172 lines
4.4 KiB
Clojure
172 lines
4.4 KiB
Clojure
(ns List
|
|
"Elixir List module — operations on linked lists.
|
|
|
|
In CljElixir: (List/flatten nested), (List/to-tuple lst), etc.
|
|
Lists are the fundamental sequence type on the BEAM (singly-linked).")
|
|
|
|
(defn first
|
|
"Returns the first element, or `default` if empty.
|
|
(List/first [1 2 3]) ;=> 1
|
|
(List/first [] :none) ;=> :none"
|
|
([list])
|
|
([list default]))
|
|
|
|
(defn last
|
|
"Returns the last element, or `default` if empty.
|
|
(List/last [1 2 3]) ;=> 3
|
|
(List/last [] :none) ;=> :none"
|
|
([list])
|
|
([list default]))
|
|
|
|
(defn flatten
|
|
"Flattens nested lists.
|
|
(List/flatten [[1 [2]] [3 4]]) ;=> [1 2 3 4]
|
|
(List/flatten [1 [2 [3]]] 1) ;=> [1 2 [3]] (one level only)"
|
|
([list])
|
|
([list tail]))
|
|
|
|
(defn foldl
|
|
"Left fold. Invokes `fun` for each element with accumulator.
|
|
(List/foldl [1 2 3] 0 (fn [elem acc] (+ acc elem))) ;=> 6"
|
|
[list acc fun])
|
|
|
|
(defn foldr
|
|
"Right fold.
|
|
(List/foldr [1 2 3] 0 (fn [elem acc] (+ acc elem))) ;=> 6"
|
|
[list acc fun])
|
|
|
|
(defn wrap
|
|
"Wraps a non-list value in a list. nil becomes []. Lists pass through.
|
|
(List/wrap 1) ;=> [1]
|
|
(List/wrap nil) ;=> []
|
|
(List/wrap [1 2]) ;=> [1 2]"
|
|
[term])
|
|
|
|
(defn duplicate
|
|
"Creates a list with `elem` repeated `n` times.
|
|
(List/duplicate :ok 3) ;=> [:ok :ok :ok]"
|
|
[elem n])
|
|
|
|
(defn zip
|
|
"Zips corresponding elements from multiple lists into tuples.
|
|
(List/zip [[1 2 3] [:a :b :c]]) ;=> [{1 :a} {2 :b} {3 :c}]"
|
|
[list-of-lists])
|
|
|
|
(defn insert-at
|
|
"Inserts `value` at `index`.
|
|
(List/insert-at [1 2 3] 1 :a) ;=> [1 :a 2 3]"
|
|
[list index value])
|
|
|
|
(defn replace-at
|
|
"Replaces element at `index` with `value`.
|
|
(List/replace-at [1 2 3] 1 :a) ;=> [1 :a 3]"
|
|
[list index value])
|
|
|
|
(defn update-at
|
|
"Updates element at `index` by applying `fun`.
|
|
(List/update-at [1 2 3] 1 (fn [x] (* x 10))) ;=> [1 20 3]"
|
|
[list index fun])
|
|
|
|
(defn delete-at
|
|
"Deletes element at `index`.
|
|
(List/delete-at [1 2 3] 1) ;=> [1 3]"
|
|
[list index])
|
|
|
|
(defn delete
|
|
"Deletes the first occurrence of `element`.
|
|
(List/delete [1 2 1 3] 1) ;=> [2 1 3]"
|
|
[list element])
|
|
|
|
(defn pop-at
|
|
"Returns the element at `index` and the list without it.
|
|
(List/pop-at [1 2 3] 1) ;=> {2 [1 3]}"
|
|
([list index])
|
|
([list index default]))
|
|
|
|
(defn starts-with?
|
|
"Returns true if `list` starts with `prefix`.
|
|
(List/starts-with? [1 2 3] [1 2]) ;=> true"
|
|
[list prefix])
|
|
|
|
(defn myers-difference
|
|
"Returns the edit steps to transform `list1` into `list2`.
|
|
(List/myers-difference [1 2 3] [1 3 4]) ;=> [eq: [1], del: [2], eq: [3], ins: [4]]"
|
|
([list1 list2])
|
|
([list1 list2 diff-script]))
|
|
|
|
(defn to-tuple
|
|
"Converts a list to a tuple.
|
|
(List/to-tuple [1 2 3]) ;=> #el[1 2 3]"
|
|
[list])
|
|
|
|
(defn to-string
|
|
"Converts a charlist to a string.
|
|
(List/to-string [104 101 108 108 111]) ;=> \"hello\""
|
|
[charlist])
|
|
|
|
(defn to-charlist
|
|
"Converts a list of codepoints to a charlist."
|
|
[list])
|
|
|
|
(defn to-integer
|
|
"Converts a charlist to integer.
|
|
(List/to-integer '123') ;=> 123"
|
|
([charlist])
|
|
([charlist base]))
|
|
|
|
(defn to-float
|
|
"Converts a charlist to float."
|
|
[charlist])
|
|
|
|
(defn to-atom
|
|
"Converts a charlist to atom."
|
|
[charlist])
|
|
|
|
(defn to-existing-atom
|
|
"Converts a charlist to an existing atom."
|
|
[charlist])
|
|
|
|
(defn ascii-printable?
|
|
"Returns true if all chars are ASCII printable.
|
|
(List/ascii-printable? 'hello') ;=> true"
|
|
([list])
|
|
([list limit]))
|
|
|
|
(defn improper?
|
|
"Returns true if the list is improper (doesn't end in []).
|
|
(List/improper? [1 | 2]) ;=> true"
|
|
[list])
|
|
|
|
(defn keyfind
|
|
"Finds a tuple in a list of tuples where element at `position` matches `key`.
|
|
(List/keyfind [{:a 1} {:b 2}] :b 0) ;=> {:b 2}
|
|
(List/keyfind [{:a 1}] :c 0 :default) ;=> :default"
|
|
([list key position])
|
|
([list key position default]))
|
|
|
|
(defn keystore
|
|
"Replaces or inserts tuple in list based on key at position."
|
|
[list position key new-tuple])
|
|
|
|
(defn keydelete
|
|
"Deletes tuple from list where element at position matches key."
|
|
[list key position])
|
|
|
|
(defn keymember?
|
|
"Returns true if any tuple has `key` at `position`.
|
|
(List/keymember? [{:a 1} {:b 2}] :a 0) ;=> true"
|
|
[list key position])
|
|
|
|
(defn keyreplace
|
|
"Replaces the first tuple with matching key at position."
|
|
[list key position new-tuple])
|
|
|
|
(defn keysort
|
|
"Sorts list of tuples by element at `position`.
|
|
(List/keysort [{:b 2} {:a 1}] 0) ;=> [{:a 1} {:b 2}]"
|
|
[list position])
|
|
|
|
(defn keytake
|
|
"Takes all tuples from list where element at position matches key."
|
|
[list key position])
|