Question

I know that you can use the following types to extend

object, array, function, string, nil

i.e.

(extend-type nil Functor
 (fmap
   ([_ _] nil)
   ([_ _ _] nil)))

I'm hoping to do the same for the native date object. how is that done?

also.. are there anymore lowercase types that I'm missing?

Était-ce utile?

La solution

Here is how to extend Date object:

(defprotocol Functor
  (fmap [_]))

(extend-type js/Date
  Functor
  (fmap
    ([_] (.log js/console 42))))

(fmap (js/Date.))      ;; logs 42

A list of lowercase types (from https://github.com/clojure/clojurescript/blob/202cfcf045cf86d3ab295cbf16a347569b652647/src/cljs/clojure/data.cljs):

nil, string, number, array, function, boolean, default

From 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.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top