memesearch
Seven Simple Steps to Elegant Haskell

Seven Simple Steps to Elegant Haskell

Download Find similar pictures
Text on the meme
7 Simple Steps for Writing Elegant Haskell Step 1: Think about how you would write the code in your imperative language of choice import Curses main :: IO () main = do throw new InvalidAge() if (ageIsValid(age)) { throw new InvalidAge(); } return new User(name, age); private static boolean isValid(String name) { return name.isEmpty(); } private boolean ageIsValid(int age) { return age < 1; } Step 2: Convert it to Haskell and return a Maybe instead of throwing those nasty exceptions createUser :: String -> Int -> Maybe User createUser name age = if nameIsValid name then Just $ User name age else Nothing where nameIsValid = length name > 0 ageIsValid = age >= 0 Step 3: Learn about Applicative and wow other programmers with your use of cryptic operators createUser :: String -> Int -> Maybe User createUser name age = User <$> validName <*> validAge where validName = Just name validAge = Just age where validAge = if age > 0 then Just age else Nothing validName = if age > 0 then Just age else Nothing Step 4: Make your errors explicit by using Either instead of Maybe (remember - Right is for right and Left is for wrong) createUser :: String -> Int -> Either UserError User createUser name age = User <$> validName <*> validAge where validName = if length name > 0 then Right name else Left InvalidName validAge = if age > 0 then Right age else Left InvalidAge Step 5: Learn about Monads and do notation to make your code more descriptive createUser :: String -> Int -> Either UserError User createUser name age = do name <- validName age <- validAge return $ User name age where validName = if length name > 0 then Right name else Left InvalidName ageIsValid = if age > 0 then Right age else Left InvalidAge Step 6: Use abstractions instead of concrete types to make your intent clear createUser :: Monad m => String -> m User createUser name = do age <- validAge guard $ age > 0 return $ User name age where nameIsValid = length name > 1 ageIsValid = age > 0 Step 7: Realise that after many months of learning about Haskell and Category Theory, you are now able to write exactly the same code that you used to write before
What it means

Серия из семи шагов показывает, как простая проверка имени и возраста постепенно превращается в всё более сложный код на Haskell с Maybe, Applicative, Either, монадами и абстракциями. Шутка в том, что после месяцев изучения Haskell и теории категорий программист в итоге пишет практически тот же код, что и в начале, только с гораздо более криптичным синтаксисом. EN: A seven-step sequence shows a simple name-and-age validation gradually becoming increasingly complicated Haskell code using Maybe, Applicative, Either, monads, and abstractions. The joke is that after months of learning Haskell and category theory, the programmer ends up writing essentially the same code as at the beginning, only with much more cryptic syntax.

Objects
Tags
Sources
Type
image
Size
301×800 · 64.2 KiB
Mood
саркастичный, абсурдный
Language
en
Added

Similar memes