Advertisement
GoToBread

Math in Programming

Feb 4th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. I've seen so many posts telling people they don't need math in coding. Yet so few of the people saying this don't realize how much math they actually use in their daily coding.
  2.  
  3. Knowing how much is the key to writing programs that are composable and self-documenting, because you'll just know how they fit together from the mathematical laws they follow.
  4.  
  5. Anything you can smush together into a bigger version of the same thing obeys the laws of semigroups.
  6.  
  7. Any two things you can combine into another of the same thing (along with a do-nothing operation) obeys the laws of monoids. Anything made up of monoids is itself a monoid.
  8.  
  9. Classes, along with their fields and methods, are captured purely with comonads.
  10.  
  11. Lists of data are sets and can be manipulated with set operations, unless they contain two or more of the same value (and type), then they're multisets. If you can order them, they're posets (or "chains" if the list has total ordering).
  12.  
  13. Expression parsing requires a set of data and a set of operations on that data, collectively called an algebra.
  14.  
  15. Changing the value of a piece of data is done with a morphism. A data type is effectively a category, assuming it obeys commutative laws, and thus the value change is described as an endomorphism within that category.
  16.  
  17. Changing the value of anything inside a structure (without changing their type) is done with functors. Running a function on a value inside a structure requires an applicative functor, so does running a function stored inside that structure.
  18.  
  19. Looping structures can be captured with recursive functions (no, they're NOT always more inefficient and difficult to understand than loops). These include maps (list transformations), filters (operating on parts of lists), and folds (turning a list into a single value). Lists stand in for iterators and indexes.
  20.  
  21. Changing each value in a list is done with list comprehensions (from set theory). These can combine the uses of maps and filters.
  22.  
  23. Input and output of data to and from the user and the program can be done with monads, which are structures that take in data to be worked with and perform operations on them away from the rest of the code (obeying the laws of monads). This works because the values are preserved and the changes are passed around (monads are endofunctors).
  24.  
  25. Structures themselves can be built up with various morphisms and F-algebras/coalgebras.
  26.  
  27. Sequential statements can be performed in a pure way with monads as well. "Do" monads perform these operations in functional languages. Monads also handle error checks and null checks, replacing try/catch blocks.
  28.  
  29. Data encapsulation without classes is done with lexical closures involving returning higher-order functions from normal functions.
  30.  
  31. (to be continued?)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement