Archive

Archive for the ‘Javascript’ Category

lodash-fp 2

March 9, 2017 5:41 pm Leave a comment
reduce array/collection   Reduces collection to a value which is the accumulated result of running each element in collection thru iteratee, where each successive invocation is supplied the return value of the previous. If accumulator is not given, the first element of collection is used as the initial value.
       

 

import reduce from ‘lodash/fp/reduce’

const noCapReduce=reduce.convert({ ‘cap’: false })

const red = noCapReduce((result,value,key)=>{

    ((result[value])||(result[value]=[])).push(key)

return result;

},{})

console.log(red({ jey11: 1, jey23: 3, jey223: 3 }))

Advertisements
Categories: Javascript Tags:

lodash fp -1

March 9, 2017 5:38 pm Leave a comment
dropRightWhile array dropElements in array at the end based on predicate Take the last array element and apply the predicate, if that returns false return entire array else drop from end and continue till predicate becomes false from end of array and return remaining array
dropWhile array dropElements in array at the begining based on predicate Take the first array element and apply the predicate, if that returns false return entire array else drop from begining and continue till predicate becomes false from begin of array and return remaining array
every array return true if predicate returns true for all elements of array else return false Checks if predicate returns truthy for all elements of collection. Iteration is stopped once predicate returns falsey
filter array return all array elements for which predicate returns true Iterates over elements of collection, returning an array of all elements predicate returns truthy for.
find array return first element and whole element that matches the predicate Iterates over elements of collection, returning the first element predicate returns truthy for. undefined if not found
findIndex array return first element index that matches the predicate returns the index of the first element predicate returns truthy for instead of the element itself.-1 if not found, index is zero based
findKey object on dictionary with key, name value pairs returns first key whose value matches returns the key of the first element predicate returns truthy for instead of the element itself
findLast array return last element and whole element that matches the predicate Iterates over elements of collection, returning the last element predicate returns truthy for. undefined if not found
findLastIndex array return last element index that matches the predicate returns the index of the last element predicate returns truthy for instead of the element itself.-1 if not found, index is zero based
findLastKey object on dictionary with key, name value pairs returns last key whose value matches returns the key of the last element predicate returns truthy for instead of the element itself
flatMap array return a new array after applying the function on all elements on the array Creates a flattened array of values by running each element in collection thru iteratee and flattening the mapped results.
flatMapDeep array recursively apply flatMap to each array that exists in the main array
const usersArray = [[{ ‘user’: ‘pebbles’, ‘active’: true },

{ ‘user’: ‘barney’,  ‘active’: true }],

[{ ‘user’: ‘fred’,    ‘active’: false }]

];

recursively flattens the mapped results
[ [ { user: ‘pebbles’, active: true },
{ user: ‘barney’, active: true } ],
[ { user: ‘pebbles’, active: true },
{ user: ‘barney’, active: true } ],
[ { user: ‘fred’, active: false } ],
[ { user: ‘fred’, active: false } ] ]
[ { user: ‘pebbles’, active: true },
{ user: ‘barney’, active: true },
{ user: ‘pebbles’, active: true },
{ user: ‘barney’, active: true },
{ user: ‘fred’, active: false },
{ user: ‘fred’, active: false } ]
flatMapDepth array same as flatMapDeep except depth specification recursively flattens the mapped results up to depth times.
forEach array/collection perform a function on each element of array or collection Iterates over elements of collection and invokes iteratee for each element.
forEachRight array/collection perform a function on each element of array or collection from right to left iterates over elements of collection from right to left.
forIn object perform a function on each key that you inherit from object or directly defined on the object Iterates over own and inherited enumerable string keyed properties of an object and invokes iteratee for each property
forInRight object perform a function on each key that you inherit from object or directly defined on the object from Right to left Iterates over own and inherited enumerable string keyed properties of an object and invokes iteratee for each property from right to left
forOwn object perform a function on each key that are directly defined on the object Iterates over own string keyed properties of an object and invokes iteratee for each property
forOwnRight object perform a function on each key that are directly defined on the object from right to left Iterates over own string keyed properties of an object and invokes iteratee for each property from right to left
map array/collection returns a new array by applying function to each element of the array Creates an array of values by running each element in collection thru iteratee.
mapKeys object returns keys associated with the object this method creates an object with the same values as object and keys generated by running each own enumerable string keyed property of object thru iteratee.
mapValues object turns values associated with the object Creates an object with the same keys as object and values generated by running each own enumerable string keyed property of object thru iteratee.
partition array/collection returns true array and false array from collection Creates an array of elements split into two groups, the first of which contains elements predicate returns truthy for, the second of which contains elements predicate returns falsey for
reject array/collection returns elements that returns false for predicate` this method returns the elements of collection that predicate does not return truthy for
remove array/collection removes array elements and returns remaining elements, original array is modified Removes all elements from array that predicate returns truthy for and returns an array of the elements remaining
some array/collection return true if any element in collection passes predicate Checks if predicate returns truthy for any element of collection. Iteration is stopped once predicate returns truthy
takeRightWhile
array/collection return an array from the end while predicate returns true Creates a slice of array with elements taken from the end. Elements are taken until predicate returns falsey
takeWhile array/collection return an array from beginging while predicate returns true Creates a slice of array with elements taken from the begining. Elements are taken until predicate returns falsey
times utility return an array as a result of application of iteratee n times Invokes the iteratee n times, returning an array of the results of each invocation

 

 

Categories: Javascript Tags:

Compile es6 js test file seperately

March 8, 2017 11:40 am Leave a comment

npm install babel-cli –save-dev

node node_modules/babel-cli/bin/babel-node.js your.js

Categories: Javascript

Currying and lodash/fp

March 8, 2017 10:18 am Leave a comment

Briefly, currying is a way of constructing functions that allows partial application of a function’s arguments. What this means is that you can pass all of the arguments a function is expecting and get the result, or pass a subset of those arguments and get a function back that’s waiting for the rest of the arguments.

Argument Order

One thing that’s important to keep in mind when currying is the order of the arguments. Using the approach we’ve described, you obviously want the argument that you’re most likely to replace from one variation to the next to be the last argument passed to the original function.

Thinking ahead about argument order will make it easier to plan for currying, and apply it to your work. And considering the order of your arguments in terms of least to most likely to change is not a bad habit to get into anyway when designing functions.

Here is how the lodash/fp documentation describes itself:

The lodash/fp module promotes a more functional programming (FP) friendly style by exporting an instance of lodash with its methods wrapped to produce immutable auto-curried iteratee-first data-last methods.

Compose yourself

The idea of composing functions (or flow in lodash) is to build a larger function out of many smaller ones. The data goes in one end and flows (ah ha!) through each function until it comes out the other side. Each function in the chain passes its return value to the next.

Let’s see an example using lodash/fp:

const flow = require('lodash/fp/flow');
const escape = require('lodash/fp/escape');
const trim = require('lodash/fp/trim');

const sanitise = flow(escape, trim);

sanitise('    <html>    '); // <html>

You can see that we’ve created a sanitise function from escape and trim and when the HTML string is passed in it flows through these two functions before being returned as expected. Again, we’re just being implicit and declaring what we want to happen, not how.

How does currying help?

lodash/fp functions are auto-curried, so when given fewer arguments than expected they give back a function. This works really well with flow:

const flow = require('lodash/fp/flow');
const get = require('lodash/fp/get');
const isEqual = require('lodash/fp/isEqual');

const data = {
  items: 45
};

const hasAllItems = flow(get('items'), isEqual(45));

hasAllItems(data) // true

Here we’re configuring the get and isEqual functions with the arguments needed to get the result, and now they’re waiting for their final argument which is the data. Thanks to flow we can pass that in at one end and let it pass through each curried function until our expected value comes out at the end.

For composition to work each function should be unary (accept one argument) and return a single value for the next function to consume. It’s fine for the first function to be polyadic (takes multiple arguments) as long it returns a single value.

A note on point-free

You may have noticed that in the last two examples you can’t see any mention of data or itemsapart from when it is passed in as an argument. From the Wikipedia page:

Tacit programming, also called point-free style, is a programming paradigm in which function definitions do not identify the arguments (or “points”) on which they operate. Instead the definitions merely compose other functions, among which are combinators that manipulate the arguments.

Categories: Javascript Tags: