Clojure cond

 

cond
(cond & clauses)
后跟一组测试求值表达式 / 表达式对( test/expr pairs ),逐次一次一个的求值。如果某个测试求值表达式 返回逻辑真,返回对应表达式的值,并且不再对下面剩下的测试求值表达式或对应表达式求值。如果 没有指定任何测试求值表达式 / 表达式对( test/expr pairs ),也就是这种形式: (cond) ,将返回 nil
例子:
user=> (def i 100)
#'user/i
user=> (cond
  #_=>   (< i 0) (str i " < 0")
  #_=>   (and (>= i 0) (< i 10)) (str i " >= 0 and " i " < 10")
  #_=>   (and (>= i 10) (< i 100)) (str i " >= 10 and " i " < 100")
  #_=>   (and (>= i 100) (< i 1000)) (str i " >= 100 and " i " < 1000")
  #_=>   true (str i " >= 1000"))
"100 >= 100 and 100 < 1000 «
 
user=> (cond)
nil
 
 
(ns clojure.wf.core.SimpleScalaControlFlow
  (:gen-class
    :implements [com.wf.core.AbstractControlFlow]
    :init constructor
    :constructors {[String] []}))

(import java.util.function.Function)
(import com.wf.core.internal.ScalaEngine)

(def script nil)

(defn -constructor
  [_script]
  (println *ns*)
  (def script _script)
  [[] (atom {:location "default"})])

(defn result
  ;;
  [Input]
  (def engine (ScalaEngine/get))
  (.put engine "input" Input)
  ;(def value (.eval engine (str "var _input = \"test\"\n" "_input == input")))
  (def value (.eval engine script))
  (println "the class of value:", (.getClass value))
  (cond
    (= value nil) false
    (not (.isInstance Boolean value)) false
    :else (.booleanValue value)))


(defn -getFunctionExpress
  ;; fn .getFunctionExpress
  [this]
  (let [functionExpress
        (proxy [Function] []
          (apply
            [t]
            (result t)))]
    functionExpress))
 
 
(ns clojure.wf.core.SimpleScalaControlFlowTest
  (:require [clojure.test :refer :all]))

(import clojure.wf.core.SimpleScalaControlFlow)

(deftest getFunctionExpress
  (println "testing getFunctionExpress")
  (def simplescala (SimpleScalaControlFlow. (str "var _input = \"test\"\n" "_input == input")))
  (def functionExpress (.getFunctionExpress simplescala))
  (println (.getClass functionExpress))
  ; call fn apply
  (def result (.apply functionExpress "what..."))
  (println result)
  (testing "the eval result of process of flow:control is false!"
    (is (not result))))
 
 
>lein test :only clojure.wf.core.SimpleScalaControlFlowTest

lein test clojure.wf.core.SimpleScalaControlFlowTest
testing getFunctionExpress
#object[clojure.lang.Namespace 0x31920ade user]
clojure.wf.core.SimpleScalaControlFlow.proxy$java.lang.Object$Function$ac181173
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.
 
 
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值