Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Clojure Programming/Examples/API Examples/Function Tools

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

fn

[edit |edit source]
(map(fn[a](str"hi "a))["mum""dad""sister"]); => ("hi mum" "hi dad" "hi sister")

#()

[edit |edit source]

See thereader page, (Macro characters -> Dispatch -> Anonymous function literal) for an explanation of the '%' and other characters used to refer to function arguments.

user=>(defsq#(*%%))#'user/squser=>(sq3)9user=>(map#(class%)[1"asd"])(java.lang.Integerjava.lang.String)user=>

"lambda"

[edit |edit source]

Use fn, or even better there is a custom syntax to create an anonymous function:

(map#(str"hi "%)["mum""dad""sister"])

%

[edit |edit source]

Represents an optional argument to an unnamed function:

#(+2%)#(+2%1%2)

Arguments in the body are determined by the presence of argument literals taking the form %, %n or %&. % is a synonym for %1, %n designates the nth arg (1-based), and %& designates a rest arg.

complement

[edit |edit source]

Usage: (complement f)

Takes a fn f and returns a fn that takes the same arguments as f,has the same effects, if any, and returns the opposite truth value.

user=>(defnsingle-digit[x](<x10))#'user/single-digituser=>(single-digit9)trueuser=>(single-digit10)falseuser=>(defmulti-digit(complementsingle-digit))#'user/multi-digituser=>(multi-digit9)falseuser=>(multi-digit10)true
Retrieved from "https://en.wikibooks.org/w/index.php?title=Clojure_Programming/Examples/API_Examples/Function_Tools&oldid=3678634"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp