The Functional Programming Triad of Folding, Scanning and Iteration - a first example in Scala and Haskell - Polyglot FP for Fun and Profit - with minor corrections and improvements
The document discusses implementing various functional programming concepts like folding, scanning, and iteration to solve problems involving converting between digit sequences and integers. It provides examples in Scala and Haskell of using fold left to implement a digits-to-integer function and the iterate function to implement an integer-to-digits function. It also discusses using techniques like pipes in Scala and the $ operator in Haskell to improve readability by ordering function applications in the sequence they will be executed.
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
This slide deck can work both as an aide mémoire (memory jogger), or as a first (not completely trivial) example of using left folds, left scans and iteration, to implement mathematical induction.
Errata: on almost half of the slides there is some minor typo or imperfection or in some cases a minor error and in one case, an omission. See a later version for corrections and some improvements.
(for best quality images, either download or view here: https://philipschwarz.dev/fpilluminated/?page_id=455)
Scala code for latest version: https://github.com/philipschwarz/fp-fold-scan-iterate-triad-a-first-example-scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Philip Schwarz
(download for perfect quality) - See how recursive functions and structural induction relate to recursive datatypes.
Follow along as the fold abstraction is introduced and explained.
Watch as folding is used to simplify the definition of recursive functions over recursive datatypes
Part 1 - through the work of Richard Bird and Graham Hutton.
This version corrects the following issues:
slide 7, 11 fib(0) is 0,rather than 1
slide 23: was supposed to be followed by 2-3 slides recapitulating definitions of factorial and fibonacci with and without foldr, plus translation to scala
slide 36: concat not invoked in concat example
slides 48 and 49: unwanted 'm' in definition of sum
throughout: a couple of typographical errors
throughout: several aesthetic imperfections (wrong font, wrong font colour)
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaPhilip Schwarz
See a program structure flowchart used to highlight how an FP program breaks down into a functional core and imperative shell
View a program structure flowchart for the Game of Life
See the code for Game of Life’s functional core and imperative shell, both in Haskell and in Scala.
Code:
https://github.com/philipschwarz/functional-core-imperative-shell-scala
https://github.com/philipschwarz/functional-core-imperative-shell-haskell
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Philip Schwarz
Quicksort - a whistle-stop tour of the algorithm in five languages and four paradigms.
Programming Paradigms: Functional, Logic, Imperative, Imperative Functional
Languages: Haskell, Scala, Java, Clojure, Prolog
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
Take the very first baby steps on the path to doing graphics in Haskell and Scala.
Learn about a simple yet educational recursive algorithm producing images that are pleasing to the eye.
Learn how functional programs deal with the side effects required to draw images.
See how libraries like Gloss and Doodle make drawing Sierpinski’s triangle a doddle.
Code for this slide deck:
https://github.com/philipschwarz/sierpinski-triangle-haskell-gloss
https://github.com/philipschwarz/sierpinski-triangle-scala-cats-io
https://github.com/philipschwarz/sierpinski-triangle-scala-awt-and-doodle
Errata:
1. the title 'Sierpinski Triangle' on the front slide could be improved by replacing it with 'Sierpinski's Triangle'.
2. a couple of typos on two slides
3. the triangles drawn using Doodle are not equilateral, as intended but isosceles.
(UPDATE 2021-06-15 I opened PR https://github.com/creativescala/doodle/pull/99 and as a result, an equilateral triangle has now been added to Doodle: https://github.com/creativescala/doodle/commit/30d20efebcc2016942e9cdbae85fefca5b95fa3c).
Here is a corrected version of the deck: https://www.slideshare.net/pjschwarz/sierpinski-triangle-polyglot-fp-for-fun-and-profit-haskell-and-scala-with-minor-corrections
The Functional Programming Triad of fold, scan and iteratePhilip Schwarz
In the functional programming paradigm, the programmer does not need to write any loops or use array indices. Instead, functional programers deal with iterative calculations by translating mathematical induction directly into code, so that they can reason about sequences as mathematical values. The implement mathematical induction by folding, scanning and iterating.
Ouch - a couple of occurrences of 'mathematical' are misspelled (missing h) :-(
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
This slide deck can work both as an aide mémoire (memory jogger), or as a first (not completely trivial) example of using left folds, left scans and iteration, to implement mathematical induction.
Errata: on almost half of the slides there is some minor typo or imperfection or in some cases a minor error and in one case, an omission. See a later version for corrections and some improvements.
(for best quality images, either download or view here: https://philipschwarz.dev/fpilluminated/?page_id=455)
Scala code for latest version: https://github.com/philipschwarz/fp-fold-scan-iterate-triad-a-first-example-scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Philip Schwarz
(download for perfect quality) - See how recursive functions and structural induction relate to recursive datatypes.
Follow along as the fold abstraction is introduced and explained.
Watch as folding is used to simplify the definition of recursive functions over recursive datatypes
Part 1 - through the work of Richard Bird and Graham Hutton.
This version corrects the following issues:
slide 7, 11 fib(0) is 0,rather than 1
slide 23: was supposed to be followed by 2-3 slides recapitulating definitions of factorial and fibonacci with and without foldr, plus translation to scala
slide 36: concat not invoked in concat example
slides 48 and 49: unwanted 'm' in definition of sum
throughout: a couple of typographical errors
throughout: several aesthetic imperfections (wrong font, wrong font colour)
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaPhilip Schwarz
See a program structure flowchart used to highlight how an FP program breaks down into a functional core and imperative shell
View a program structure flowchart for the Game of Life
See the code for Game of Life’s functional core and imperative shell, both in Haskell and in Scala.
Code:
https://github.com/philipschwarz/functional-core-imperative-shell-scala
https://github.com/philipschwarz/functional-core-imperative-shell-haskell
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Philip Schwarz
Quicksort - a whistle-stop tour of the algorithm in five languages and four paradigms.
Programming Paradigms: Functional, Logic, Imperative, Imperative Functional
Languages: Haskell, Scala, Java, Clojure, Prolog
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
Take the very first baby steps on the path to doing graphics in Haskell and Scala.
Learn about a simple yet educational recursive algorithm producing images that are pleasing to the eye.
Learn how functional programs deal with the side effects required to draw images.
See how libraries like Gloss and Doodle make drawing Sierpinski’s triangle a doddle.
Code for this slide deck:
https://github.com/philipschwarz/sierpinski-triangle-haskell-gloss
https://github.com/philipschwarz/sierpinski-triangle-scala-cats-io
https://github.com/philipschwarz/sierpinski-triangle-scala-awt-and-doodle
Errata:
1. the title 'Sierpinski Triangle' on the front slide could be improved by replacing it with 'Sierpinski's Triangle'.
2. a couple of typos on two slides
3. the triangles drawn using Doodle are not equilateral, as intended but isosceles.
(UPDATE 2021-06-15 I opened PR https://github.com/creativescala/doodle/pull/99 and as a result, an equilateral triangle has now been added to Doodle: https://github.com/creativescala/doodle/commit/30d20efebcc2016942e9cdbae85fefca5b95fa3c).
Here is a corrected version of the deck: https://www.slideshare.net/pjschwarz/sierpinski-triangle-polyglot-fp-for-fun-and-profit-haskell-and-scala-with-minor-corrections
The Functional Programming Triad of fold, scan and iteratePhilip Schwarz
In the functional programming paradigm, the programmer does not need to write any loops or use array indices. Instead, functional programers deal with iterative calculations by translating mathematical induction directly into code, so that they can reason about sequences as mathematical values. The implement mathematical induction by folding, scanning and iterating.
Ouch - a couple of occurrences of 'mathematical' are misspelled (missing h) :-(
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Philip Schwarz
(download for perfect quality) See aggregation functions defined inductively and implemented using recursion.
Learn how in many cases, tail-recursion and the accumulator trick can be used to avoid stack-overflow errors.
Watch as general aggregation is implemented and see duality theorems capturing the relationship between left folds and right folds.
Through the work of Sergei Winitzki and Richard Bird.
Download for flawless quality (slides viewed online look a bit grainy and out of focus). A monad is an implementation of one of the minimal sets of monadic combinators, satisfying the laws of associativity and identity - see how compositional responsibilities are distributed in each combinator set
Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3Philip Schwarz
(download for picture-perfect quality) Follow along as Trampolining is used to overcome Stack Overflow issues with the simple IO monad, deepening you understanding of the IO monad in the process. See Game of Life IO actions migrated to the Cats Effect IO monad, which is trampolined in its flatMap evaluation.
Errata:
slide 33: "impure factorial function" should be "initial factorial function"
slide 34: there is a pointless short vertical bar inside a speech bubble
slide 39: "it is till annoying" should be "it is still annoying"
slide 44: "values that we are interested" should be "values that we are interested in"
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Philip Schwarz
(download for better quality)
Introducing Assignment invalidates the Substitution Model of Evaluation and violates Referential Transparency
- as explained in SICP (the Wizard Book)
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Philip Schwarz
(download for picture-perfect quality) Follow along as the impure functions in the Game of Life are translated from Haskell into Scala, deepening you understanding of the IO monad in the process.
This is simply a copy of the original with an error corrected on slides 3 and 52, which were supposed to show Scala code and instead showed the Haskell equivalent! Plus removal of a few minor aesthetic imperfections.
Original: https://www.slideshare.net/pjschwarz/game-of-life-polyglot-fp-haskell-scala-unison-part-2
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...Philip Schwarz
Learn how to write FP code that displays a graphical representation of all the numerous N-Queens solutions for N=4,5,6,7,8 .
See how to neatly solve the problem by exploiting its self-similarity and using a divide and conquer approach.
Make light work of assembling multiple images into a whole, by exploiting Doodle’s facilities for combining images using a relative layout.
See relevant FP functions, like Foldable’s intercalate and intersperse, in action.
Code for part 3: https://github.com/philipschwarz/n-queens-combinatorial-problem-scala-part-3
Left and Right Folds- Comparison of a mathematical definition and a programm...Philip Schwarz
We compare typical definitions of the left and right fold functions, with their mathematical definitions in Sergei Winitzki’s upcoming book: The Science of Functional Programming.
Errata:
Slide 13: "The way 𝑓𝑜𝑙𝑑𝑙 does it is by associating to the right" - should, of course ,end in "to the left".
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
(download for perfect quality) - See how recursive functions and structural induction relate to recursive datatypes.
Follow along as the fold abstraction is introduced and explained.
Watch as folding is used to simplify the definition of recursive functions over recursive datatypes
Part 1 - through the work of Richard Bird and Graham Hutton.
Errata:
slide 7, 11 fib(0) is 0,rather than 1
slide 23: was supposed to be followed by 2-3 slides recapitulating definitions of factorial and fibonacci with and without foldr, plus translation to scala
slide 36: concat not invoked in concat example
slides 48 and 49: unwanted 'm' in definition of sum
throughout: a couple of typographical errors
throughout: several aesthetic imperfections (wrong font, wrong font colour)
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...Philip Schwarz
See how the guard function has migrated from MonadPlus to Alternative and learn something about the latter.
Learn how to write a Scala program that draws an N-Queens solution board using the Doodle graphics library.
See how to write the equivalent Haskell program using the Gloss graphics library.
Learn how to use Monoid and Foldable to compose images both in Haskell and in Scala.
Link to part 1: https://www.slideshare.net/pjschwarz/nqueens-combinatorial-problem-polyglot-fp-for-fun-and-profit-haskell-and-scala-part-1
Errata:
On slide 22, the last line of the showQueens function should of course be show(solution).draw(frame) rather than show(solution).draw
On slide 43, it would be better if the definitions of the beside, above and on Monoids were also shown.
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Philip Schwarz
(download for perfect quality) See aggregation functions defined inductively and implemented using recursion.
Learn how in many cases, tail-recursion and the accumulator trick can be used to avoid stack-overflow errors.
Watch as general aggregation is implemented and see duality theorems capturing the relationship between left folds and right folds.
Through the work of Sergei Winitzki and Richard Bird.
This version corrects the following issues:
slide 32: = reverse --> reverse =
Slide 33: 100_000 -> 1_000_000
It also adds slides 36, 37 and 38
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Philip Schwarz
Embark on an informative and fun journey through everything you need to know to understand how the Applicative instance for functions makes for a terse palindrome checker function definition in point-free style.
Slideshare hasn't imported my notes, so here's the link to the Google Presentation: https://goo.gl/Gl4Vhm
Haskell is a statically typed, non strict, pure functional programming language. It is often talked and blogged about, but rarely used commercially. This talk starts with a brief overview of the language, then explains how Haskell is evaluated and how it deals with non-determinism and side effects using only pure functions. The suitability of Haskell for real world data science is then discussed, along with some examples of its users, a small Haskell-powered visualization, and an overview of useful packages for data science. Finally, Accelerate is introduced, an embedded DSL for array computations on the GPU, and an ongoing attempt to use it as the basis for a deep learning package.
Download for flawless quality (slides viewed online look a bit grainy and out of focus).
Equivalence of nested flatMaps and chained flatMaps for Kleisli arrow composition.
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5Philip Schwarz
(download for best quality slides) Gain a deeper understanding of why right folds over very large and infinite lists are sometimes possible in Haskell.
See how lazy evaluation and function strictness affect left and right folds in Haskell.
Learn when an ordinary left fold results in a space leak and how to avoid it using a strict left fold.
Errata:
slide 15: "as sharing is required" should be "as sharing is not required"
slide 43: 𝑠𝑓𝑜𝑙𝑑𝑙 (⊕) 𝑎 should be 𝑠𝑓𝑜𝑙𝑑𝑙 (⊕) 𝑒
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
For functions that can be defined both as an instance of a right fold and as an instance of a left fold, one may be more efficient than the other.
Let's look at the example of a function 'decimal' that converts a list of digits into the corresponding decimal number.
Erratum: it has been pointed out that it is possible to define the zip function using a right fold (see slide 5).
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Philip Schwarz
(download for perfect quality) See aggregation functions defined inductively and implemented using recursion.
Learn how in many cases, tail-recursion and the accumulator trick can be used to avoid stack-overflow errors.
Watch as general aggregation is implemented and see duality theorems capturing the relationship between left folds and right folds.
Through the work of Sergei Winitzki and Richard Bird.
Download for flawless quality (slides viewed online look a bit grainy and out of focus). A monad is an implementation of one of the minimal sets of monadic combinators, satisfying the laws of associativity and identity - see how compositional responsibilities are distributed in each combinator set
Game of Life - Polyglot FP - Haskell - Scala - Unison - Part 3Philip Schwarz
(download for picture-perfect quality) Follow along as Trampolining is used to overcome Stack Overflow issues with the simple IO monad, deepening you understanding of the IO monad in the process. See Game of Life IO actions migrated to the Cats Effect IO monad, which is trampolined in its flatMap evaluation.
Errata:
slide 33: "impure factorial function" should be "initial factorial function"
slide 34: there is a pointless short vertical bar inside a speech bubble
slide 39: "it is till annoying" should be "it is still annoying"
slide 44: "values that we are interested" should be "values that we are interested in"
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Philip Schwarz
(download for better quality)
Introducing Assignment invalidates the Substitution Model of Evaluation and violates Referential Transparency
- as explained in SICP (the Wizard Book)
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Philip Schwarz
(download for picture-perfect quality) Follow along as the impure functions in the Game of Life are translated from Haskell into Scala, deepening you understanding of the IO monad in the process.
This is simply a copy of the original with an error corrected on slides 3 and 52, which were supposed to show Scala code and instead showed the Haskell equivalent! Plus removal of a few minor aesthetic imperfections.
Original: https://www.slideshare.net/pjschwarz/game-of-life-polyglot-fp-haskell-scala-unison-part-2
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...Philip Schwarz
Learn how to write FP code that displays a graphical representation of all the numerous N-Queens solutions for N=4,5,6,7,8 .
See how to neatly solve the problem by exploiting its self-similarity and using a divide and conquer approach.
Make light work of assembling multiple images into a whole, by exploiting Doodle’s facilities for combining images using a relative layout.
See relevant FP functions, like Foldable’s intercalate and intersperse, in action.
Code for part 3: https://github.com/philipschwarz/n-queens-combinatorial-problem-scala-part-3
Left and Right Folds- Comparison of a mathematical definition and a programm...Philip Schwarz
We compare typical definitions of the left and right fold functions, with their mathematical definitions in Sergei Winitzki’s upcoming book: The Science of Functional Programming.
Errata:
Slide 13: "The way 𝑓𝑜𝑙𝑑𝑙 does it is by associating to the right" - should, of course ,end in "to the left".
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
(download for perfect quality) - See how recursive functions and structural induction relate to recursive datatypes.
Follow along as the fold abstraction is introduced and explained.
Watch as folding is used to simplify the definition of recursive functions over recursive datatypes
Part 1 - through the work of Richard Bird and Graham Hutton.
Errata:
slide 7, 11 fib(0) is 0,rather than 1
slide 23: was supposed to be followed by 2-3 slides recapitulating definitions of factorial and fibonacci with and without foldr, plus translation to scala
slide 36: concat not invoked in concat example
slides 48 and 49: unwanted 'm' in definition of sum
throughout: a couple of typographical errors
throughout: several aesthetic imperfections (wrong font, wrong font colour)
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...Philip Schwarz
See how the guard function has migrated from MonadPlus to Alternative and learn something about the latter.
Learn how to write a Scala program that draws an N-Queens solution board using the Doodle graphics library.
See how to write the equivalent Haskell program using the Gloss graphics library.
Learn how to use Monoid and Foldable to compose images both in Haskell and in Scala.
Link to part 1: https://www.slideshare.net/pjschwarz/nqueens-combinatorial-problem-polyglot-fp-for-fun-and-profit-haskell-and-scala-part-1
Errata:
On slide 22, the last line of the showQueens function should of course be show(solution).draw(frame) rather than show(solution).draw
On slide 43, it would be better if the definitions of the beside, above and on Monoids were also shown.
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Philip Schwarz
(download for perfect quality) See aggregation functions defined inductively and implemented using recursion.
Learn how in many cases, tail-recursion and the accumulator trick can be used to avoid stack-overflow errors.
Watch as general aggregation is implemented and see duality theorems capturing the relationship between left folds and right folds.
Through the work of Sergei Winitzki and Richard Bird.
This version corrects the following issues:
slide 32: = reverse --> reverse =
Slide 33: 100_000 -> 1_000_000
It also adds slides 36, 37 and 38
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Philip Schwarz
Embark on an informative and fun journey through everything you need to know to understand how the Applicative instance for functions makes for a terse palindrome checker function definition in point-free style.
Slideshare hasn't imported my notes, so here's the link to the Google Presentation: https://goo.gl/Gl4Vhm
Haskell is a statically typed, non strict, pure functional programming language. It is often talked and blogged about, but rarely used commercially. This talk starts with a brief overview of the language, then explains how Haskell is evaluated and how it deals with non-determinism and side effects using only pure functions. The suitability of Haskell for real world data science is then discussed, along with some examples of its users, a small Haskell-powered visualization, and an overview of useful packages for data science. Finally, Accelerate is introduced, an embedded DSL for array computations on the GPU, and an ongoing attempt to use it as the basis for a deep learning package.
Download for flawless quality (slides viewed online look a bit grainy and out of focus).
Equivalence of nested flatMaps and chained flatMaps for Kleisli arrow composition.
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 5Philip Schwarz
(download for best quality slides) Gain a deeper understanding of why right folds over very large and infinite lists are sometimes possible in Haskell.
See how lazy evaluation and function strictness affect left and right folds in Haskell.
Learn when an ordinary left fold results in a space leak and how to avoid it using a strict left fold.
Errata:
slide 15: "as sharing is required" should be "as sharing is not required"
slide 43: 𝑠𝑓𝑜𝑙𝑑𝑙 (⊕) 𝑎 should be 𝑠𝑓𝑜𝑙𝑑𝑙 (⊕) 𝑒
Similar to The Functional Programming Triad of Folding, Scanning and Iteration - a first example in Scala and Haskell - Polyglot FP for Fun and Profit - with minor corrections and improvements
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
For functions that can be defined both as an instance of a right fold and as an instance of a left fold, one may be more efficient than the other.
Let's look at the example of a function 'decimal' that converts a list of digits into the corresponding decimal number.
Erratum: it has been pointed out that it is possible to define the zip function using a right fold (see slide 5).
Function Programming in Scala.
A lot of my examples here comes from the book
Functional programming in Scala By Paul Chiusano and Rúnar Bjarnason, It is a good book, buy it.
Watch video (in Hebrew): http://parleys.com/play/53f7a9cce4b06208c7b7ca1e
Type classes are a fundamental feature of Scala, which allows you to layer new functionality on top of existing types externally, i.e. without modifying or recompiling existing code. When combined with implicits, this is a truly remarkable tool that enables many of the advanced features offered by the Scala library ecosystem. In this talk we'll go back to basics: how type classes are defined and encoded, and cover several prominent use cases.
A talk given at the Underscore meetup on 19 August, 2014.
An overview of the Idris functional programming language. Idris has a number of interesting features such as dependent types, function totality checking and theorem proving.
First presentation of the Vancouver Haskell Meetup. Here we introduce the capabilities of Haskell and some of it's main characteristics as a language
Similar to The Functional Programming Triad of Folding, Scanning and Iteration - a first example in Scala and Haskell - Polyglot FP for Fun and Profit - with minor corrections and improvements (20)
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidPhilip Schwarz
The subject of this deck is the small Print[A] program in the following blog post by Noel Welsh: https://www.inner-product.com/posts/direct-style-effects/.
Keywords: "direct-style", "context function", "context functions", "algebraic effect", "algebraic effects", "scala", "effect system", "effect systems", "effect", "side effect", "composition", "fp", "functional programming"
Tagless Final Encoding - Algebras and Interpreters and also ProgramsPhilip Schwarz
Tagless Final Encoding - Algebras and Interpreters and also Programs - An introduction, through the work of Gabriel Volpe.
Slide deck home: http://fpilluminated.com/assets/tagless-final-encoding-algebras-interpreters-and-programs.html
A sighting of traverseFilter and foldMap in Practical FP in ScalaPhilip Schwarz
Slide deck home: http://fpilluminated.com/assets/sighting-of-scala-cats-traverseFilter-and-foldMap-in-practical-fp-in-scala.html.
Download PDF for perfect image quality.
A sighting of sequence function in Practical FP in ScalaPhilip Schwarz
Slide deck home: http://fpilluminated.com/assets/sighting-of-scala-cats-sequence-function-in-practical-fp-in-scala.html.
Download PDF for perfect image quality.
This talk was presented on Aug 3rd 2023 during the Scala in the City event a ITV in London https://www.meetup.com/scala-in-the-city/events/292844968/
Visit the following for a description, slideshow, all slides with transcript, pdf, github repo, and eventually a video recording: http://fpilluminated.com/assets/n-queens-combinatorial-puzzle-meets-cats.html
At the centre of this talk is the N-Queens combinatorial puzzle. The reason why this puzzle features in the Scala book and functional programming course by Martin Odersky (the language’s creator), is that such puzzles are a particularly suitable application area of 'for comprehensions'.
We’ll start by (re)acquainting ourselves with the puzzle, and seeing the role played in it by permutations. Next, we’ll see how, when wanting to visualise candidate puzzle solutions, Cats’ monoidal functions fold and foldMap are a great fit for combining images.
While we are all very familiar with the triad providing the bread, butter and jam of functional programming, i.e. map, filter and fold, not everyone knows about the corresponding functions in Cats’ monadic variant of the triad, i.e. mapM, filterM and foldM, which we are going to learn about next.
As is often the case in functional programming, the traverse function makes an appearance, and we shall grab the opportunity to point out the symmetry that exists in the interrelation of flatMap / foldMap / traverse and flatten / fold / sequence.
Armed with an understanding of foldM, we then look at how such a function can be used to implement an iterative algorithm for the N-Queens puzzle.
The talk ends by pointing out that the iterative algorithm is smarter than the recursive one, because it ‘remembers’ where it has already placed previous queens.
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Philip Schwarz
Kleisli composition, flatMap, join, map, unit. A study/memory aid, to help learn/recall their implementation/interrelation.
Version 2, updated for Scala 3
Nat, List and Option Monoids -from scratch -Combining and Folding -an examplePhilip Schwarz
Nat, List and Option Monoids, from scratch. Combining and Folding: an example.
This is a new version of the original which has some cosmetic changes and a new 7th slide which only differs from slide 6 in that it defines the fold function in terms of the foldRight function.
Code: https://github.com/philipschwarz/nat-list-and-option-monoids-from-scratch-combining-and-folding-an-example
Nat, List and Option Monoids -from scratch -Combining and Folding -an examplePhilip Schwarz
Nat, List and Option Monoids, from scratch. Combining and Folding: an example.
Code: https://github.com/philipschwarz/nat-list-and-option-monoids-from-scratch-combining-and-folding-an-example
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...Philip Schwarz
When I posted the deck for Part 1 to the Scala users forum, Odd Möller linked to a paper titled "The Genuine Sieve of Eratosthenes", which speaks of the Unfaithful Sieve.
Part 2 is based on that paper and on Richard Bird's faithful Haskell implementation of the Sieve, which we translate into Scala.
Scala code for Richard Bird's infinite primes Haskell program: https://github.com/philipschwarz/sieve-of-eratosthenes-part-2-scala
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Philip Schwarz
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Haskell, Scala and Java.
Inspired by the example in Scott Wlaschin’s F# book: Domain Modeling Made Functional.
Download for better results.
Java 19 Code: https://github.com/philipschwarz/fruit-salad-and-fruit-snack-ADT-example-java
Jordan Peterson - The pursuit of meaning and related ethical axiomsPhilip Schwarz
I have only recently become aware of the work of Jordan Peterson. Because I am finding it so interesting, I hope that the following small collection of excerpts from some of his writings and speeches might entice any fellow latecomers to find out more about his work. See below for my own summary of some of the subjects touched upon in these slides.
Download for best results.
Strategies for Successful Data Migration Tools.pptxvarshanayak241
Data migration is a complex but essential task for organizations aiming to modernize their IT infrastructure and leverage new technologies. By understanding common challenges and implementing these strategies, businesses can achieve a successful migration with minimal disruption. Data Migration Tool like Ask On Data play a pivotal role in this journey, offering features that streamline the process, ensure data integrity, and maintain security. With the right approach and tools, organizations can turn the challenge of data migration into an opportunity for growth and innovation.
Accelerate Enterprise Software Engineering with PlatformlessWSO2
Key takeaways:
Challenges of building platforms and the benefits of platformless.
Key principles of platformless, including API-first, cloud-native middleware, platform engineering, and developer experience.
How Choreo enables the platformless experience.
How key concepts like application architecture, domain-driven design, zero trust, and cell-based architecture are inherently a part of Choreo.
Demo of an end-to-end app built and deployed on Choreo.
Quarkus Hidden and Forbidden ExtensionsMax Andersen
Quarkus has a vast extension ecosystem and is known for its subsonic and subatomic feature set. Some of these features are not as well known, and some extensions are less talked about, but that does not make them less interesting - quite the opposite.
Come join this talk to see some tips and tricks for using Quarkus and some of the lesser known features, extensions and development techniques.
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...informapgpstrackings
Keep tabs on your field staff effortlessly with Informap Technology Centre LLC. Real-time tracking, task assignment, and smart features for efficient management. Request a live demo today!
For more details, visit us : https://informapuae.com/field-staff-tracking/
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Hivelance Technology
Cryptocurrency trading bots are computer programs designed to automate buying, selling, and managing cryptocurrency transactions. These bots utilize advanced algorithms and machine learning techniques to analyze market data, identify trading opportunities, and execute trades on behalf of their users. By automating the decision-making process, crypto trading bots can react to market changes faster than human traders
Hivelance, a leading provider of cryptocurrency trading bot development services, stands out as the premier choice for crypto traders and developers. Hivelance boasts a team of seasoned cryptocurrency experts and software engineers who deeply understand the crypto market and the latest trends in automated trading, Hivelance leverages the latest technologies and tools in the industry, including advanced AI and machine learning algorithms, to create highly efficient and adaptable crypto trading bots
A Comprehensive Look at Generative AI in Retail App Testing.pdfkalichargn70th171
Traditional software testing methods are being challenged in retail, where customer expectations and technological advancements continually shape the landscape. Enter generative AI—a transformative subset of artificial intelligence technologies poised to revolutionize software testing.
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?XfilesPro
Worried about document security while sharing them in Salesforce? Fret no more! Here are the top-notch security standards XfilesPro upholds to ensure strong security for your Salesforce documents while sharing with internal or external people.
To learn more, read the blog: https://www.xfilespro.com/how-does-xfilespro-make-document-sharing-secure-and-seamless-in-salesforce/
First Steps with Globus Compute Multi-User EndpointsGlobus
In this presentation we will share our experiences around getting started with the Globus Compute multi-user endpoint. Working with the Pharmacology group at the University of Auckland, we have previously written an application using Globus Compute that can offload computationally expensive steps in the researcher's workflows, which they wish to manage from their familiar Windows environments, onto the NeSI (New Zealand eScience Infrastructure) cluster. Some of the challenges we have encountered were that each researcher had to set up and manage their own single-user globus compute endpoint and that the workloads had varying resource requirements (CPUs, memory and wall time) between different runs. We hope that the multi-user endpoint will help to address these challenges and share an update on our progress here.
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus
As part of the DOE Integrated Research Infrastructure (IRI) program, NERSC at Lawrence Berkeley National Lab and ALCF at Argonne National Lab are working closely with General Atomics on accelerating the computing requirements of the DIII-D experiment. As part of the work the team is investigating ways to speedup the time to solution for many different parts of the DIII-D workflow including how they run jobs on HPC systems. One of these routes is looking at Globus Compute as a way to replace the current method for managing tasks and we describe a brief proof of concept showing how Globus Compute could help to schedule jobs and be a tool to connect compute at different facilities.
Listen to the keynote address and hear about the latest developments from Rachana Ananthakrishnan and Ian Foster who review the updates to the Globus Platform and Service, and the relevance of Globus to the scientific community as an automation platform to accelerate scientific discovery.
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisGlobus
JASMIN is the UK’s high-performance data analysis platform for environmental science, operated by STFC on behalf of the UK Natural Environment Research Council (NERC). In addition to its role in hosting the CEDA Archive (NERC’s long-term repository for climate, atmospheric science & Earth observation data in the UK), JASMIN provides a collaborative platform to a community of around 2,000 scientists in the UK and beyond, providing nearly 400 environmental science projects with working space, compute resources and tools to facilitate their work. High-performance data transfer into and out of JASMIN has always been a key feature, with many scientists bringing model outputs from supercomputers elsewhere in the UK, to analyse against observational or other model data in the CEDA Archive. A growing number of JASMIN users are now realising the benefits of using the Globus service to provide reliable and efficient data movement and other tasks in this and other contexts. Further use cases involve long-distance (intercontinental) transfers to and from JASMIN, and collecting results from a mobile atmospheric radar system, pushing data to JASMIN via a lightweight Globus deployment. We provide details of how Globus fits into our current infrastructure, our experience of the recent migration to GCSv5.4, and of our interest in developing use of the wider ecosystem of Globus services for the benefit of our user community.
Understanding Globus Data Transfers with NetSageGlobus
NetSage is an open privacy-aware network measurement, analysis, and visualization service designed to help end-users visualize and reason about large data transfers. NetSage traditionally has used a combination of passive measurements, including SNMP and flow data, as well as active measurements, mainly perfSONAR, to provide longitudinal network performance data visualization. It has been deployed by dozens of networks world wide, and is supported domestically by the Engagement and Performance Operations Center (EPOC), NSF #2328479. We have recently expanded the NetSage data sources to include logs for Globus data transfers, following the same privacy-preserving approach as for Flow data. Using the logs for the Texas Advanced Computing Center (TACC) as an example, this talk will walk through several different example use cases that NetSage can answer, including: Who is using Globus to share data with my institution, and what kind of performance are they able to achieve? How many transfers has Globus supported for us? Which sites are we sharing the most data with, and how is that changing over time? How is my site using Globus to move data internally, and what kind of performance do we see for those transfers? What percentage of data transfers at my institution used Globus, and how did the overall data transfer performance compare to the Globus users?
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTier1 app
Even though at surface level ‘java.lang.OutOfMemoryError’ appears as one single error; underlyingly there are 9 types of OutOfMemoryError. Each type of OutOfMemoryError has different causes, diagnosis approaches and solutions. This session equips you with the knowledge, tools, and techniques needed to troubleshoot and conquer OutOfMemoryError in all its forms, ensuring smoother, more efficient Java applications.
Globus Connect Server Deep Dive - GlobusWorld 2024Globus
We explore the Globus Connect Server (GCS) architecture and experiment with advanced configuration options and use cases. This content is targeted at system administrators who are familiar with GCS and currently operate—or are planning to operate—broader deployments at their institution.
Developing Distributed High-performance Computing Capabilities of an Open Sci...Globus
COVID-19 had an unprecedented impact on scientific collaboration. The pandemic and its broad response from the scientific community has forged new relationships among public health practitioners, mathematical modelers, and scientific computing specialists, while revealing critical gaps in exploiting advanced computing systems to support urgent decision making. Informed by our team’s work in applying high-performance computing in support of public health decision makers during the COVID-19 pandemic, we present how Globus technologies are enabling the development of an open science platform for robust epidemic analysis, with the goal of collaborative, secure, distributed, on-demand, and fast time-to-solution analyses to support public health.
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Anthony Dahanne
Les Buildpacks existent depuis plus de 10 ans ! D’abord, ils étaient utilisés pour détecter et construire une application avant de la déployer sur certains PaaS. Ensuite, nous avons pu créer des images Docker (OCI) avec leur dernière génération, les Cloud Native Buildpacks (CNCF en incubation). Sont-ils une bonne alternative au Dockerfile ? Que sont les buildpacks Paketo ? Quelles communautés les soutiennent et comment ?
Venez le découvrir lors de cette session ignite
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
The Functional Programming Triad of Folding, Scanning and Iteration - a first example in Scala and Haskell - Polyglot FP for Fun and Profit - with minor corrections and improvements
1. fold
λ
The Func%onal Programming Triad of Folding, Scanning and Itera%on
a first example in Scala and Haskell
Polyglot FP for Fun and Profit
@philip_schwarz
slides by
h"ps://www.slideshare.net/pjschwarz
Richard Bird
Sergei Winitzki
sergei-winitzki-11a6431
h"p://www.cs.ox.ac.uk/people/richard.bird/
2. This slide deck can work both as an aide mémoire (memory jogger), or as a first (not
completely trivial) example of using le/ folds, le/ scans and itera5on to implement
mathema5cal induc5on.
We first look at the implementaAon, using a le/ fold, of a digits-to-int funcAon
that converts a sequence of digits into a whole number.
Then we look at the implementaAon, using the iterate funcAon, of an int-to-
digits funcAon that converts an integer into a sequence of digits. In Scala this
funcAon is very readable because the signatures of funcAons provided by collec5ons
permit the piping of such funcAons with zero syntac5c overhead. In Haskell, there is
some syntac5c sugar that can be used to achieve the same readability, so we look at
how that works.
We then set ourselves a simple task involving digits-to-int and int-to-digits,
and write a funcAon whose logic can be simplified with the introducAon of a le/ scan.
Let’s begin, on the next slide, by looking at how Richard Bird describes the digits-
to-int funcAon, which he calls 𝑑𝑒𝑐𝑖𝑚𝑎𝑙.
@philip_schwarz
3. Suppose we want a function decimal that takes a list of digits and returns the corresponding decimal number;
thus
𝑑𝑒𝑐𝑖𝑚𝑎𝑙 [𝑥0, 𝑥1, … , 𝑥n] = ∑!"#
$
𝑥 𝑘10($&!)
It is assumed that the most significant digit comes first in the list. One way to compute decimal efficiently is by a
process of multiplying each digit by ten and adding in the following digit. For example
𝑑𝑒𝑐𝑖𝑚𝑎𝑙 𝑥0, 𝑥1, 𝑥2 = 10 × 10 × 10 × 0 + 𝑥0 + 𝑥1 + 𝑥2
This decomposition of a sum of powers is known as Horner’s rule.
Suppose we define ⊕ by 𝑛 ⊕ 𝑥 = 10 × 𝑛 + 𝑥. Then we can rephrase the above equation as
𝑑𝑒𝑐𝑖𝑚𝑎𝑙 𝑥0, 𝑥1, 𝑥2 = (0 ⊕ 𝑥0) ⊕ 𝑥1 ⊕ 𝑥2
This example motivates the introduction of a second fold operator called 𝑓𝑜𝑙𝑑𝑙 (pronounced ‘fold left’).
Informally:
𝑓𝑜𝑙𝑑𝑙 ⊕ 𝑒 𝑥0, 𝑥1, … , 𝑥𝑛 − 1 = … ((𝑒 ⊕ 𝑥0) ⊕ 𝑥1) … ⊕ 𝑥 𝑛 − 1
The parentheses group from the left, which is the reason for the name. The full definition of 𝑓𝑜𝑙𝑑𝑙 is
𝑓𝑜𝑙𝑑𝑙 ∷ 𝛽 → 𝛼 → 𝛽 → 𝛽 → 𝛼 → 𝛽
𝑓𝑜𝑙𝑑𝑙 𝑓 𝑒 = 𝑒
𝑓𝑜𝑙𝑑𝑙 𝑓 𝑒 𝑥: 𝑥𝑠 = 𝑓𝑜𝑙𝑑𝑙 𝑓 𝑓 𝑒 𝑥 𝑥𝑠
Richard Bird
4. On the next slide we look at how Sergei
Winitzki describes the digits-to-int
funcAon, and how he implements it in Scala.
5. Example 2.2.5.3 Implement the function digits-to-int using foldLeft.
…
The required computation can be written as the formula
𝑟 = @
!"#
$&(
𝑑 𝑘 ∗ 10$&(&!.
…
Solution The inductive definition of digitsToInt
• For an empty sequence of digits, Seq(), the result is 0. This is a convenient base case, even if we never call
digitsToInt on an empty sequence.
• If digitsToInt(xs) is already known for a sequence xs of digits, and we have a sequence xs :+ x with one
more digit x, then
digitsToInt(xs :+ x) = digitsToInt(xs) * 10 + x
is directly translated into code:
def digitsToInt(d: Seq[Int]): Int = d.foldLeft(0){ (n, x) => n * 10 + x }
Sergei Winitzki
sergei-winitzki-11a6431
6. (⊕) :: Int -> Int -> Int
(⊕) n d = 10 * n + d
digits_to_int :: [Int] -> Int
digits_to_int = foldl (⊕) 0
extension (n:Int)
def ⊕ (d:Int) = 10 * n + d
def digitsToInt(ds: Seq[Int]): Int =
ds.foldLeft(0)(_⊕_)
assert( digitsToInt(List(5,3,7,4)) == 5374)
assert( (150 ⊕ 7) == 1507 )
𝑓𝑜𝑙𝑑𝑙 + 0 1,2,3 = 6𝑓𝑜𝑙𝑑𝑙 ⊕ 𝑒 𝑥0, 𝑥1, … , 𝑥𝑛 = … ((𝑒 ⊕ 𝑥0) ⊕ 𝑥1) … ⊕ 𝑥 𝑛
𝑓𝑜𝑙𝑑𝑙 ∷ 𝛽 → 𝛼 → 𝛽 → 𝛽 → 𝛼 → 𝛽
𝑓𝑜𝑙𝑑𝑙 𝑓 𝑒 = 𝑒
𝑓𝑜𝑙𝑑𝑙 𝑓 𝑒 𝑥: 𝑥𝑠 = 𝑓𝑜𝑙𝑑𝑙 𝑓 𝑓 𝑒 𝑥 𝑥𝑠
𝑓𝑜𝑙𝑑𝑙 ⊕ 𝑒 𝑥0, 𝑥1, 𝑥2
= 𝑓𝑜𝑙𝑑𝑙 ⊕ 𝑒 ⊕ 𝑥0 𝑥1, 𝑥2
= 𝑓𝑜𝑙𝑑𝑙 ⊕ 𝑒 ⊕ 𝑥0 ⊕ 𝑥1 𝑥2
= 𝑓𝑜𝑙𝑑𝑙 ⊕ (( 𝑒 ⊕ 𝑥0 ⊕ 𝑥1) ⊕ 𝑥2) [ ]
= ((𝑒 ⊕ 𝑥0) ⊕ 𝑥1) ⊕ 𝑥2
𝑓𝑜𝑙𝑑𝑙 ⊕ 0 1,2,3
= 𝑓𝑜𝑙𝑑𝑙 ⊕ 0 ⊕ 1 2,3
= 𝑓𝑜𝑙𝑑𝑙 ⊕ 0 ⊕ 1 ⊕ 2 3
= 𝑓𝑜𝑙𝑑𝑙 ⊕ (( 0 ⊕ 1 ⊕ 2) ⊕ 3) [ ]
= ((0 ⊕ 1) ⊕ 2) ⊕ 3
Here is a quick refresher on fold left
So let’s implement the
digits-to-int function
in Haskell.
And now in Scala.
𝑑𝑒𝑐𝑖𝑚𝑎𝑙 [𝑥0, 𝑥1, … , 𝑥n] = @
!"#
$
𝑥 𝑘10($&!)
7. Now we turn to int-to-digits, a function that is the opposite of digits-to-int,
and one that can be implemented using the iterate function.
In the next two slides, we look at how Sergei Winitzki describes digits-to-int
(which he calls digitsOf) and how he implements it in Scala.
8. 2.3 Converting a single value into a sequence
An aggregation converts (“folds”) a sequence into a single value; the opposite operation (“unfolding”) converts a single value into
a sequence. An example of this task is to compute the sequence of decimal digits for a given integer:
def digitsOf(x: Int): Seq[Int] = ???
scala> digitsOf(2405)
res0: Seq[Int] = List(2, 4, 0, 5)
We cannot implement digitsOf using map, zip, or foldLeft, because these methods work only if we already have a sequence;
but the function digitsOf needs to create a new sequence.
…
To figure out the code for digitsOf, we first write this function as a mathematical formula. To compute the digits for, say, n =
2405, we need to divide n repeatedly by 10, getting a sequence nk of intermediate numbers (n0 = 2405, n1 = 240, ...) and the
corresponding sequence of last digits, nk mod 10 (in this example: 5, 0, ...). The sequence nk is defined using mathematical
induction:
• Base case: n0 = n, where n is the given initial integer.
• Inductive step: nk+1 =
nk
10 for k = 1, 2, ...
Here
nk
10 is the mathematical notation for the integer division by 10.
Let us tabulate the evaluation of the sequence nk for n = 2405:
The numbers nk will remain all zeros after k = 4. It is clear that the useful part of the sequence is before it becomes all zeros. In this
example, the sequence nk needs to be stopped at k = 4. The sequence of digits then becomes [5, 0, 4, 2], and we need to reverse it
to obtain [2, 4, 0, 5]. For reversing a sequence, the Scala library has the standard method reverse.
Sergei Winitzki
sergei-winitzki-11a6431
9. So, a complete implementation for digitsOf is:
def digitsOf(n: Int): Seq[Int] =
if (n == 0) Seq(0) else { // n == 0 is a special case.
Stream.iterate(n) { nk => nk / 10 }
.takeWhile { nk => nk != 0 }
.map { nk => nk % 10 }
.toList.reverse
}
We can shorten the code by using the syntax such as (_ % 10) instead of { nk => nk % 10 },
def digitsOf(n: Int): Seq[Int] =
if (n == 0) Seq(0) else { // n == 0 is a special case.
Stream.iterate(n) (_ / 10 )
.takeWhile ( _ != 0 )
.map ( _ % 10 )
.toList.reverse
}
Sergei Winitzki
sergei-winitzki-11a6431
The Scala library has a general stream-producing func8on Stream.iterate. This func8on has two arguments, the ini8al
value and a func8on that computes the next value from the previous one:
…
The type signature of the method Stream.iterate can be wri>en as
def iterate[A](init: A)(next: A => A): Stream[A]
and shows a close correspondence to a defini8on by mathema8cal induc8on. The base case is the first value, init, and
the induc8ve step is a func8on, next, that computes the next element from the previous one. It is a general way of
crea8ng sequences whose length is not determined in advance.
10. the prelude function iterate returns an infinite list:
𝑖𝑡𝑒𝑟𝑎𝑡𝑒 ∷ 𝑎 → 𝑎 → 𝑎 → 𝑎
𝑖𝑡𝑒𝑟𝑎𝑡𝑒 𝑓 𝑥 = 𝑥 ∶ 𝑖𝑡𝑒𝑟𝑎𝑡𝑒 𝑓 (𝑓 𝑥)
In particular, 𝑖𝑡𝑒𝑟𝑎𝑡𝑒 +1 1 is an infinite list of the positive integers, a value we can also
write as [1..].
Richard Bird
Here is how Richard Bird
describes the iterate funcAon
int_to_digits :: Int -> [Int]
int_to_digits n =
reverse (
map (x -> mod x 10) (
takeWhile (x -> x /= 0) (
iterate (x -> div x 10)
n
)
)
)
import LazyList.iterate
def intToDigits(n: Int): Seq[Int] =
iterate(n) (_ / 10 )
.takeWhile ( _ != 0 )
.map ( _ % 10 )
.toList
.reverse
Here on the left I had a go at a Haskell implementation of the int-to-digits function (we
are not handling cases like n=0 or negative n), and on the right, the same logic in Scala.
I find the Scala version slightly easier
to understand, because the funcAons
that we are calling appear in the order
in which they are executed. Contrast
that with the Haskell version, in which
the funcAon invocaAons occur in the
opposite order.
11. While the ‘fluent’ chaining of funcAon calls on Scala collecAons is very convenient, when using other funcAons, we
face the same problem that we saw in Haskell on the previous slide. e.g. in the following code, square appears first,
even though it is executed last, and vice versa for inc.
assert(square(twice(inc(3))) == 64)
assert ((3 pipe inc pipe twice pipe square) == 64)
To help a bit with that problem, in Scala
there is a pipe funcAon which is available
on all values, and which allows us to order
funcAon invocaAons in the ‘right’ order.
Armed with pipe, we can rewrite the code so that funcAon
names occur in the same order in which the funcAons are
invoked, which makes the code more understandable.
def inc(n: Int): Int = n + 1
def twice(n: Int): Int = n * 2
def square(n: Int): Int = n * n
3 pipe inc
pipe twice
pipe square
square(twice(inc(3))
@philip_schwarz
12. What about in Haskell? First of all, in Haskell there is a function application
operator called $, which we can sometimes use to omit parentheses
Application operator. This operator is redundant, since ordinary application (f x)
means the same as (f $ x).
However, $ has low, right-associative binding precedence, so it sometimes allows
parentheses to be omitted; for example:
f $ g $ h x = f (g (h x))
int_to_digits :: Int -> [Int]
int_to_digits n =
reverse (
map (x -> mod x 10) (
takeWhile (x -> x /= 0) (
iterate (x -> div x 10)
n
)
)
)
int_to_digits :: Int -> [Int]
int_to_digits n =
reverse $ map (x -> mod x 10)
$ takeWhile (x -> x > 0)
$ iterate (x -> div x 10)
$ n
Armed with $, we can simplify our function as follows
simplify
For beginners, the $ oVen makes Haskell code more difficult to
parse. In pracXce, the $ operator is used frequently, and you’ll
likely find you prefer using it over many parentheses. There’s
nothing magical about $; if you look at its type signature, you
can see how it works:
($) :: (a -> b) -> a -> b
The arguments are just a funcXon and a value. The trick is
that $ is a binary operator, so it has lower precedence than the
other funcXons you’re using. Therefore, the argument for the
funcXon will be evaluated as though it were in parentheses.
13. But there is more we can do. In addiAon to the
func5on applica5on operator $, in Haskell there
is also a reverse func5on applica5on operator &.
https://www.fpcomplete.com/haskell/tutorial/operators/
14. int_to_digits :: Int -> [Int]
int_to_digits n =
reverse $ map (x -> mod x 10)
$ takeWhile (x -> x > 0)
$ iterate (x -> div x 10)
$ n
int_to_digits :: Int -> [Int]
int_to_digits n =
n & iterate (x -> div x 10)
& takeWhile (x -> x > 0)
& map (x -> mod x 10)
& reverse
def intToDigits(n: Int): Seq[Int] =
iterate(n) (_ / 10 )
.takeWhile ( _ != 0 )
.map ( _ % 10 )
.toList
.reverse
Thanks to the & operator, we can rearrange our int_to_digits
function so that it is as readable as the Scala version.
15. There is one school of thought according to which the choice of names for $ and & make
Haskell hard to read for newcomers, that it is better if $ and & are instead named <| and |>.
Flow provides operators for writing more understandable Haskell. It is an alternative to some
common idioms like ($) for function application and (.) for function composition.
…
Rationale
I think that Haskell can be hard to read. It has two operators for applying functions. Both are not
really necessary and only serve to reduce parentheses. But they make code hard to read. People
who do not already know Haskell have no chance of guessing what foo $ bar or baz &
qux mean.
…
I think we can do better. By using directional operators, we can allow readers to move their eye
in only one direction, be that left-to-right or right-to-left. And by using idioms common in other
programming languages, we can allow people who aren't familiar with Haskell to guess at the
meaning.
So instead of ($), I propose (<|). It is a pipe, which anyone who has touched a Unix system
should be familiar with. And it points in the direction it sends arguments along. Similarly,
replace (&) with (|>). …
square $ twice $ inc $ 3
square <| twice <| inc <| 3
3 & inc & twice & square
3 |> inc |> twice |> square
Here is an example of how |> and
<| improve readability
16. Since & is just the reverse of $, we can define |> ourselves simply by flipping $
λ "left" ++ "right"
"leftright”
λ
λ (##) = flip (++)
λ
λ "left" ## "right"
"rightleft"
λ
λ inc n = n + 1
λ twice n = n * 2
λ square n = n * n
λ
λ square $ twice $ inc $ 3
64
λ
λ (|>) = flip ($)
λ
λ 3 |> inc |> twice |> square
64
λ
int_to_digits :: Int -> [Int]
int_to_digits n =
n |> iterate (x -> div x 10)
|> takeWhile (x -> x > 0)
|> map (x -> mod x 10)
|> reverse
And here is how our
function looks using |>.
@philip_schwarz
17. Now let’s set ourselves the following task. Given a posiAve integer N with n digits, e.g. the five-digit number
12345, we want to compute the following:
[(0,0),(1,1),(12,3),(123,6),(1234,10),(12345,15)]
i.e. we want to compute a list of pairs p0 , p1 , … , pn with pk being ( Nk , Nk 𝛴 ), where Nk is the integer number
formed by the first k digits of N, and Nk𝛴 is the sum of those digits. We can use our int_to_digits funcAon to
convert N into its digits d1 , d2 , … , dn :
λ int_to_digits 12345
[1,2,3,4,5]
λ
And we can use digits_to_int to turn digits d1 , d2 , … , dk into Nk , e.g. for k = 3 :
λ digits_to_int [1,2,3]
123
λ
How can we generate the following sequences of digits ?
[ [ ] , [d1 ] , [d1 , d2 ] , [d1 , d2 , d3 ] , … , [d1 , d2 , d3 , … , dn ] ]
As we’ll see on the next slide, that is exactly what the inits funcAon produces when passed [d1 , d2 , d3 , … , dn ] !
18. 𝑖𝑛𝑖𝑡𝑠 𝑥0, 𝑥1, 𝑥2 = [[ ], 𝑥0 , 𝑥0, 𝑥1 , 𝑥0, 𝑥1, 𝑥2 ]
𝑖𝑛𝑖𝑡𝑠 ∷ α → α
𝑖𝑛𝑖𝑡𝑠 = [ ]
𝑖𝑛𝑖𝑡𝑠 𝑥: 𝑥𝑠 = ∶ 𝑚𝑎𝑝 𝑥: (𝑖𝑛𝑖𝑡𝑠 𝑥𝑠)
Here is a definiAon for inits.
So we can apply inits to [d1 , d2 , d3 , … , dn ] to generate the following:
[ [ ] , [d1 ] , [d1 , d2 ] , [d1 , d2 , d3 ] , … , [d1 , d2 , d3 , … , dn ] ]
e.g.
λ inits [1,2,3,4]
[[],[1],[1,2],[1,2,3],[1,2,3,4]]]
λ
And here is what inits produces, i.e.
the list of all initial segments of a list.
19. So if we map digits_to_int over the initial segments of [d1 , d2 , d3 , … , dn ], i.e
[ [ ] , [d1 ] , [d1 , d2 ] , [d1 , d2 , d3 ] , … , [d1 , d2 , d3 , … , dn ] ]
we obtain a list containing N0 , N1 , … , Nn , e.g.
λ map digits_to_int (inits [1,2,3,4,5]))
[0,1,12,123,1234,12345]]
λ
What we need now is a function digits_to_sum which is similar to digits_to_int but which instead of converting a list of digits
[d1 , d2 , d3 , … , dk ] into Nk , i.e. the number formed by those digits, it turns the list into Nk𝛴, i.e. the sum of the digits. Like
digits_to_int, the digits_to_sum function can be defined using a left fold:
digits_to_sum :: [Int] -> Int
digits_to_sum = foldl (+) 0
Let’s try it out:
λ digits_to_sum [1,2,3,4])
10]
λ
Now if we map digits_to_sum over the initial segments of [d1 , d2 , d3 , … , dn ], we obtain a list containing N0 𝛴 , N1 𝛴 , … , Nn 𝛴 ,
e.g.
λ map digits_to_sum (inits [1,2,3,4,5]))
[0,1,3,6,10,15]]
λ
(⊕) :: Int -> Int -> Int
(⊕) n d = 10 * n + d
digits_to_int :: [Int] -> Int
digits_to_int = foldl (⊕) 0
20. digits_to_sum :: [Int] -> Int
digits_to_sum = foldl (+) 0
(⊕) :: Int -> Int -> Int
(⊕) n d = 10 * n + d
digits_to_int :: [Int] -> Int
digits_to_int = foldl (⊕) 0
convert :: Int -> [(Int,Int)]
convert n = zip nums sums
where nums = map digits_to_int segments
sums = map digits_to_sum segments
segments = inits (int_to_digits n)
int_to_digits :: Int -> [Int]
int_to_digits n =
n |> iterate (x -> div x 10)
|> takeWhile (x -> x > 0)
|> map (x -> mod x 10)
|> reverse
λ convert 12345
[(0,0),(1,1),(12,3),(123,6),(1234,10),(12345,15)]
λ
So here is how our complete
program looks at the moment.
@philip_schwarz
21. What we are going to do next is see how, by using the scan le/ funcAon, we are
able to simplify the definiAon of convert which we just saw on the previous slide.
As a quick refresher of (or introducAon to) the scan le/ funcAon, in the next two
slides we look at how Richard Bird describes the funcAon.
22. 4.5.2 Scan le/
SomeXmes it is convenient to apply a 𝑓𝑜𝑙𝑑𝑙 operaXon to every iniXal segment of a list. This is done by a funcXon 𝑠𝑐𝑎𝑛𝑙
pronounced ‘scan leB’. For example,
𝑠𝑐𝑎𝑛𝑙 ⊕ 𝑒 𝑥0, 𝑥1, 𝑥2 = [𝑒, 𝑒 ⊕ 𝑥0, (𝑒 ⊕ 𝑥0) ⊕ 𝑥1, ((𝑒 ⊕ 𝑥0) ⊕ 𝑥1) ⊕ 𝑥2]
In parXcular, 𝑠𝑐𝑎𝑛𝑙 + 0 computes the list of accumulated sums of a list of numbers, and 𝑠𝑐𝑎𝑛𝑙 × 1 [1. . 𝑛] computes a list of
the first 𝑛 factorial numbers. … We will give two programs for 𝑠𝑐𝑎𝑛𝑙; the first is the clearest, while the second is more efficient.
For the first program we will need the funcXon inits that returns the list of all iniDal segments of a list. For Example,
𝑖𝑛𝑖𝑡𝑠 𝑥0, 𝑥1, 𝑥2 = [[ ], 𝑥0 , 𝑥0, 𝑥1 ,
𝑥0, 𝑥1, 𝑥2 ]
The empty list has only one segment, namely the empty list itself; A list 𝑥: 𝑥𝑠 has the empty list as its shortest iniDal segment,
and all the other iniDal segments begin with 𝑥 and are followed by an iniDal segment of 𝑥𝑠. Hence
𝑖𝑛𝑖𝑡𝑠 ∷ α → α
𝑖𝑛𝑖𝑡𝑠 = [ ]
𝑖𝑛𝑖𝑡𝑠 𝑥: 𝑥𝑠 = ∶ 𝑚𝑎𝑝 (𝑥: )(𝑖𝑛𝑖𝑡𝑠 𝑥𝑠)
The funcXon 𝑖𝑛𝑖𝑡𝑠 can be defined more succinctly as an instance of 𝑓𝑜𝑙𝑑𝑟 :
𝑖𝑛𝑖𝑡𝑠 = 𝑓𝑜𝑙𝑑𝑟 𝑓 [ ] 𝑤ℎ𝑒𝑟𝑒 𝑓 𝑥 𝑥𝑠𝑠 = ∶ 𝑚𝑎𝑝 𝑥: 𝑥𝑠𝑠
Now we define
𝑠𝑐𝑎𝑛𝑙 ∷ 𝛽 → 𝛼 → 𝛽 → 𝛽 → 𝛼 → [𝛽]
𝑠𝑐𝑎𝑛𝑙 𝑓 𝑒 = 𝑚𝑎𝑝 𝑓𝑜𝑙𝑑𝑙 𝑓 𝑒 . 𝑖𝑛𝑖𝑡𝑠
This is the clearest definiXon of 𝑠𝑐𝑎𝑛𝑙 but it leads to an inefficient program. The funcXon 𝑓 is applied k Xmes in the evaluaXon of Richard Bird
23. 𝑓𝑜𝑙𝑑𝑙 𝑓 𝑒 on a list of length k and, since the initial segments of a list of length n are lists with lengths 0,1,…,n, the function 𝑓 is
applied about n2/2 times in total.
Let us now synthesise a more efficient program. The synthesis is by an induction argument on 𝑥𝑠 so we lay out the calculation in
the same way.
<…not shown…>
In summary, we have derived
𝑠𝑐𝑎𝑛𝑙 ∷ 𝛽 → 𝛼 → 𝛽 → 𝛽 → 𝛼 → [𝛽]
𝑠𝑐𝑎𝑛𝑙 𝑓 𝑒 = [𝑒]
𝑠𝑐𝑎𝑛𝑙 𝑓 𝑒 𝑥: 𝑥𝑠 = 𝑒 ∶ 𝑠𝑐𝑎𝑛𝑙 𝑓 𝑓 𝑒 𝑥 𝑥𝑠
This program is more efficient in that function 𝑓 is applied exactly n times on a list of length n.
Richard Bird
𝑠𝑐𝑎𝑛𝑙 ⊕ 𝑒 𝑥0, 𝑥1, 𝑥2
⬇
[𝑒, 𝑒 ⊕ 𝑥0, (𝑒 ⊕ 𝑥0) ⊕ 𝑥1, ((𝑒 ⊕ 𝑥0) ⊕ 𝑥1) ⊕ 𝑥2]
𝑓𝑜𝑙𝑑𝑙 ∷ 𝛽 → 𝛼 → 𝛽 → 𝛽 → 𝛼 → 𝛽
𝑓𝑜𝑙𝑑𝑙 𝑓 𝑒 = 𝑒
𝑓𝑜𝑙𝑑𝑙 𝑓 𝑒 𝑥: 𝑥𝑠 = 𝑓𝑜𝑙𝑑𝑙 𝑓 𝑓 𝑒 𝑥 𝑥𝑠
Note the similarities and differences between 𝑠𝑐𝑎𝑛𝑙 and 𝑓𝑜𝑙𝑑𝑙, e.g. the left hand sides of their equations are the
same, and their signatures are very similar, but 𝑠𝑐𝑎𝑛𝑙 returns [𝛽] rather than 𝛽 and while 𝑓𝑜𝑙𝑑𝑙 is tail recursive,
𝑠𝑐𝑎𝑛𝑙 isn’t.
𝑠𝑐𝑎𝑛𝑙 ∷ 𝛽 → 𝛼 → 𝛽 → 𝛽 → 𝛼 → [𝛽]
𝑠𝑐𝑎𝑛𝑙 𝑓 𝑒 = [𝑒]
𝑠𝑐𝑎𝑛𝑙 𝑓 𝑒 𝑥: 𝑥𝑠 = 𝑒 ∶ 𝑠𝑐𝑎𝑛𝑙 𝑓 𝑓 𝑒 𝑥 𝑥𝑠
𝑓𝑜𝑙𝑑𝑙 ⊕ 𝑒 𝑥0, 𝑥1, 𝑥2
⬇
((𝑒 ⊕ 𝑥0) ⊕ 𝑥1) ⊕ 𝑥2
24. On the next slide, a very simple example of
using scanl, and a reminder of how the result
of scanl relates to the result of inits and foldl.
26. 𝑠𝑐𝑎𝑛𝑙 𝑓 𝑒 = 𝑚𝑎𝑝 𝑓𝑜𝑙𝑑𝑙 𝑓 𝑒 . 𝑖𝑛𝑖𝑡𝑠
convert :: Int -> [(Int,Int)]
convert n = zip nums sums
where nums = map digits_to_int segments
sums = map digits_to_sum segments
segments = inits (int_to_digits n)
convert :: Int -> [(Int,Int)]
convert n = zip nums sums
where nums = map foldl (⊕) 0 segments
sums = map foldl (+) 0 segments
segments = inits (int_to_digits n)
AVer that refresher of (introducXon to) the scanl funcXon, let’s see how it can help us simplify our definiXon of the convert funcXon.
The first thing to do is to take the definiXon of convert and inline its invocaXons of digits_to_int and digits_to_sum:
refactor
Now let’s extract (int_to_digits n) into digits and inline segments.
refactor
convert :: Int -> [(Int,Int)]
convert n = zip nums sums
where nums = map foldl (⊕) 0 (inits digits)
sums = map foldl (+) 0 (inits digits)
digits = int_to_digits n
convert :: Int -> [(Int,Int)]
convert n = zip nums sums
where nums = map foldl (⊕) 0 segments
sums = map foldl (+) 0 segments
segments = inits (int_to_digits n)
convert :: Int -> [(Int,Int)]
convert n = zip nums sums
where nums = scanl (⊕) 0 digits
sums = scanl (+) 0 digits
digits = int_to_digits n
convert :: Int -> [(Int,Int)]
convert n = zip nums sums
where nums = map foldl (⊕) 0 (inits digits)
sums = map foldl (+) 0 (inits digits)
digits = int_to_digits n
refactor
As we saw earlier, mapping foldl over the result of applying inits to a list, is just applying
scanl to that list, so let’s simplify convert by calling scanl rather than mapping foldl.
27. convert :: Int -> [(Int,Int)]
convert n = zip nums sums
where nums = scanl (⊕) 0 digits
sums = scanl (+) 0 digits
digits = int_to_digits n
The suboptimal thing about our current definition of convert
is that it does two left scans over the same list of digits.
Can we refactor it to do a single scan? Yes, by using tupling, i.e. by changing the funcEon
that we pass to scanl from a funcEon like (⊕) or (+), which computes a single result, to
a funcEon, let’s call it next, which uses those two funcEons to compute two results
convert :: Int -> [(Int,Int)]
convert n = scanl next (0, 0) digits
where next (number, sum) digit = (number ⊕ digit, sum + digit)
digits = int_to_digits n
On the next slide, we inline digits and compare the resulting
convert function with our initial version, which invoked scanl twice.
@philip_schwarz
28. convert :: Int -> [(Int,Int)]
convert n = scanl next (0, 0) (int_to_digits n)
where next (number, sum) digit = (number ⊕ digit, sum + digit)
convert :: Int -> [(Int,Int)]
convert n = zip nums sums
where nums = map digits_to_int segments
sums = map digits_to_sum segments
segments = inits (int_to_digits n)
Here is our first definition of convert
And here is our refactored version, which uses a left scan.
The next slide shows the complete Haskell program,
and next to it, the equivalent Scala program.
29. digits_to_sum :: [Int] -> Int
digits_to_sum = foldl (+) 0
(⊕) :: Int -> Int -> Int
(⊕) n d = 10 * n + d
int_to_digits :: Int -> [Int]
int_to_digits n =
n |> iterate (x -> div x 10)
|> takeWhile (x -> x > 0)
|> map (x -> mod x 10)
|> reverse
λ convert 1234
[(0,0),(1,1),(12,3),(123,6),(1234,10)]
λ
convert' :: Int -> [(Int,Int)]
convert' n = scanl next (0, 0) (int_to_digits n)
where next (number, sum) digit =
(number ⊕ digit, sum + digit)
def digitsToInt(ds: Seq[Int]): Int =
ds.foldLeft(0)(_⊕_)
def digitsToSum(ds: Seq[Int]): Int =
ds.foldLeft(0)(_+_)
def intToDigits(n: Int): Seq[Int] =
iterate(n) (_ / 10 )
.takeWhile ( _ != 0 )
.map ( _ % 10 )
.toList
.reverse
def `convert⚡`(n: Int): Seq[(Int,Int)] =
val next: ((Int,Int),Int) => (Int,Int) =
case ((number, sum), digit) =>
(number ⊕ digit, sum + digit)
intToDigits(n).scanLeft((0,0))(next)
digits_to_int :: [Int] -> Int
digits_to_int = foldl (⊕) 0
fold
λ
convert :: Int -> [(Int,Int)]
convert n = zip nums sums
where nums = map digits_to_int segments
sums = map digits_to_sum segments
segments = inits (int_to_digits n)
def `convert⌛`(n: Int): Seq[(Int,Int)] =
val segments = intToDigits(n).inits.toList.reverse
val nums = segments map digitsToInt
val sums = segments map digitsToSum
nums zip sums
extension (n:Int)
def ⊕ (d:Int) = 10 * n + d
assert(intToDigits(1234) == List(1,2,3,4)); assert((123 ⊕ 4) == 1234)
assert(digitsToInt(List(1,2,3,4)) == 1234)
assert(digitsToSum(List(1,2,3,4)) == 10)
assert(`convert `(1234) == List((0,0),(1,1),(12,3),(123,6),(1234,10)))
assert(`convert⚡`(1234) == List((0,0),(1,1),(12,3),(123,6),(1234,10)))
30. In the next slide we conclude this deck with Sergei Winitzki‘s
recap of how in functional programming we implement
mathematical induction using folding, scanning and iteration.
31. Use arbitrary inducDve (i.e., recursive) formulas to:
• convert sequences to single values (aggregaDon or “folding”);
• create new sequences from single values (“unfolding”);
• transform exisXng sequences into new sequences.
Table 2.1: ImplemenXng mathemaDcal inducDon
Table 2.1 shows Scala code implemenDng those tasks. IteraDve calculaDons are implemented by translaDng mathemaDcal
inducDon directly into code. In the funcDonal programming paradigm, the programmer does not need to write any loops or use
array indices. Instead, the programmer reasons about sequences as mathemaDcal values: “StarDng from this value, we get that
sequence, then transform it into this other sequence,” etc. This is a powerful way of working with sequences, dicDonaries, and
sets. Many kinds of programming errors (such as an incorrect array index) are avoided from the outset, and the code is shorter
and easier to read than convenDonal code wriPen using loops.
Sergei Winitzki
sergei-winitzki-11a6431