•
fn
(fn name? [params*] exprs*)
(fn name? ([params*] exprs*) +)
定义一个函数
如果是匿名函数,可以不指定
name
返回的是一个
form,这个form就是定义该函数的form,但还不能调用。如:
=> (fn ScalaEvalFunction3 #_=> ;; with specific script #_=> [script] #_=> (println "a")) #object[clojure.examples.hello$eval1433$ScalaEvalFunction3__1434 0x5206605e "clojure.examples.hello$eval1433$ScalaEvalFunction3__1434@5206605e"]
如果需要调用的话,应该:
=> (def fx (fn ScalaEvalFunction3 #_=> ;; with specific script #_=> [script] #_=> (println "a"))) #'clojure.examples.hello/fx => (fx "1") a nil
定义一个匿名函数,该函数传入一个参数
x,
函数返回
x*x
。同时传入
3
执行该函
数
user=> ((fn [x] (* x x)) 3)
9
等价于:
=> (def fx (fn [x] (* x x)))
#'clojure.examples.hello/fx
=> (fx 3)
9
将
1..10
数字
put
到
map
中,并指定一个匿名回调函数,该函数接收
put
到
map
的每
个数字,执行
x*x
后
put
到
map
中
user=> (map (fn [x] (* x x)) (range 1 10))
(1 4 9 16 25 36 49 64 81)
等价于:
=> (def r (range 1 10))
#'clojure.examples.hello/r
=> (def fx (fn [x] (* x x)))
#'clojure.examples.hello/fx
=> (map fx r)
(1 4 9 16 25 36 49 64 81)
<!--[if ppt]-->
•<!--[endif]-->
第一种形式:
(fn name? [params*] exprs*)
(def ScalaEvalFunction (fn ;; with specific script [script] (let [functionExpress (proxy [Function] [] (apply [t] (clojure.wf.core/_eval script t)))] functionExpress)))
第二种形式:
(fn name? ([params*] exprs*) +)
(def ScalaEvalFunction (fn ;; with specific script ([script] (let [functionExpress (proxy [Function] [] (apply [t] (clojure.wf.core/_eval script t)))] functionExpress)) ;; with the specific script and args ;; param args not use. ([script args] (let [functionExpress (proxy [Function] [] (apply [t] (clojure.wf.core/_eval script t)))] functionExpress))))
一个完整的例子:
;;;; (ns clojure.wf.corex1 (import java.util.function.Function) (:require [clojure.wf.core]) (:gen-class)) (def ScalaEvalFunction (fn ;; with specific script ([script] (let [functionExpress (proxy [Function] [] (apply [t] (clojure.wf.core/_eval script t)))] functionExpress)) ;; with the specific script and args ;; param args not use. ([script args] (let [functionExpress (proxy [Function] [] (apply [t] (clojure.wf.core/_eval script t)))] functionExpress))))
(ns clojure.wf.corex1test (:require [clojure.test :refer :all] [clojure.wf.corex1])) ;(refer 'clojure.wf.corex1 :only '[ScalaEvalFunction]) (deftest ScalaEvalFunctionTest (def input "what...") (def script (str "var _input = \"test\"\n" "_input == input")) (def tfn (clojure.wf.corex1/ScalaEvalFunction script)) ; call fn apply (def result (.apply tfn input)) (println result) (testing "[OK] it's ok." (is (not result))))
>lein test :only clojure.wf.corex1test lein test clojure.wf.corex1test log4j:WARN No appenders could be found for logger (com.wf.core.internal.ScalaEngine). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. input: Object = what... the class of value: java.lang.Boolean false Ran 1 tests containing 1 assertions. 0 failures, 0 errors.
Clojure映射到Java类的成员,包括变量和方法
;;;; default control flow ;;;; ;;;; the DefaultControlFlow (ns) is a default implementation of com.wf.core.AbstractControlFlow (ns clojure.wf.core.DefaultControlFlow (import com.wf.core.AbstractControlFlow) (:gen-class :implements [com.wf.core.AbstractControlFlow] :init constructor :constructors {[com.wf.core.AbstractControlFlow] []} :methods [[getTargetObject [] com.wf.core.AbstractControlFlow]])) (def target nil) (defn -constructor [_target] (println "construct...") (def target _target) [[] (atom {:location "default"})]) (defn getFunctionExpress [_] (comment " the function express ... ") (println (format "class: %s" "DefaultControlFlow")) (if (target) (.getFunctionExpress target) nil)) (defn -getTargetObject [this] target)
(ns clojure.wf.core.DefaultControlFlowTest (import com.wf.core.TestBooleanFunction) (import clojure.wf.core.DefaultControlFlow) (import com.wf.core.SimpleControlFlow) (import com.wf.core.SimpleScalaControlFlow) (:require [clojure.test :refer :all])) (deftest getTargetObjectTest (println "testing getTargetObject") (comment " testing getTargetObject ... ") ; (def tfn (TestBooleanFunction.)) ; (def simple (SimpleControlFlow. tfn)) (def simplescala (SimpleScalaControlFlow. (str "var _input = \"test\"\n" "_input == input"))) (println ";;;;;;;;;;;;;;;;") ; call fn getFunctionExpress (def functionExpress (.getFunctionExpress simple)) (println (.getClass functionExpress)) ; call fn apply (def result (.apply functionExpress "what...")) (println result) (println ";;;;;;;;;;;;;;;;") (def _default (DefaultControlFlow. simple)) (def target (.getTargetObject _default)) (println (str "the class of target" (.getClass target))) (testing "it's ok." (is (= simple target))) (println ";;;;;;;;;;;;;;;;") (def functionExpress (.getFunctionExpress target)) (println (.getClass functionExpress)) ; call fn apply (def result (.apply functionExpress "what...")) (println result) ;; ;; ;; ;; (println ";;;;;;;;;;;;;;;;") (def functionExpress (.getFunctionExpress simplescala)) (println (.getClass functionExpress)) (def result (.apply functionExpress "what...")) (println result) (println ";;;;;;;;;;;;;;;;") (def _default (DefaultControlFlow. simplescala)) (def target (.getTargetObject _default)) (println (str "the class of target" (.getClass target))) (testing "it's ok." (is (= simplescala target))) (println ";;;;;;;;;;;;;;;;") (def functionExpress (.getFunctionExpress target)) (println (.getClass functionExpress)) ; call fn apply (def result (.apply functionExpress "what...")) (println result))
>lein test :only clojure.wf.core.DefaultControlFlowTest/getTargetObjectTest lein test clojure.wf.core.DefaultControlFlowTest testing getTargetObject test boolean function ;;;;;;;;;;;;;;;; com.wf.core.TestBooleanFunction what... true ;;;;;;;;;;;;;;;; construct... the class of targetclass com.wf.core.SimpleControlFlow ;;;;;;;;;;;;;;;; com.wf.core.TestBooleanFunction what... true ;;;;;;;;;;;;;;;; com.wf.core.SimpleScalaControlFlow$$Lambda$1/552937500 log4j:WARN No appenders could be found for logger (com.wf.core.internal.ScalaEngine). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. input: Object = what... false ;;;;;;;;;;;;;;;; construct... the class of targetclass com.wf.core.SimpleScalaControlFlow ;;;;;;;;;;;;;;;; com.wf.core.SimpleScalaControlFlow$$Lambda$1/552937500 input: Object = what... false Ran 1 tests containing 2 assertions. 0 failures, 0 errors.