Created
April 28, 2014 16:16
-
-
Save joneshf/11376722 to your computer and use it in GitHub Desktop.
Motivation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Motivation.Semigroup where | |
import Data.Monoid.All | |
import Data.String | |
instance semigroupFunction :: (Semigroup b) => Semigroup (a -> b) where | |
(<>) f g = \x -> f x <> g x | |
-- String examples. | |
noun = "strings" | |
verb = "form" | |
article = "a" | |
noun' = "`Semigroup`" | |
space = " " | |
subject = noun | |
predicate = verb <> space <> article <> space <> noun' | |
sentence :: String | |
sentence = subject <> space <> predicate | |
sentence' :: String | |
sentence' = noun | |
<> space | |
<> verb | |
<> space | |
<> article | |
<> space | |
<> noun' | |
bothCase :: String -> String | |
bothCase s = toUpper s <> toLower s | |
-- Parser examples. | |
startsWith :: String -> String -> All | |
startsWith s s' = All $ substr 0 (lengthS s') s == s' | |
endsWith :: String -> String -> All | |
endsWith s s' = All $ substr (lengthS s - lengthS s') (lengthS s') s == s' | |
lParen :: String -> All | |
lParen s = s `startsWith` "(" | |
rParen :: String -> All | |
rParen s = s `endsWith` ")" | |
parens :: String -> All | |
parens = lParen <> rParen | |
startStop :: String -> String -> String -> All | |
startStop start stop = let startTest s = s `startsWith` start | |
stopTest s = s `endsWith` stop | |
in startTest <> stopTest | |
parens' :: String -> All | |
parens' = startStop "(" ")" | |
brackets :: String -> All | |
brackets = startStop "[" "]" | |
braces :: String -> All | |
braces = startStop "{" "}" | |
other :: String -> All | |
other = startStop "wat" "yup" | |
startMidStop :: String -> (String -> All) -> String -> String -> All | |
startMidStop start middleTest stop = let startTest s = s `startsWith` start | |
stopTest s = s `endsWith` stop | |
in startTest <> middleTest <> stopTest | |
contains :: String -> String -> All | |
contains s s' = All $ s `indexOfS` s' /= -1 | |
thenElse :: String -> All | |
thenElse s = let afterThen = s `indexOfS` "then" | |
s' = substring afterThen (lengthS s) s | |
in s `contains` "then" <> s' `contains` "else" | |
rubyIfThenElse :: String -> All | |
rubyIfThenElse = startMidStop "if" thenElse "end" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment