[1] http://stackoverflow.com/questions/10496932/is-jquery-a-mona...
[1] http://stackoverflow.com/questions/10496932/is-jquery-a-mona...
Obviously it is trivial to show that it isn't a monad by the fact that javascript permits any action in pure code, but by the same token, unsafePerformIO proves that Haskell has no monads. I think it's fair to operate under a reasonable subset of jQuery functionality when asking the question.
If JQuery is a monad, what are the monadic operations that make it a monad? What are its equivalents of return and bind (or any other equivalent definitions of a monad)? It's not a tricky question.
Saying that JQuery is a monad without saying what its return and bind are is equivalent to saying that whistling is Turing complete without saying how you can compute stuff with whistling. The problem here is not whether whistling is Turing complete or not, the problem is what it would even mean for whistling to be Turing complete.
This means that as of now, as long as you don't tell us what are the monadic operations of JQuery, it cannot be determined whether JQuery is a monad, because this question does not make sense yet.
So $(element) implements return as a -> [a]
And bind is mapcat, which is called by most of the jQuery methods.
Yes, that's a subset of the API, but it's the subset that distinguishes jQuery from any other collection of helper functions.
jQuery may be analogous to a monad. That doesn't make it a monad.
Can you disprove that?
(P.S. you are wronger)
Monads are more specific, and it is in that specificity that you find the requirement for monads to be "polymorphic": monads are endofunctors, and the monad laws concern the composition of those endofunctors. An endofunctor is a mapping between a category and itself. When we apply this category theory to programming, we talk about a category of types. In Haskell this is the category Hask. This is where the polymorphism requirement comes into play. The monad maps from Hask to itself. That means that it must be able to map over all types, which means polymorphism.
bind f (return a) = f a
For all a and f. Therefore, by substituting f = id, and a = return x: bind id (return (return x)) = return x
If, as in jQuery, `return (return x) = return x`, then we can simplify to: bind id (return x) = return x
Now, applying the first monad law again, we get: x = return x
So return must be the identity function. But jQuery's $ is not the identity function. Contradiction. QED.No wait, I can do better. For some values, $($(x)) is the equivalent to $(x). So, for some values $(y) = y. So for the domain of y where $(y) = y, $() is the identity function, and now it's a monad.
But yes, presto magico. I am outwitting your narrow-minded mathematics.
return :: a -> m a
in the jQuery monad?First I thought
return = jQuery
but $(".yo") :: [HTMLElement]
isnt return ".yo" :: JQuery String
I'd probably model it like this: (|>) = flip ($)
jQuery "selector" |> css "color" "red" |> filter someSelector |> css "color" "blue"
which would turn into: css "color" "blue" (filter someSelector (css "color "red" (jQuery "selector")))
class Selector a where
instance Selector Text where
instance Selector HtmlElement where
jQuery :: Selector a -> JQuery
css :: Text -> Text -> JQuery -> JQuery
filter :: Selector a -> JQuery -> JQueryreturn [Integer] will give you [[Integer]] (if you're inside the list Monad).
$($("#some-id")) will give you the same result as $("#some-id"), because $ of a $ doesn't nest (or make sense, really).
Therefore jQuery doesn't satisfy the Monad laws :).