clojure-basic syntax-function definition
July 21:12 PM
1. Create function 1.fn anonymous function
Examples are as follows:
"hello")#<user$eval756$fn__757 [email protected]413e8>"hello"))"hello""hello") ; 带参数"hello"user=>
The short function can use # (),% to indicate the unique parameter;% 1,%2. Represents the 1th, 2 、.. A parameter;%& represents all parameters, as follows:
user=> (#(/ % 3) 4)4/3user=> (#(/ % 3) 4);结果为4/34/3user=> (#(/ %2 %1) 3 4);结果为4/34/3user=> (#(apply / %&) 3 5 7);结果为3/5/73/35user=>
2.defn public function is visible outside NS
Examples are as follows:
(defn f2 [x] (format "hello %s" x));定义一个参数函数(f2 "girl")"hello girl"
User=>(defn F4 "F4 function" [] (println "F4 function ")); a function with annotations# ' user/f4User=>(F4)F4Functionniluser=>(Doc F4); Comment Information through the doc view function-------------------------user/f4([])F4Functionnil
user=> (defn f5 ( [] (str " no parameter ") ) #_ = ([name] ( str "My name is" name) ) ) ; define overloaded functions # ' user/f5user=> (f5 ) "no parameter" user= > (f5 "Clojure" ) "My name is Clojure" USER=>
(defn f1 [& a] a);定义变参函数(f1 1 2 3)(1 2 3)
User=>(defn m [& Arg] (str arg , size= " (Count Arg))) ; Define a variable parameter function# ' user/muser=>(m 1 2 3 4 5)"(1 2 3 4 5), size=5 "user=>(m "a" 1 2.3 -1)"(\"a\" 1 2.3-1), size=4"user=>
user=> (defn F [a F1 b f2 c] (f2 (f1 a B) c) ) # ' user/fuser=> (f 5 - 2 + 3 ) 6
(defn f [a] (fn [b] (- a b)));函数作为返回值((f 7) 4)3user=>
3.defn-Private Function not visible outside NS
Examples are as follows:
User=>(ns test1)The NS means to switch to the specified namespace, and if it does not exist, create a new namespaceNiltest1=>(defn- foo [] "World"); define private function foo, return string World# ' test1/footest1=>(defn bar [] (str "Hello" (foo))) define the public function bar and call the Private function foo# ' test1/bartest1=>test1=>(foo)The foo function is called in the current namespace"World" test1=>(bar)The bar function is called within the current namespace"Hello World" test1=>test1=>test1=>(ns test2); switch to the Test2 namespaceNiltest2=>(test1/bar); Call the bar function of the Test1 namespace to return to success"Hello World" test2=>(test1/foo)called the Foo function of the Test1 namespace, an exception occurred, prompting test1 that the Foo function is not publicCompilerexception java.lang.IllegalStateException:var: # ' Test1/foo isn't public, compiling:(no_source_path: 1: 1)Test2=>(ns user)Niluser=>
4. Comp Combination Functions
Shaped like:
(Comp F1 F2. fn) arg1 arg2: argn)
is to perform all functions on a right-to-left combination of parameters, which can be transformed into:
(F1 (F2 (.. (FN arg1 arg2. argn))))
Examples are as follows:
Calculation-(2 * 4)
user=> (defn f [x y] (- (* x y)));使用defn定义函数方式;;#user/fuser=> (f 2 4);;-8
user=> (def fc (comp - *));使用comp定义组合函数方式;;#user/fcuser=> (fc 2 4);;-8
5.partial partial function or "partial complete function", because it is incomplete, the definition also uses def instead of Defn.
Shaped like:
(Partial f arg1 arg2: argn) Arga ARGB: Argz)
is to execute:
(f arg1 arg2: argn arga ARGB: Argz)
Examples are as follows:
user=> (defn f [n] (* n 10));正常函数;;#‘user/fuser=> (f 2);;20
;;user=> (def fp (partial * 10));偏函数;;#‘user/fpuser=> (fp 2);;20
The 6.constantly constant function takes a parameter x and returns a variable parameter function that returns x regardless of the number of arbitrary arguments passed in when the parameter function is called.
user=> (def consf (constantly "a"))(consf 1 2 3)(consf [1 2 3])"a"user=>
2. Call function 1.-> also known as "thread" macro: Call a series of functions, the latter function iterates using the previous function to return the result as a 1th entry, return the last function call value
user=> (first ( (.replace (.touppercase "a b c D" ) "A" "X" ) ) ) " X "user=> -> " a B c D " . toUpperCase (.replace "A" "X" ) (.split ) first) "X" USER=>
2.->> calls a series of functions that return the last function call value using the previous function to return the result as the last entry parameter
User=>( -- 3 (/ 4))3/4User=>( - 3 (/ 4) (/ 5 ))3/20user=>(->> 3 (/ 4))4/3user=>(->> 3 (/ 4) (/ 5 ))15/4
The 3.eval function parses an expression data structure (not a string!) and returns the result.
Examples are as follows:
User=>(Doc eval)-------------------------Clojure.core/eval([Form])Evaluates the form data structure( not text!)and returns the Result.niluser=>(eval (+ 1 2))3user=>(doc read-string)-------------------------clojure.core/read-string([s])Reads one object from the string s. Note that read-string can execute code(controlled by *read-eval*), and as such should is used only with trusted sources. For data structure Interop use Clojure.edn/read-stringniluser=>(read-string "(println 1)")(println 1)User=>(eval (read-string "(println 1)" )1niluser=>
4The. Apply function assigns all elements inside a collection to the following function as arguments.
Examples are as follows:
(apply + [1 2 3 4])10
3. function check fn?
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
clojure-basic syntax-function definition