Giter Site home page Giter Site logo

functional-programming-weekly-challenge's Introduction

Functional Programming Weekly Challenge

What is Functional Programming?

From Wikipedia:

In computer science, functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids state and mutable data.

Hrm, that probably doesn't help much. Check out some of these additional lectures / videos / stuff that helps to disambiguate the term:

  1. Pete Kocks, "What is functional programming?" - 3 minutes long, basic overview
  2. Martin Odersky, "Programming Paradigms"

Why Functional Programming?

Now that you have some vague idea as to what functional programming is - you want to know what it's good for... right?

  1. Rich Hickey, "Simple Made Easy" - thesis: imperative programs are too complex
  2. Martin Odersky, "Working Hard to Keep It Simple" - thesis: functional programs are better-adapted for modern hardware
  3. Erik Meijer, "Fundamentalist Functional Programming" - thesis: tie-dyed shirts are where it's at

General Rules

  1. Use whatever language you like
  2. Once you've set the value of a variable, don't change it (even if your language allows you to)

In case #2 is a bit murky, check out these examples of legal / illegal codez:

# bad
x = [1,2,3]
x << 

# good
x = [1,2,3]
y = x + 4

# bad
x = 10
x += 1

# good
x = 10
y = x + 1

# bad
x = [1,2,3]
ys = []
append_num_plus_one = lambda { |n| ys << (n + 1) } # mutates ys (accessible via closure) as a 'side-effect'
x.each(&append_num_plus_one)

# good
x = [1,2,3]
add_one = lambda { |n| n + 1 } # a 'pure' function; accepts a value, returns a value
y = x.map(&add_one)

Functions that operate on array-like structures that return new arrays are fair-game (map, reduce). Destructive methods (Ruby's concat and map!) should be avoided at all costs!

Some Tips:

Leverage First-class Functions

Functions as values that can be passed around like integers / strings / whatever:

add_one = lambda { |n| n + 1 }
[1,2,3].map(&add_one)

Use Higher-order Functions

Functions that do one or more of the following:

  1. Accept functions as arguments
  2. Return functions
// ex 1: accepts addOne function as argument

var x = [1,2,3]
x.map(function addOne(n) {
  return n + 1;
}); // [2,3,4]

// ex2: returns a function, then passed to map

var addN = function(n) {
  return function(m) {
    return m + n;
  };
};

var x = [1,2,3];
x.map(addN(5)); // [6,7,8]

How to Join the Fun

  1. Fork this repo!
  2. Clone your fork:
% git clone [email protected]:username/functional-programming-weekly-challenge.git
% cd functional-programming-weekly-challenge
  1. Set this repo as the upstream:
% git remote add upstream [email protected]:carbonfive/functional-programming-weekly-challenge.git
  1. Under the folder for a given week, create a folder for your solution files. Name it after your Github username.
  2. Commit and push the changes to your fork.
  3. Create a pull request back to this repo to show off your answers!
  4. Pull in future challenges:
% git fetch upstream
% git merge upstream/master

Each week we will add a new folder containing a README.md presenting a challenge. For example, here is the first week's challenge!

functional-programming-weekly-challenge's People

Contributors

clarkcutler avatar mwean avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.