Interpreted, dynamic typing, functional
Compiled, static typing, imperative (procedural)
| 'use strict'; | |
| var R = require('ramda'); | |
| var blank = function(value) { | |
| return value == null || (typeof value === 'string' && value.trim().length === 0); | |
| }; | |
| var present = function(value) { | |
| return !blank(value); |
| // BEFORE REFACTORING (IMPERATIVE) | |
| var filteredLinks = []; | |
| var weekdayCounts = {}; | |
| // Limit to 4 links per weekday. | |
| // Loop through all links and keep track of | |
| // how many are in each weekday. | |
| widget.links.forEach(function(link) { | |
| var weekday = link.group; | |
| if (weekdayCounts[weekday] === undefined) { | |
| weekdayCounts[weekday] = 0; |
| // 1. Swith two variables | |
| // ES5 | |
| var temp = a; | |
| a = b; | |
| b = temp; | |
| // ES6 - destructuring assignment | |
| [b, a] = [a, b]; |
| # Simple wrapper to allow hashes to be accessed via dot notation recursively. | |
| # Recurses over hashes and arrays. Works with string keys | |
| # and symbol keys - other types of keys are not supported and | |
| # all keys must be of the same type. Write access is only supported via | |
| # []= Hash syntax. Supports accessing hash values with square bracket Hash syntax ([...]) | |
| # and access is indifferent to if the key is given as a string or a symbol. | |
| # Supports JSON generation. | |
| # | |
| # Dependencies: Ruby. | |
| # |
| # Running a single minimalistic unit test with Rails 4 and Ruby 2.1.1 is a lot faster | |
| # if you avoid loading Rails: | |
| # Running the test *with* Rails | |
| time ruby -Itest test/models/my_model_test.rb # => real ~ 6s | |
| # Running the test *without* Rails | |
| time ruby -Itest/no_rails test/models/my_model_test.rb # => real ~ 0.6s |
| # NOTE: There must be a better way to do this - help appreciated! | |
| ############################################################### | |
| # uploaded_file.rb - utility class | |
| ############################################################### | |
| class UploadedFile | |
| def self.size(file) | |
| uploaded?(file) ? file.size : nil | |
| end |
| // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
| // Hooking up the middleware with the express app | |
| // In app.js | |
| // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
| var forceSSL = require('../middleware/ssl').force(config.hostname); | |
| if ('production' == app.get('env')) { | |
| app.use(forceSSL); | |
| } |
| ################################################################# | |
| # | |
| # File: lib/api.rb | |
| # | |
| ################################################################# | |
| require 'user' # ActiveRecord model | |
| # Connect to the db | |
| ActiveRecord::Base.establish_connection(ENV['DATABASE_URL']) |
| # Poor man's identity map for reusing ActiveRecord objects and avoid fetching the same | |
| # record multiple times from the database within a request or background job. The check method can | |
| # be used to locate duplicate queries in development. The goal is to instantiate | |
| # each ActiveRecord object only once. The Rails identity map is known to have issues: | |
| # http://stackoverflow.com/questions/6905891/rails-3-1-identity-map-issues | |
| class IdentityMap | |
| # Set one or more ActiveRecord objects in the map | |
| def set(*objects) | |
| objects.each do |object| | |
| map[key(object)] = object |