SlideShare a Scribd company logo
1 of 59
Download to read offline
ЛЕНИВЫЕ
ВЫЧИСЛЕНИЯ:
плюсы и минусы
(RuHaskell.Meetup 2015 Summer, fromGregorian 2015 06 21)
  Лучше ничем не заниматься,
чем заниматься ничем.
Тацит
QUI EST QUI
Haskell-программа —
совокупность выражений
/англ. expression/
12.3 - 1.09
Это выражение
"/home/dsh" </> "meetup2015"
И это
head [15, 2, 6]
И это
9
И даже это!
Запуск Haskell-программы —
начало вычисления её выражений
/англ. evaluation/
Вычислить выражение —
значит сократить его
/англ. reduce/
В основе сокращения —
применение функции
/англ. function application/
let sum = 2 + 3
Можно сократить
let sum = (+) 2 3
Сокращаем…
let sum = 5
Готово
Если сократить нельзя —
выражение в нормальной форме
/англ. normal form/
Если сократить можно —
выражение называют сокращаемым
/англ. reducible expression или redex/
ПРИНЦИПЫ ЛЕНИ
№ 1
Выражение не сокращается,
пока в этом нет необходимости
main :: IO ()
main =
let stupid = 2 `div` 0
in print stupid
Вполне ожидаемая ошибка
main :: IO ()
main =
let stupid = 2 `div` 0
in putStrLn "Just exit!"
Никакой ошибки
№ 2
Спускаемся настолько,
насколько это нужно
main :: IO ()
main =
let some = ( 2 `div` 0
, [2 * 3, 4 `div` 0]
)
(_, second) = some
[elem1, _] = second
in print elem1
Ленивый лифт
ПЛЮСЫ
№ 1
Рациональность
main :: IO ()
main = do
file <- readFile "/home/dsh/big.log"
putStrLn $ take 100 file
Читаем лишь то, что нужно
№ 2
Бесконечность
head :: [a] -> a
head (x:_) = x
Возвращаем первый элемент списка
main :: IO ()
main =
print $ head [1, 10, 100]
Причём как конечного списка…
main :: IO ()
main =
print $ head [1..]
… так и бесконечного
№ 3
DSL
selectBy :: Bool -> (a, a) -> a
selectBy True (f, _) = f
selectBy False (_, s) = s
Замена условной конструкции
import System.Exit
main :: IO ()
main = do
putStrLn "Input web prefix:"
prefix <- getLine
selectBy (prefix == "https")
( putStrLn "secure web :)"
, exitWith $ ExitFailure 1
)
И это честная замена!
МИНУСЫ
№ 1
Неожиданное поведение
import Data.String.Utils
main :: IO ()
main = do
file <- readFile path
writeFile path $ replace "," ";" file
where path = "/home/dsh/data"
Ну-ну…
openFile: resource busy (file is locked)
Лень мешает
import Data.String.Utils
import qualified System.IO.Strict as S
main :: IO ()
main = do
file <- S.readFile path
writeFile path $ replace "," ";" file
where path = "/home/dsh/data"
Строгость решает
Выход:
Не игнорируйте слова
strictly и lazily
в документации
№ 2
Space Leak
Не путать с Memory Leak!
Memory Leak —
низкоуровневая ошибка
управления памятью
Space Leak —
высокоуровневая ошибка
проектирования
main :: IO ()
main =
let stupid = 2 `div` 0
in putStrLn "Just exit!"
Деления не было
2 `div` 0
Невычисленное (отложенное) выражение
/англ. unevaluated expression или thunk/
Все отложенные выражения
живут в памяти
Thank you Cap!
Проблема в их количестве!
f :: Num b => [a] -> b -> b
f [] c = c
f (_:xs) c = f xs $ c + 1
Пустышка с большой проблемой
f [1,2,3] 0
Применяем…
f 1:[2,3] 0 + 1
Первый шаг…
f 1:2:[3] (0 + 1) + 1
Второй шаг…
f 1:2:3:[] ((0 + 1) + 1) + 1
Третий шаг…
f [] ((0 + 1) + 1) + 1
Последний шаг…
((0 + 1) + 1) + 1 = 3
Готово
Ленивый вариант
main :: IO ()
main =
let v = f [1..50000000] (0 :: Integer)
in print v
Время: 62 с
Память: 6.19 ГБ
f' :: Num b => [a] -> b -> b
f' [] c = c
f' (_:xs) c = f' xs $! c + 1
Пустышка без проблем
Строгий вариант
main :: IO ()
main =
let v = f' [1..50000000] (0 :: Integer)
in print v
Время: 4 с
Память: мизерная
Выход:
Если код может раздуться,
разбавьте лень строгостью
main :: IO ()
main =
print $ Laziness good bad
where good = [ "Рациональность"
, "Бесконечность"
, "DSL"
]
bad = [ "Неожиданное поведение"
, "Space Leak"
]
Благодарю за внимание!
Денис Шевченко
dshevchenko.biz

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Ленивые вычисления: плюсы и минусы. Денис Шевченко