haskell für java programmierer - MCS Systeme GmbH

Werbung
ROGER GILLIAR / MCS GMBH
HASKELL FÜR JAVA
PROGRAMMIERER
HASKELL FÜR JAVA PROGRAMMIERER
interface ImportantService {
Double getMoney();
}
HASKELL FÜR JAVA PROGRAMMIERER
... sollte stets ein so genannter Kontrakt
definiert werden, über den die Bedeutung
der verschiedenen Methoden festgelegt wird
– also deren Semantik."
Wikipedia / Schnittstelle
HASKELL FÜR JAVA PROGRAMMIERER
interface ImportantService {
Double getMoney();
}
HASKELL FÜR JAVA PROGRAMMIERER
...that determines the possible values for that
type; the operations that can be done …
Wikipedia / Data-Type
HASKELL FÜR JAVA PROGRAMMIERER
DATENTYPE DOUBLE
▸ byteValue
▸ compareTo
▸ toString
HASKELL FÜR JAVA PROGRAMMIERER
Account implements ImportantService {
Double getMoney() {
throw new IllegalStateException();
}
}
HASKELL FÜR JAVA PROGRAMMIERER
Account implements ImportantService {
Double getMoney() {
return null;
}
}
HASKELL FÜR JAVA PROGRAMMIERER
Account implements ImportantService {
Double getMoney() {
eraseHardDisk();
return new Double(42);
}
}
HASKELL FÜR JAVA PROGRAMMIERER
HASKELL FÜR JAVA PROGRAMMIERER
HASKELL FÜR JAVA PROGRAMMIERER
FUNKTIONALE PROGRAMMIERUNG?
▸ Keine Seiteneffekte
▸ Immutability
▸ Higher Order Functions
▸ Rekursion
HASKELL FÜR JAVA PROGRAMMIERER / FUNKTIONALE PROGRAMMIERUNG
PURE VS. IMPURE
Pure
Keine Seiteneffekte
Impure
Seiteneffekte
HASKELL FÜR JAVA PROGRAMMIERER / FUNKTIONALE PROGRAMMIERUNG
rollDice :: IO Int
rollDice = getStdRandom (randomR (1,6))
stopWordFileContent :: IO String
stopWordFileContent = readFile "stop.txt"
HASKELL FÜR JAVA PROGRAMMIERER
stop.txt
als
am
an
auch
auf
aus
bei
bin
…
goethe.txt
Hat der alte Hexenmeister
Sich doch einmal wegbegeben!
Und nun sollen seine Geister
Auch nach meinem Willen leben.
Seine Wort' und Werke
Merkt ich und den Brauch,
Und mit Geistesstärke
Tu' ich Wunder auch.
…
HASKELL FÜR JAVA PROGRAMMIERER
stopWordFileContent :: IO String
stopWordFileContent = readFile "stop.txt"
zauberLehrlingFileContent :: IO String
zauberLehrlingFileContent = readFile "goethe.txt"
main :: IO ()
main = do
stopWordsContent <- stopWordFileContent
zauberLehrlingContent <- zauberLehrlingFileContent
let stopWords = listify stopWordsContent
zauberLehrling = listify zauberLehrlingContent
parsedContent = removeStopWords $ parse zauberLehrling stopWords
wc = wordCount parsedContent
print $ take 15 $ sortBy sortDesc wc
return ()
HASKELL FÜR JAVA PROGRAMMIERER
listify :: String -> [T.Text]
listify c = filter ((> 1) . T.length) $ toList c
where
toList :: String -> [T.Text]
toList c = map (T.strip . T.pack) $ toListOfWords c
where
toListOfWords :: String -> [String]
toListOfWords c = concatMap words (lines c)
main :: IO ()
main = do
stopWordsContent <- stopWordFileContent
zauberLehrlingContent <- zauberLehrlingFileContent
let stopWords = listify stopWordsContent
zauberLehrling = listify zauberLehrlingContent
parsedContent = removeStopWords $ parse zauberLehrling stopWords
wc = wordCount parsedContent
print $ take 15 $ sortBy sortDesc wc
return ()
HASKELL FÜR JAVA PROGRAMMIERER
listify :: String -> [T.Text]
listify c = filter ((> 1) . T.length) $ toList c
where
toList :: String -> [T.Text]
toList c = map (T.strip . T.pack) $ toListOfWords c
where
toListOfWords :: String -> [String]
toListOfWords c = concatMap words $ lines c
toListOfWords "Oben sei ein Kopf!\nEile nun und gehe"
=> ["Oben", "sei", "ein" , "Kopf", "Eile", "nun", "und", "gehe"]
concatMap words $ lines c
=> concatMap words (lines c)
(T.strip . T.pack)
=> (\c -> T.strip (T.pack c))
HASKELL FÜR JAVA PROGRAMMIERER
["Oben",
"sei", "ein" ,
"Kopf",
"Eile", "nun",
"und", "gehe"]
[Word "oben", Word "sei", StopWord , Word "kopf", Word "eile", StopWord, StopWord, Word"gehe"]
HASKELL FÜR JAVA PROGRAMMIERER
data Token = Word T.Text
| StopWord deriving (Eq, Ord)
instance Show Token where
show StopWord = show "#"
show (Word w) = show ("{" ++ T.unpack w ++ "}")
Token
=> Type Constructor
Word
=> Data Constructor
StopWord
=> Data Constructor
Eq, Ord, Show => Type Classes
class Token implements Show, Eq, Ord {
void show (Token aToken) {…}
}
HASKELL FÜR JAVA PROGRAMMIERER
parse :: [T.Text] -> [T.Text] -> [Token]
parse xs stopWords = [ if x `elem` stopWords
then StopWord
else Word x
| x <- map normalize xs ]
main :: IO ()
main = do
stopWordsContent <- stopWordFileContent
zauberLehrlingContent <- zauberLehrlingFileContent
let stopWords = listify stopWordsContent
zauberLehrling = listify zauberLehrlingContent
parsedContent = removeStopWords $ parse zauberLehrling stopWords
wc = wordCount parsedContent
print $ take 15 $ sortBy sortDesc wc
return ()
parse ["Eile", "nun"] ["nun"] -> [Word "Eile", StopWord]
HASKELL FÜR JAVA PROGRAMMIERER
[Word "oben", Word "sei", StopWord , Word "kopf",
Word "eile", StopWord, StopWord, Word"gehe"]
[Word "oben", Word "sei", Word "kopf", Word "eile", Word"gehe"]
HASKELL FÜR JAVA PROGRAMMIERER
removeStopWords :: [Token] -> [Token]
removeStopWords c = filter (/= StopWord) c
main :: IO ()
main = do
stopWordsContent <- stopWordFileContent
zauberLehrlingContent <- zauberLehrlingFileContent
let stopWords = listify stopWordsContent
zauberLehrling = listify zauberLehrlingContent
parsedContent = removeStopWords $ parse zauberLehrling stopWords
wc = wordCount parsedContent
print $ take 15 $ sortBy sortDesc wc
return ()
filter (/= StopWord) c
=> filter (\e -> e /= StopWord) c
HASKELL FÜR JAVA PROGRAMMIERER
[Word "oben", Word "sei", Word "kopf", Word "eile", Word"gehe"]
[(1, Word "oben"), (1, Word "sei"), (1, Word "kopf", (1, Word "eile"), (1, Word"gehe")]
HASKELL FÜR JAVA PROGRAMMIERER
wordCount :: [Token] -> [(Int, Token)]
wordCount c = let
groupAndSort = group . sort
in
map (length &&& head) $ groupAndSort c
main :: IO ()
main = do
stopWordsContent <- stopWordFileContent
zauberLehrlingContent <- zauberLehrlingFileContent
let stopWords = listify stopWordsContent
zauberLehrling = listify zauberLehrlingContent
parsedContent = removeStopWords $ parse zauberLehrling stopWords
wc = wordCount parsedContent
print $ take 15 $ sortBy sortDesc wc
return ()
wordCount ["a", "b", "a"] -> sort -> ["a", "a", "b"] -> group -> [["a", "a"], ["b"]] -> length &&& head -> [(2, "a"),(1,"b")]
HASKELL FÜR JAVA PROGRAMMIERER
[(3, Word "oben"), (1, Word "sei"), (2, Word "kopf", (2, Word "eile"), (4, Word"gehe")]
[(4, Word "gehe"), (3, Word "oben"), (2, Word "eile", (2, Word "kopf"), (1, Word"sei")]
HASKELL FÜR JAVA PROGRAMMIERER
sortDesc :: (Ord a , Ord b) => (a, b) -> (a, b) -> Ordering
sortDesc (a1,b1) (a2,b2) =
case compare a1 a2 of
EQ -> compare b1 b2
LT -> GT
GT -> LT
main :: IO ()
main = do
stopWordsContent <- stopWordFileContent
zauberLehrlingContent <- zauberLehrlingFileContent
let stopWords = listify stopWordsContent
zauberLehrling = listify zauberLehrlingContent
parsedContent = removeStopWords $ parse zauberLehrling stopWords
wc = wordCount parsedContent
print $ take 15 $ sortBy sortDesc wc
return ()
Interface Comparable<T>.compareTo
HASKELL FÜR JAVA PROGRAMMIERER
[(4, Word "gehe"), (3, Word "oben"), (2, Word "eile", (2, Word "kopf"), (1, Word"sei")]
HASKELL FÜR JAVA PROGRAMMIERER
Daten einlesen
Daten transformieren (strip)
Daten filtern (length > 1)
Daten transformieren (Word, StopWord)
Daten filtern (not StopWord)
sortieren
gruppieren
sortieren
HASKELL FÜR JAVA PROGRAMMIERER
module Main where
import
import
import
import qualified
Control.Arrow
Data.List
Data.List.Split
Data.Text
((&&&))
(group, sort, sortBy)
(splitOn)
as T
data Token = Word T.Text
| StopWord
deriving (Eq, Ord)
instance Show Token where
show StopWord = show "#"
show (Word w) = show ("{" ++ T.unpack w ++ "}")
stopWordFileContent :: IO String
stopWordFileContent = readFile "stop.txt"
zauberLehrlingFileContent :: IO String
zauberLehrlingFileContent = readFile "goethe.txt"
listify :: String -> [T.Text]
listify c = filter ((> 1) . T.length) $ toList c
where
toList :: String -> [T.Text]
toList c = map (T.strip . T.pack) $ toListOfWords c
where
toListOfWords :: String -> [String]
toListOfWords c = concatMap words (lines c)
normalize :: T.Text -> T.Text
normalize w
| T.last lc `elem` "!?.,;" = T.dropEnd 1 lc
| otherwise = lc
where
lc = T.toLower w
parse :: [T.Text] -> [T.Text] -> [Token]
parse xs stopWords = [ if x `elem` stopWords
then StopWord
else Word x
| x <- map normalize xs ]
removeStopWords :: [Token] -> [Token]
removeStopWords c = filter (/= StopWord) c
wordCount :: [Token] -> [(Int, Token)]
wordCount c =
let groupAndSort = group . sort
in map (length &&& head) $ groupAndSort c
sortDesc :: (Ord a, Ord b) => (a, b) -> (a, b) -> Ordering
sortDesc (a1, b1) (a2, b2) =
case compare a1 a2 of
EQ -> compare b1 b2
LT -> GT
GT -> LT
main :: IO ()
main = do
stopWordsContent <- stopWordFileContent
zauberLehrlingContent <- zauberLehrlingFileContent
let stopWords = listify stopWordsContent
zauberLehrling = listify zauberLehrlingContent
parsedContent = removeStopWords $ parse zauberLehrling stopWords
wc = wordCount parsedContent
print $ take 15 $ sortBy sortDesc wc
return ()
HASKELL FÜR JAVA PROGRAMMIERER
HASKELL FÜR JAVA PROGRAMMIERER
HASKELL STACK
MEGA-TUTORIAL CHRISTOPHER ALLEN
HASKELL FÜR JAVA PROGRAMMIERER
MONADS
GREG MEREDITH
MONADIC DESIGN PATTERNS
HASKELL FÜR JAVA PROGRAMMIERER
FRAGEN?
[email protected]
Herunterladen