Skip to content

Instantly share code, notes, and snippets.

@sranso
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sranso/4fba22f52ec4706843c6 to your computer and use it in GitHub Desktop.
Save sranso/4fba22f52ec4706843c6 to your computer and use it in GitHub Desktop.

You say Functional, I say Object Oriented, we all say Javascript

Sarah Ransohoff

notes

you say functional, i say object oriented programming

let's get back to basics

  • apples and apples
  • potatoes and potatoes
  • i feel like when you're learning how to code, there is a focus on certain things
    • tdd
    • dry
    • oo vs f ... which often are points of interest in work, but not always. it's easy to forget what these things are, why we learned them in the first place, how they relate to what we do day-to-day. so i want to take a moment to step back and cover what these thigns are.. and for me, figure out what they actually are (idk which of these i learned or what the difference is, tbch...)

what is programming

  • process done by people to instruct a computer on how to do a task

what is programming paradigm

  • fundamental style of comp programming
  • a program is sequence of stateless functional evaluations
  • a way of building the structure and elements of comp pgming
  • many programming paradigms are as well known for what techniques they forbid as for what they enable
    • e.g. functional disallows use of side-effects
    • structured programming disallows the use of the goto statement
  • programming paradigms that are often distinguished include
    • imperative
    • declarative
      • comp is told only what the problem is, not how to actually solve it
    • functional
    • oo
    • procedural
    • logic
    • symbolic --> w/ diff paradigms, programs can be seen and built in diff ways
  • diff languages are defined by diff paradigms -- some designed to support one particular p
    • Smalltalk -> oop
    • Haskell -> functional ... while others support multiple
    • Object Pascal, C++ -> can be purely procedural, purely oo, or elements of both or others
  • seems like the evolution of programming paradigms looks something like this 1 machine code
    • lowest level; directly represents the instructions as seq of #s (0010110110) 2 procedural languages
    • describe step by step exactly the procedure that should, according to the particular programmer, be followed to solve a specific problem
    • use vocab related to the problem being solved (e.g. file, move, copy)
    • ex
      • COBOL
      • BASIC
      • C 3 oop
    • data and methods of manipulating the data are kept as a single unit called an obj
    • only way a user can access the data is via the object's methods (subroutines)
    • oop considered a paradigm not a language
    • ex
      • Simula
      • C++
      • Java 4 further paradigms
    • literate programming, a form of imperative p, structures programs as a human-centered web, as in a hypertext essay; readibility is of more importance
    • declarative programming (diff than ^), the comp is told what the prob is not how to solve it
      • e.g. react render function: give state and returns new dom
      • e.g. decl vs imperative
        • jquery if want to change pg bkgrnd color, query dom for el, css change for bkgrnd color (imperative) VS decl if state is this, go change the bkgrnd color
    • functional p, subset of declarative
      • programs written using this paradaigm use functions -- blocks of code intended to behave like mathetmatical functions. discourage changes in the val of variables thru assignment; make a great deal of use of recursion instead
    • logic programming
    • symbolic programming
    • multi-paradigm

what is funcitonal programming

  • the absence of side effects
    • doesn't rely on data outside the current fn
    • doesn't change data that exists outside the current fn
  • rather than iterating use map or reduce
    • why???
      • doesn't have side effects(?) -- no var names, no changing the current fn, no unnecessary or unused mem
      • often one-liners
      • iteration - the collection, operation and return val - are always in the same places in every map n red
      • code in a loop may affect vars defined before it or code that runs after it. by convention, maps and reduces are functional
      • map and reduce are elemental operations. every time a person reads a for loop, they have to work thru the logic line by line. in contrast, map and reduce are at once building blocks that can be combined into complex algorithms, and elements that the code reader can instantly understand and abstract int heir mind
      • map and red have many friends that provide useful, tweaked versions of their basic behavior (e.g. filter all any find)
  • functional code is declarative
    • describes what to do (decl) rather than how to do it (imperative)
    • e.g. to understand the program, the reader just needs to read the loop/what come last vs imp have to read the whole thing -- shows you step by step what's going on
  • exists v well w other code written in other styles
  • functions are first class objects that eliminate side effects
  • based on mathemtatical notion of fn composition
  • write functions that take n args and returns a val
  • looking for simple, repeatable actions that can be abstracted out into a function
    • then we can build more complex features by calling these functions in a sequence, aka "composing" functions gif? --> couldn't really find a good, simple definition... seems like there are 2 imp things to know
  1. data in functional programs should be immutable
  • aka never change gif
  • rather than changing the original object, create and return a new data structure
  1. functional programs should be stateless
  • aka should perform every task as if for the first time gif touched for the very first time/it's like the first time
  • each function is operating in a vacuum --> makes me think of react... not really functional. some ref to state, tho we try not to other imp things
  1. functions should (must?) accept at least one arg
  2. functions should (must?) return data or another func
  3. no loops
  • treats functions as primary abstraction
  • focus is on writing reusable, composable fns, w particulars of the underlying data structures as equal or secondary
  • a "pure" fn style treats the fn more like a mathematical fn, avoiding mutable state and the side effects that come w/ imperative code

why use it

  • allows programs to be broken down into smaller, simpler pieces --> makes programs more reliable, easier to understand
  • allows coding w/ fewer potentials for bugs b/c each component is completely isolated
  • using recursion and first-class functions allows for simple proofs of correctness which typically mirror the structure of the code

functional languages

  • note: you can write functional code in any lang that supports first-class functions what makes these guys diff is that they enforce functional programming -- eg haskell does not allow you to change state. in addition, you're not supposed to make any side effects (e.g. print out text) at all. eg fn.js force avoidance of side effects, object mutation, and function state.
  • clojure
  • scala
  • haskell
  • python (incorporates features of functional programming)
  • libs like underscore.js and bacon.js allow js to do fn prgming

what is object oriented programming

  • everything in js inherits from on obj aka is an obj
  • a program is a collection of objects interacting in explicitly defined ways
  • focuses on defining thigs (objs) and the actions you can do to them (methods)
    • treats objs and primary abstraction -- think in terms of nouns w verbs that you can do to them
    • objs tend to hold mutable state
    • the fns that operate on this state are usually imperative in style

which of these two things do i do

  • react?

what other options are there

  • reactive programming
    • oriented around data flows and the propagation of change --> react..?

am i doing it correctly

does it matter

  • the act of combining parts or elements to form a whole
  • in programming, it's about making more complex programs out of simpler programs w/o modifying the simpler pieces being composed
  • objects
    • unit of composition: an object (or class)
    • do they compose? shmeh.. i know when i see it. not super well overall.
      • objs that represent data structures w/ little behavior usually do
      • the real composability and reusability in object-oriented code doesn’t come from object-oriented design at all: it comes from abstraction and encapsulation
      • immutability helps to make objs composable by eliminating complex life-cycles
  • functions
    • unit of composition: a fn
      • compose functions f and g means g(f(x)) -- f's output becomes g's input
      • if you have 2 fns w/ matching input n output types, they always compose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment