- Part 1: An Intro to Functional Programming Concepts in JavaScript
- Part 2: An Intro to Functional Programming Concepts in JavaScript
- Part 3: An Intro to Functional Programming Concepts in JavaScript
- Dipping into wu.js: autoCurry
- Haskell in ES6: Part 1
- [Haskell in ES6: Part 2](http://casualjavascript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Strategy Pattern in Javascript | |
| * Can replace a naive switch case | |
| * Essentially it is a way to evaluate an input at runtime and execute predefined code based off of that input. | |
| * | |
| * Encapsulates what varies | |
| * Favor composition over inheritance | |
| * Program to interfaces, not implementations | |
| * Another benefit of the Strategy pattern is that it can hide complex logic or data that the client doesn't need to know about. | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * I have a USER model that comes from two different sources: Database and Web services. | |
| * When receiving USER model from these sources, they both have different form, | |
| * but basically contains almost the same data. | |
| * I want to parse this external data forms into transitional data form that my application understands. | |
| * This transitional data form is called Correspondence model (in MDD theory). | |
| * When I have both external models in Correspondence model, I want to merge them and map the result to the API model. | |
| * The API model is exposed from the application through the JSON REST API. | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Procedural style | |
| function validate(values) { | |
| if (!values.first) { | |
| return false; | |
| } | |
| return values.last; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Delegation using Object.create() (Prototype delegation) | |
| * This pattern improves things by: | |
| * - Delegation builds functionality by composing (via links) regular objects, not by using constructor functions. | |
| * - Delegation uses more specific method names on the delegator objects that are reflective of the actions they perform. | |
| * Inheritance keeps method names fairly generic, as sub-classes tend to re-implement specific behavior that override | |
| * the base classes method. | |
| * - State is typically maintained at the delegator level, not in the delegatee objects. | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // General helper to create the builder pattern automatically | |
| const make = target => ({ | |
| with(source) { | |
| return Object.assign(target, source); | |
| }, | |
| }); | |
| // Let's make a spaghetti | |
| const pasta = { ingredients: [] }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| UI state with data | |
| Software programs are just transformations of data. | |
| Given input A, the program P produces output B. | |
| It's easy to see this pattern in small Unix command-line tools, | |
| but you could also apply it to larger systems, and why not a user-interface? | |
| */ | |
| const State = {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const show = function (output) { | |
| if (typeof output !== 'undefined') { | |
| console.log(output); | |
| } | |
| }; | |
| // composes a vehicle object | |
| const makeVehicle = function (spec) { | |
| const vehicle = {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 'Prototype' pattern | |
| ------------------- | |
| Traditional pattern to emulate classical inheritance in JavaScript | |
| Pros: | |
| - already used by majority of OO JS developers | |
| - performs well |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // in Javascript, is completely pointless. | |
| // Structure and example | |
| var commandPattern = (function(){ | |
| var commandSet = { | |
| doSomething: function(arg1, arg2) { | |
| return "This is argument 1 "+ arg1 + "and this is arg 2 "+ arg2; | |
| }, | |
| doSomethingElse: function(arg3) { | |
| return "This is arg 3 "+arg3; | |
| }, |