git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Prelude additions

Either instance for Functor doesn't work at the moment

commit8749c70bf0d3cc321863b7b0d7560fe7d00aafb0 authorGary Burgess <garyb@users.noreply.github.com> dateSat 8 Feb 2014, 21:29 (13 years ago) parent1238f29
1 file changed+46−2
prelude/prelude.purs+46−2
prelude/prelude.purs+46−2
@@ -54,6 +54,13 @@ module Prelude where
5454 instance Read Boolean where
5555 read "true" = true
5656 read _ = false
57+
58+ foreign import readNumber "function readNumber(n) {\
59+ \ return parseInt(n, 10);\
60+ \}" :: String -> Number
61+
62+ instance Read Number where
63+ read = readNumber
5764
5865 infixl 4 <$>
5966
@@ -362,6 +369,10 @@ module Monoid where
362369 instance Monoid String where
363370 mempty = ""
364371 (<>) = (++)
372+
373+ instance Monoid [a] where
374+ mempty = []
375+ (<>) = Arrays.concat
365376
366377 mconcat :: forall m. (Monoid m) => [m] -> m
367378 mconcat [] = mempty
@@ -429,22 +440,52 @@ module Maybe where
429440
430441 fromMaybe :: forall a. a -> Maybe a -> a
431442 fromMaybe a = maybe a (Prelude.id :: forall a. a -> a)
443+
444+ instance Prelude.Functor Maybe where
445+ (<$>) fn (Just x) = Just (fn x)
446+ (<$>) _ _ = Nothing
447+
448+ instance Prelude.Applicative Maybe where
449+ pure = Just
450+ (<*>) (Just fn) x = fn <$> x
451+ (<*>) Nothing _ = Nothing
432452
433453 instance Prelude.Monad Maybe where
434454 return = Just
435455 (>>=) m f = maybe Nothing f m
456+
457+ instance (Show a) => Prelude.Show (Maybe a) where
458+ show (Just x) = "Just " ++ (show x)
459+ show Nothing = "Nothing"
436460
437461module Either where
438462
463+ import Prelude
464+
439465 data Either a b = Left a | Right b
440466
441467 either :: forall a b c. (a -> c) -> (b -> c) -> Either a b -> c
442468 either f _ (Left a) = f a
443469 either _ g (Right b) = g b
470+
471+ {-
472+ instance Prelude.Functor (Either a) where
473+ (<$>) _ (Left x) = Left x
474+ (<$>) f (Right y) = Right (f y)
475+ -}
476+
477+ instance Prelude.Applicative (Either e) where
478+ pure = Right
479+ (<*>) (Left e) _ = Left e
480+ (<*>) (Right f) r = f <$> r
444481
445482 instance Prelude.Monad (Either e) where
446483 return = Right
447484 (>>=) = either (\e _ -> Left e) (\a f -> f a)
485+
486+ instance (Show a, Show b) => Prelude.Show (Either a b) where
487+ show (Left x) = "Left " ++ (show x)
488+ show (Right y) = "Right " ++ (show y)
448489
449490module Arrays where
450491
@@ -578,7 +619,7 @@ module Arrays where
578619 range lo hi = {
579620 var ns = [];
580621 for (n <- lo until hi) {
581− ns = push ns n;
622+ ns = push ns n;
582623 }
583624 return ns;
584625 }
@@ -598,7 +639,10 @@ module Arrays where
598639 instance (Prelude.Show a) => Prelude.Show [a] where
599640 show [] = "[]"
600641 show (x:xs) = show x ++ " : " ++ show xs
601−
642+
643+ instance Prelude.Functor [] where
644+ (<$>) = map
645+
602646 instance Prelude.Monad [] where
603647 return = singleton
604648 (>>=) = concatMap