Think Different

part description. yadayada. etc.

Different styles of Functions in Ruby

Defs

Lambda’s

Short Lambda’s

Lambda’s as constants

Functional Patterns in Standard Ruby

Functions as the Atoms of an Interface

Isn’t all code functional ?

FP proceeds from a startling premise—that we construct programs using only pure functions, or functions that avoid side effects like changing variables(state), writing to a database or reading from a file.

In a pure functional programming language, everything is a function. we can pass them around and “calculate” with them (combine, evaluate or partially evaluate, etc).

A function defines a mapping from a “Domain” into the “Codomain” (image, range)

Advantages

  • No side effects, no implicit hidden state, less bugs.
  • No variables ! (only values) ⇒ optimizations
  • Can (more easily) be parallelized over cpu’s (multicore), etc. ⇒ no locks, no race conditions, no deadlocks
  • Enables Provability (both for humans and computers)

FP versus OO

Whereas an object-oriented mindset will foster the approach of defining an application domain as a set of nouns (classes) [ person, ticket, etc ]

The functional mind will see the solution as the composition or verbs (functions) [ register, sell ]

Though both programmers may in all likelihood generate equivalent results, the functional solution will be more succinct, understandable, and reusable. Grand claims indeed!

Why Bang means Watch Out (Imutable)

Explain about the immutable Pattern in Ruby

  • An OO pattern that actually originates in FP world
  • Instead of changing a data structure, don’t modify in place but create a new object.

rework:

Why should your objects be immutable ? In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change but the object’s state appears to be unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object. Immutable objects are often useful because they are inherently thread-safe. Other benefits are that they are simpler to understand and reason about and offer higher security than mutable objects. In Ruby this is typically the default. Methods that don’t follow this principle are assumed ‘dangerous’ and are typically marked with a ‘!’

1 name.reverse => returns a new string that contains the reversed name
2 name.reverse! => replaces the name with the reversed value

Iterator and Visitor Revisited

No more Factory Method, Strategy, Template Method

Design Patterns

Are “The design patterns” fundamental or actually just an expression of language incompleteness? Most of the traditional GoF patterns are about fundamental concerns but will eventually be absorbed by a fundamental language feature or a framework. A Pattern as such might become a feature but the essence remains that it will still have a name and it will still ease communication. But as CS evolves, patterns will remain a powerful means to optimize communication between CS engineers.

Pulling your data through the blender

Functional processing of data: Filter-Sort-Map-Reduce

Taking a look at the Enumerable methods Run over all key methods with good examples

  • Map
  • Inject
  • Partition
  • Find
  • Drop, Take
  • Each_Slice
  • Min, Max, Minmax
  • Sort, Reverse
  • BSearch

Find

1     - [Find, Partition, Grep, BSearch]()

Sort

1     - [Sort, Reverse]()

### Map

1     - [Map, Flatmap, Zip]()

### Reduce

1     - [Drop, Take, Each_Slice]()
2     - [Min, Max, Minmax]()

Exercises

Group-Theory: Permutations-Combinations-Products

List comprehensions

### Permutations ### Combinations ### Product

Exercises

Coming back to the basic algorithms

Basic algorithms: rewrite

## Sorting algorithms: rewrite ## Backtracking algorithms: rewrite

Exercises