{"id":1278,"date":"2018-12-02T14:37:28","date_gmt":"2018-12-02T09:07:28","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=1278"},"modified":"2018-12-03T17:27:11","modified_gmt":"2018-12-03T11:57:11","slug":"book-review-functional-programming-in-c","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2018\/12\/02\/book-review-functional-programming-in-c\/","title":{"rendered":"Book Review: Functional Programming in C++"},"content":{"rendered":"<p><em><strong>Title: Functional Programming in C++: How to improve your C++ programs using functional techniques<\/strong><\/em><\/p>\n<p><em><strong>Author: Ivan Cukic<\/strong><\/em><\/p>\n<p><em><strong>Publisher: Manning Publications Co.<\/strong><\/em><\/p>\n<p><em><strong>Year: 2019<\/strong><\/em><\/p>\n<p>This book came out recently, about a week ago. I bought it immediately, and when I started to read it, I couldn\u2019t put it down! Very informative and well written.<\/p>\n<p>If you are new to C++, it is better to read the book after gaining some familiarity with C++ first. You might also benefit by going through other books on <em><strong>Functional Programming<\/strong><\/em> (<em><strong>FP<\/strong><\/em>) as supplementary reading material.<\/p>\n<p>Before I delve into the book, I want to highlight the fact that C++ is <strong>NOT<\/strong> a <em><strong>functional<\/strong><\/em> programming language, just as it is <strong>NOT<\/strong> an <em><strong>object-oriented<\/strong><\/em> programming language. It is a <em><strong>multi-paradigm<\/strong><\/em> programming language, and allows us to choose the appropriate mix of language features for solving a given problem. The exciting news is that more exotic features are expected in <em><strong>C++20<\/strong><\/em> (and beyond). So, as C++ programmers, we are truly privileged!<\/p>\n<p>Here is an overview of what the book covers.<\/p>\n<p>This book is a sincere attempt to lay a solid foundation for how C++ could be used as a functional language.<\/p>\n<p>The first two chapters provide the necessary introductory material for what comes later, by giving an overview of the principles of functional programming in general, and the features of C++ that enable this.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>The third chapter goes into the details of <em><strong>function objects<\/strong><\/em> in C++, including normal <em><strong>functions<\/strong><\/em>, <em><strong>classes<\/strong><\/em> that behave like function objects, <em><strong>std::function&lt;&gt;<\/strong><\/em>, <em><strong>lambdas<\/strong><\/em>, and even gives some examples from the <a href=\"https:\/\/theboostcpplibraries.com\/boost.phoenix\" target=\"_blank\" rel=\"noopener\"><em><strong>Boost.Phoenix<\/strong><\/em><\/a> library. For people familiar with C++, this chapter might appear somewhat redundant, but I feel the author has done the right thing by including it because it provides continuity and makes the book more widely accessible.<\/p>\n<p>In the fourth chapter, the author explains the notion of <em><strong>partial functions<\/strong><\/em> and how to implement them in C++ using <strong>std::bind&lt;&gt;<\/strong> and <em><strong>lambda<\/strong><\/em> functions. Then he describes what <em><strong>currying<\/strong><\/em> is and how curried functions can be implemented using nested <em><strong>lambda<\/strong><\/em> functions. Many people find the difference between <em><strong>partial functions<\/strong><\/em> and <em><strong>currying<\/strong><\/em> difficult to understand, but this chapter provides a good overview of the topics.<span class=\"Apple-converted-space\">\u00a0 <\/span>There is also a brief discussion on <em><strong>function composition<\/strong><\/em> and <em><strong>function lifting<\/strong><\/em>.<\/p>\n<p>The fifth chapter is about <em><strong>pure functions<\/strong><\/em> and how to write code that does <em><strong>not<\/strong><\/em> have side effects (not a trivial task in many cases). The sixth chapter is about <em><strong>lazy evaluation<\/strong><\/em> and <em><strong>memoization<\/strong><\/em>, common themes in functional programming.<\/p>\n<p>The seventh chapter discusses <em><strong>Ranges<\/strong><\/em>, a feature that is planned for <em><strong>C++20<\/strong><\/em>. The author explains the idea using the <a href=\"https:\/\/github.com\/ericniebler\/range-v3\" target=\"_blank\" rel=\"noopener\"><em><strong>range-v3<\/strong><\/em><\/a> library (somewhat similar to <a href=\"https:\/\/www.boost.org\/doc\/libs\/1_68_0\/libs\/range\/doc\/html\/index.html\" target=\"_blank\" rel=\"noopener\"><em><strong>Boost.Range<\/strong><\/em><\/a>). I merely skimmed through this chapter, deciding to wait for official support of the feature in the language.<\/p>\n<p>In the next chapter, there is a nice explanation of <em><strong>functional data structures<\/strong><\/em>. These are data structures that are <em><strong>immutable<\/strong><\/em> and hence support efficient copying. <em><strong>Bartosz Milewski<\/strong><\/em>, another experienced C++ developer, has implemented a few functional data structures in C++ and <a href=\"https:\/\/www.youtube.com\/watch?v=OsB09djvfl4\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a> is a video talk given by him.<\/p>\n<p>The next two chapters talk about <em><strong>Algebraic Data Types<\/strong><\/em> and <em><strong>Monads<\/strong><\/em>. <em><strong>s<\/strong><strong>td::optional&lt;&gt;<\/strong><\/em> is an example of <em><strong>Monad<\/strong><\/em>, as well as the forthcoming <em><strong>std::expected&lt;&gt;<\/strong><\/em>. If you would like an overview of <em><strong>Monads<\/strong><\/em>, I recommend this <a href=\"https:\/\/www.youtube.com\/watch?v=t1e8gqXLbsU\" target=\"_blank\" rel=\"noopener\"><em><strong>video tutorial<\/strong><\/em><\/a>. You might also want to watch this <em><strong><a href=\"https:\/\/www.youtube.com\/watch?v=JfMBLx7qE0I\" target=\"_blank\" rel=\"noopener\">video<\/a><\/strong><\/em> on <em><strong>std::expected&lt;&gt;<\/strong><\/em> planned for <em><strong>C++20.<\/strong><\/em><\/p>\n<p>Chapter 11 covers <em><strong>Template Metaprogramming<\/strong><\/em>. This is not directly concerned with functional programming, but the idea is useful in implementing <em><strong>FP<\/strong><\/em> abstractions. I liked the last section of this chapter, which touches upon <em><strong>DSL<\/strong><\/em>s.<\/p>\n<p>Chapter 12 details the complexity of building <em><strong>concurrent<\/strong><\/em> systems and gives FP-based solutions for the same. This is easily one of the best chapters in the book.<\/p>\n<p>The last chapter focusses on <em><strong>Testing<\/strong><\/em> and <em><strong>Debugging<\/strong><\/em>. In addition to showing how to write test cases, the author ably demonstrates how it is possible to even generate test cases automatically in certain cases!<\/p>\n<p><em><strong>Functional programming<\/strong><\/em> is gaining a lot of attention these days. Although one could use a pure functional language such as <a href=\"https:\/\/www.haskell.org\" target=\"_blank\" rel=\"noopener\"><em><strong>Haskell<\/strong><\/em><\/a>, being able to apply functional programming techniques in a mainstream language such as C++ has immense benefits. Doing this correctly in C++ might take a while for novice C++ developers (perhaps even for experienced ones!), but the effort is well worth it.<\/p>\n<p><em><strong>Ivan Cukic<\/strong><\/em>, a respected software developer and researcher, clearly demonstrates his knowledge and deep understanding of the subject through this book. Here is a brief <a href=\"https:\/\/www.youtube.com\/watch?v=NIwc5xORHew\" target=\"_blank\" rel=\"noopener\"><em><strong>video talk<\/strong><\/em><\/a> on the subject by him.<\/p>\n<p><em><strong>Buy the book. Read it. Read it again.<\/strong><\/em> If you are a C++ programmer, you will definitely become a better C++ programmer. In fact, you will become a better programmer, period!<\/p>\n<p>Have a great weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Title: Functional Programming in C++: How to improve your C++ programs using functional techniques Author: Ivan Cukic Publisher: Manning Publications Co. Year: 2019 This book came out recently, about a week ago. I bought it immediately, and when I started to read it, I couldn\u2019t put it down! Very informative and well written. If you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[94,49,17],"tags":[67,171],"class_list":["post-1278","post","type-post","status-publish","format-standard","hentry","category-book-review","category-c","category-programming","tag-c","tag-functional-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-kC","jetpack-related-posts":[{"id":2496,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/08\/07\/first-encounter-with-the-ring-programming-language\/","url_meta":{"origin":1278,"position":0},"title":"First Encounter with the Ring Programming Language","author":"admin","date":"August 7, 2021","format":false,"excerpt":"The Ring Programming Language, designed by Mahmoud Fayed,\u00a0 has been around since 2016. I came to know of it quite accidentally two weeks ago when I received a promotional email from Apress about their book \"Beginning Ring Programming\" by Mansour Ayouni, published in 2020. I immediately did a google search\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Ring Installation Directory","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/08\/directory-300x155.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2935,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/11\/07\/why-learn-c\/","url_meta":{"origin":1278,"position":1},"title":"Why Learn C++","author":"admin","date":"November 7, 2022","format":false,"excerpt":"This is the third and the last article in the series \u201cWhy Learn Language-XYZ?\u201d. I talked about Prolog and Lisp earlier. The present article is on C++. Once you have gained reasonable proficiency with Prolog and Lisp, you are ready to learn C++! In my view, C++ is a complex\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1339,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/12\/23\/book-review-real-time-c\/","url_meta":{"origin":1278,"position":2},"title":"Book Review: Real-Time C++","author":"admin","date":"December 23, 2018","format":false,"excerpt":"Title: Real-Time C++: Efficient Object-Oriented and Template Microcontroller Programming Author: Christopher Kormanyos Publisher: Springer-Verlag Year: 2018 (Third Edition) It was the title of this book which caught my attention a few weeks ago and I decided to invest in it. I am not in to real-time software development these days,\u2026","rel":"","context":"In &quot;Book Review&quot;","block_context":{"text":"Book Review","link":"https:\/\/www.rangakrish.com\/index.php\/category\/book-review\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2986,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/01\/11\/book-review-adventures-in-rule-based-programming-a-clips-tutorial\/","url_meta":{"origin":1278,"position":3},"title":"Book Review: Adventures in Rule-Based Programming &#8211; A CLIPS Tutorial","author":"admin","date":"January 11, 2023","format":false,"excerpt":"Title: Adventures in Rule-Based Programming - A CLIPS Tutorial Author: Gary Riley Publisher: Secret Society Software, LLC Year: 2022 In an earlier article, I had talked about the relevance of Rule-based systems today. In that article I had also listed a few Rule engines that are popular and widely used.\u2026","rel":"","context":"In &quot;Knowledge Representation&quot;","block_context":{"text":"Knowledge Representation","link":"https:\/\/www.rangakrish.com\/index.php\/category\/knowledge-representation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/01\/Cover-188x300.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2927,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/20\/why-learn-lisp\/","url_meta":{"origin":1278,"position":4},"title":"Why Learn Lisp?","author":"admin","date":"October 20, 2022","format":false,"excerpt":"In the last article, I had shared my views on why programmers should learn Prolog, preferably as the first language. What language should one learn next? I strongly pitch for Lisp, to be precise, \u201cCommon Lisp\u201d. Lisp happens to be the second oldest (1958) programming language, only after Fortran (1957)!\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2200,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/11\/08\/book-review-the-common-lisp-condition-system-beyond-exception-handling-with-control-flow-mechanisms\/","url_meta":{"origin":1278,"position":5},"title":"Book Review &#8211; The Common Lisp Condition System: Beyond Exception Handling with Control Flow Mechanisms.","author":"admin","date":"November 8, 2020","format":false,"excerpt":"Title: The Common Lisp Condition System: Beyond Exception Handling with Control Flow Mechanisms Author: Micha\u0142 \"phoe\" Herda Publisher: Apress Year: 2020 Unlike with other languages, it is rare to find new books being published on Lisp. The last book on Lisp, which I purchased was \u201cCommon Lisp Recipes\u201d\u00a0\u00a0by Edmund Weitz\u2026","rel":"","context":"In &quot;Book Review&quot;","block_context":{"text":"Book Review","link":"https:\/\/www.rangakrish.com\/index.php\/category\/book-review\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/11\/Cover-page-209x300.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=1278"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1278\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=1278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=1278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=1278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}