Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Clojure Programming/Examples/API Examples/Sequences

From Wikibooks, open books for an open world
<Clojure Programming |Examples |API Examples

interpose

[edit |edit source]
user=>(applystr(interpose"|"["hi""mum""and""dad"]))"hi|mum|and|dad"

interleave

[edit |edit source]
user=>(interleave[123][:a:b:c])(1:a2:b3:c)

reverse

[edit |edit source]
user=>(reverse[123])(321)user=>(applystr(interpose" "(reverse(.split"I am cold"" "))))"cold am I"

butlast

[edit |edit source]
user=>(butlast"hello")(\h\e\l\l)

next

[edit |edit source]
user=>(next[12345])(2345)

nfirst

[edit |edit source]
user=>(nfirst[[123][456][789]])(23)

nnext

[edit |edit source]
user=>(nnext[12345])(345)

second

[edit |edit source]
user=>(second[:a\b"c"])\b

nth

[edit |edit source]
user=>(nth[12345]3)4

replace

[edit |edit source]
user=>(replace{\l""}"hello world")(\h\e""""\o\space\w\o\r""\d)user=>(applystr(replace{\l""}"hello world"))"heo word"

Sequence Building

[edit |edit source]

conj

[edit |edit source]
user=>(conj[123]4)[1234]user=>(conj'(:a:b:c)\d)(\d:a:b:c)

concat

[edit |edit source]
(defnpoly-expandpoly-expand[points](loop[aa(firstpoints)remaining(restpoints)built(emptypoints)](if(empty?remaining)(concatbuilt[aa(firstpoints)])(recur(firstremaining)(restremaining)(concatbuilt[aa(firstremaining)])))))(poly-expand'[abcd])->[abbccdda]

merge

[edit |edit source]

Combine two maps into a more massive map

(merge{:a1}{:b2})=>{:a1,:b2})(merge-with+{:a1}{:a2,:b3})=>{:a3:b3}

Mapping Operators

[edit |edit source]

map

[edit |edit source]
user=>(map+[1234][1234])(2468)

reduce

[edit |edit source]
user=>(reduce*[234])24; sum the odd numbers to 100;; Not so beautiful code, see next example for a better approach(reduce#(+%1(if(=1(rem%22))%20))(range100)); sum the odd numbers to 100 (Cleaner version)(reduce+(filterodd?(range100)))

apply

[edit |edit source]
user=>(applystr[12]); equivalent to (str 1 2)"12"user=>(let[pair(fn[ab](stra" 'n "b))](applypair["M""Ms"]))"M 'n Ms"user=>(defnfactorial[n](apply*(range2(incn))))#'user/factorialuser=>(factorial5)120
Retrieved from "https://en.wikibooks.org/w/index.php?title=Clojure_Programming/Examples/API_Examples/Sequences&oldid=3676591"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp