我知道您可以使用以下types
来扩展
object, array, function, string, nil
即
(extend-type nil Functor
(fmap
([_ _] nil)
([_ _ _] nil)))
我希望为原生日期对象做同样的事情。怎么做的?
也..还有我缺少的小写类型吗?
答案 0 :(得分:4)
以下是如何扩展Date对象:
(defprotocol Functor
(fmap [_]))
(extend-type js/Date
Functor
(fmap
([_] (.log js/console 42))))
(fmap (js/Date.)) ;; logs 42
nil,string,number,array,function,boolean,default
来自himera(http://himera.herokuapp.com/synonym.html):
;; In addition native JavaScript objects like
;; Function, Object, Array, Number, String
;; are never actually directly extended
;; For example say you'd like to use RegExps
;; as functions
(extend-type js/RegExp
IFn
(-invoke
([this s]
(re-matches this s))))
(filter #"foo.*" ["foo" "bar" "foobar"])
;; => ("foo" "foobar")
;; This is precisely how callable collections
;; are implemented.