|
| 1 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 2 | + |
| 3 | +module Functora.Miso.Orphan () where |
| 4 | + |
| 5 | +import Data.JSString (JSString) |
| 6 | +import qualified Data.JSString as JS |
| 7 | +import qualified Data.JSString.Text as JST |
| 8 | +import Data.Monoid.Factorial |
| 9 | +import Data.Monoid.GCD |
| 10 | +import Data.Monoid.Null |
| 11 | +import Data.Monoid.Textual |
| 12 | +import Data.Semigroup.Cancellative |
| 13 | +import Functora.Prelude |
| 14 | + |
| 15 | +instance MonoidNull JSString where |
| 16 | + null = JS.null |
| 17 | + |
| 18 | +instance Factorial JSString where |
| 19 | + factors = JS.chunksOf 1 |
| 20 | + primePrefix = JS.take 1 |
| 21 | + primeSuffix x = if JS.null x then JS.empty else JS.singleton (JS.last x) |
| 22 | + foldl f = JS.foldl f' |
| 23 | + where |
| 24 | + f' a char = f a (JS.singleton char) |
| 25 | + foldl' f = JS.foldl' f' |
| 26 | + where |
| 27 | + f' a char = f a (JS.singleton char) |
| 28 | + foldr f = JS.foldr f' |
| 29 | + where |
| 30 | + f' char a = f (JS.singleton char) a |
| 31 | + length = JS.length |
| 32 | + reverse = JS.reverse |
| 33 | + |
| 34 | +instance LeftGCDMonoid JSString where |
| 35 | + stripCommonPrefix x y = maybe (JS.empty, x, y) id (JS.commonPrefixes x y) |
| 36 | + |
| 37 | +instance LeftReductive JSString where |
| 38 | + stripPrefix = JS.stripPrefix |
| 39 | + isPrefixOf = JS.isPrefixOf |
| 40 | + |
| 41 | +instance TextualMonoid JSString where |
| 42 | + fromText = JST.textToJSString |
| 43 | + singleton = JS.singleton |
| 44 | + splitCharacterPrefix = JS.uncons |
| 45 | + characterPrefix t = if JS.null t then Nothing else Just (JS.head t) |
| 46 | + map = JS.map |
| 47 | + concatMap = JS.concatMap |
| 48 | + toString = const JS.unpack |
| 49 | + toText = const JST.textFromJSString |
| 50 | + any = JS.any |
| 51 | + all = JS.all |
| 52 | + |
| 53 | + foldl = const JS.foldl |
| 54 | + foldl' = const JS.foldl' |
| 55 | + foldr = const JS.foldr |
| 56 | + |
| 57 | + scanl = JS.scanl |
| 58 | + scanl1 = JS.scanl1 |
| 59 | + scanr = JS.scanr |
| 60 | + scanr1 = JS.scanr1 |
| 61 | + mapAccumL = JS.mapAccumL |
| 62 | + mapAccumR = JS.mapAccumR |
| 63 | + |
| 64 | + takeWhile _ = JS.takeWhile |
| 65 | + dropWhile _ = JS.dropWhile |
| 66 | + break _ = JS.break |
| 67 | + span _ = JS.span |
| 68 | + spanMaybe s0 _ft fc t = case JS.foldr g id t (0, s0) of |
| 69 | + (i, s') | (prefix, suffix) <- JS.splitAt i t -> (prefix, suffix, s') |
| 70 | + where |
| 71 | + g c cont (i, s) |
| 72 | + | Just s' <- fc s c = let i' = succ i :: Int in seq i' $ cont (i', s') |
| 73 | + | otherwise = (i, s) |
| 74 | + spanMaybe' s0 _ft fc t = case JS.foldr g id t (0, s0) of |
| 75 | + (i, s') | (prefix, suffix) <- JS.splitAt i t -> (prefix, suffix, s') |
| 76 | + where |
| 77 | + g c cont (i, s) |
| 78 | + | Just s' <- fc s c = |
| 79 | + let i' = succ i :: Int in seq i' $ seq s' $ cont (i', s') |
| 80 | + | otherwise = (i, s) |
| 81 | + split = JS.split |
| 82 | + find = JS.find |
| 83 | + |
| 84 | +instance FactorialMonoid JSString where |
| 85 | + splitPrimePrefix = fmap (first JS.singleton) . JS.uncons |
| 86 | + splitPrimeSuffix x = |
| 87 | + if JS.null x |
| 88 | + then Nothing |
| 89 | + else Just (JS.init x, JS.singleton (JS.last x)) |
| 90 | + inits = JS.inits |
| 91 | + tails = JS.tails |
| 92 | + span f = JS.span (f . JS.singleton) |
| 93 | + break f = JS.break (f . JS.singleton) |
| 94 | + dropWhile f = JS.dropWhile (f . JS.singleton) |
| 95 | + takeWhile f = JS.takeWhile (f . JS.singleton) |
| 96 | + spanMaybe s0 f t = case JS.foldr g id t (0, s0) of |
| 97 | + (i, s') | (prefix, suffix) <- JS.splitAt i t -> (prefix, suffix, s') |
| 98 | + where |
| 99 | + g c cont (i, s) |
| 100 | + | Just s' <- f s (JS.singleton c) = |
| 101 | + let i' = succ i :: Int in seq i' $ cont (i', s') |
| 102 | + | otherwise = (i, s) |
| 103 | + spanMaybe' s0 f t = case JS.foldr g id t (0, s0) of |
| 104 | + (i, s') | (prefix, suffix) <- JS.splitAt i t -> (prefix, suffix, s') |
| 105 | + where |
| 106 | + g c cont (i, s) |
| 107 | + | Just s' <- f s (JS.singleton c) = |
| 108 | + let i' = succ i :: Int in seq i' $ seq s' $ cont (i', s') |
| 109 | + | otherwise = (i, s) |
| 110 | + split f = JS.split f' |
| 111 | + where |
| 112 | + f' = f . JS.singleton |
| 113 | + splitAt = JS.splitAt |
| 114 | + drop = JS.drop |
| 115 | + take = JS.take |
0 commit comments