2

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?

1 Answer 1

4

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.