Giter VIP home page Giter VIP logo

1-liners's Introduction

MADE IN SWITZERLAND

Coverage Status Build Status Dependency Status npm version Stability: unstable Join the chat at https://gitter.im/1-liners/1-liners

Functional tools that couldn’t be simpler.

We’re proud to present 1-liners – a dead simple functional utility belt. 137 one-liner functions (and counting). Each hand-crafted with love and attention.










Our decalogue

You get a product of top-quality functional programming craftmanship. Each function follows the KISS principle, which we’ve broken down into ten strict rules.

We always follow them. You have our word.



1-liners


  1. Each function shall fit in one readable line of code
    – take a glimpse at the source and you know exactly what’s going on.
     

  2. Each function shall have no side-effects
    – you can use it with confidence.
     

  3. Each function shall have a fixed number of arguments
    – it’s dead easy to bind, curry and uncurry, apply partially, implode and explode.
     

  4. Each function shall deal with data in an immutable way
    – no more debugging nightmares.
     

  5. Each function shall take data as the last argument
    – this makes currying and composing new functions a breeze.
     

  6. Each function shall be in a separate micro-module
    – you only load/bundle the single 1-liner you need.
     

  7. Each function shall be thoroughly tested
    – 100% code coverage guaranteed. We even test if every function fits in one line!
     

  8. Each function shall have great documentation
    – the docs explain usage, present the source, and link to the specs.
     

  9. We shall provide functional versions of native object methods
    – like reduce(callback, array) for array.reduce(callback).
     

  10. We shall provide functional versions of language constructs
    – like plus(a, b) for a + b.
     

Usage

Install

$ npm install --save 1-liners

Usage in ES5

// The lightweight, recommended way:
var map = require('1-liners/map');

// Sometimes practical:
var map = require('1-liners').map;

Usage in ES 2015 (formerly ES6)

// The lightweight, recommended way:
import map from '1-liners/module/map';

// Sometimes practical:
import { map, filter } from '1-liners/module';

API

Checkout the documentation

Maintainers

stoeffel tomekwi hemanth
stoeffel tomekwi hemanth
davidchase tristaaan
davidchase tristaaan

created with gh-contributors-table

License

MIT © stoeffel tomekwi hemanth davidchase tristaaan

1-liners's People

Contributors

adamchainz avatar addityasingh avatar alx-l avatar christiaanwesterbeek avatar derhuerst avatar dkunin avatar fbn avatar gitter-badger avatar guumaster avatar hemanth avatar hueitan avatar jfsiii avatar robinpokorny avatar scf4 avatar shfshanyue avatar srinivasdamam avatar stefanmaric avatar stoeffel avatar tristaaan avatar unlight avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

1-liners's Issues

Logo

Which one do you like guys? Feedback?

@hemanth

  • green
  • red

@tomekwi

  • green
  • red

@stoeffel

  • green
  • red

I have just one revision at fiverr and I will get it as a SVG.

green red
1liners01 1liners02
1liners01 1liners02

Boast about how great this is :)

Mention that:

  • all functions are pure
  • all functions have a stable number of arguments
  • all functions are in one line
  • we have functional versions of many native object methods
  • and functional versions of most operators

How about:

  • showing the source of every function in the readme

Mark as unstable

There’s lots of fiery development going on at the moment. I know the 0.x semver version already means the same – but why not warn people even so?

How about one of these ugly guys https://github.com/badges/stability-badges:

unstable

Or some prettier ones http://shields.io:

Stability: unstable Stability: unstable

This way taking off the “unstable” badge will be a point to look forward to and celebrate :D

isType

Add functions to check if argument is of a type

  • isString
  • isObject
  • isPlainObject
  • isBoolean
  • isNumber
  • isFunction
  • isTypeOf === typeof arg === 'function'
  • isTrue
  • isFalse
  • isNull
  • isUndefined

Documentation

  • TOC in the readme
  • add them as owners
  • create a nice logo (WIP)
  • contributors list

Curry all the things

How do you feel about adding a step to the prepublish task to create a folder ./curry with all the functions curryied.
So a user can require i.e.

var ifElse = require('1-liners/curry/ifElse');
ifElse(cond)(then)(otherwise);

The generated modules in curry would look like this:

var curry = require('../curry3'); // 1, 2, 3 or 4 the task needs to check the length of the function
module.exports = curry(require('../ifElse'));

// cc @hemanth @tomekwi @tristaaan

A way to pull in the whole lot

When size doesn’t matter, you’d sometimes just want to save 5 lines of code and write:

import {flip, curry2, split, and, or, isTruthy} from '1-liners/module';

Here’s one way to do it – which works both for require and the ES6 import:

Add `put`

Like 101/put.

For example:

(key, value, object) => Object.assign({}, object, {[key]: value});

Rename parameters

#94 (comment)

We want them to match those at MDN. For example, instead of:

// 1-liners/reduceFrom
export default (reduce, initial, arr) => arr.reduce(reduce, initial);

we’d want:

export default (callback, initialValue, array) => array.reduce(callback, initialValue);

How to handle flexible-length functions?

In JS:

arr.reduce(callback[, initialValue])

In 1-liners:

reduce

Same as [1, 2, 3].reduce(sum).

var reduce = require('1-liners/reduce');

reduce(sum, [1, 2, 3]); // => 6
SpecSource: (reduce, arr) => arr.reduce(reduce);

We don’t want to overload the function, because it will make auto-currying impossible. How about this then:

const reduce = require('1-liners/reduce/2');  // or just '1-liners/reduce'

reduce(callback, array);
const reduce = require('1-liners/reduce/3');

reduce(callback, array, initialValue);

Idea: opt-in auto-curry?

I’ve never really had the time to get my hands on it, but AFAIK pure functional languages come with currying out of the box.

I like it how 1-liners stays lean – without dependencies – without inter-module imports – without auto-currying and other fancy. But I often find myself currying half of the 1-liners functions I import.

How about creating another repo with all functions auto-curried by default? With chickencurry or something.

import between from '1-liners-plus/between';

between(1, 10, 2.5) ===
between(1)(10, 2.5) ===
between(1, 10)(2.5) ===
between(1)(10)(2.5);

Converge

Converges two functions into one.

converge(plus, times, by)(3, 3) // === plus(times(3, 3), by(3, 3))

`ifElse` violates our decalogue

export default (pred, ifDo, elseDo=()=>{}) => (...args) => pred(...args) ? ifDo(...args) : elseDo(...args);

It doesn’t have a stable number of arguments. I suggest splitting this down into two functions:

export const ifThen (predicate, then) => 
export const ifThenElse (predicate, then, else) => 

Then it’ll also be more clear from the name of the function what it does.

Includes is missing

We should add includes.

includes(2, [1, 2, 3]); // => true
var includes2 = curry(includes)(2);
includes2([1, 2, 3]); // => true

match

match(regEx, string)

Moving documentation to jsdoc

/**
 * @module and
 * 
 * Same as `a && b`.
 *
 * @example
 *   var and = require('1-liners/and');
 * 
 *   and(true, true); // => true
 *   and(false, true); // => false
 */
export default (x, y) => x && y;

Todolist

  • and
  • bitAnd
  • or
  • bitOr
  • not
  • compose
  • nand
  • nor
  • nth
  • noop
  • apply
  • pipe
  • length
  • inc
  • dec
  • equal
  • looseEqual
  • lt
  • gt
  • le
  • ge
  • bind
  • call
  • has
  • converge
  • isString
  • isObject
  • isBoolean
  • isNumber
  • isInt
  • isPlainObject
  • plus
  • minus
  • times (I don’t like this name)
  • by
  • isTrue
  • isFalse
  • isNull
  • isUndefined

Function

  • curry1,2,3,4

Array functions

  • filter(function, array)
  • reduce(function, array)
  • every(function, array)
  • some(function, array)
  • reduceRight(function, array)
  • reduceFrom(function, initialValue, array)
  • reduceRightFrom(function, initialValue, array)
  • forEach(function, array)

String functions

  • join(string, array)
  • split(splitStr, string)
  • word
  • csv

RegExp

  • replace(regEx, string)
  • match(regEx, string)

Error at `npm run istanbul`

This is what happened to me after a fresh npm install (only the relevant part):

> [email protected] istanbul …/1-liners/1-liners
> istanbul cover ./node_modules/mocha/bin/_mocha  -- --report lcov --compilers js:babel/register --ui qunit ./tests

No coverage information was collected, exit without writing coverage information
…/1-liners/1-liners/node_modules/babel/node_modules/babel-core/lib/babel/transformation/file/logger.js:37
    throw new Constructor(this._buildMessage(msg));
          ^
ReferenceError: [BABEL] …/1-liners/1-liners/tests/always.js: Unknown option: attachAuxiliaryComment
    at Logger.error (…/1-liners/1-liners/node_modules/babel/node_modules/babel-core/lib/babel/transformation/file/logger.js:37:11)
    at File.normalizeOptions (…/1-liners/1-liners/node_modules/babel/node_modules/babel-core/lib/babel/transformation/file/index.js:154:29)
    at new File (…/1-liners/1-liners/node_modules/babel/node_modules/babel-core/lib/babel/transformation/file/index.js:133:22)
    at TransformerPipeline.transform (…/1-liners/1-liners/node_modules/babel/node_modules/babel-core/lib/babel/transformation/transformer-pipeline.js:96:16)
    at Object.transformFileSync (…/1-liners/1-liners/node_modules/babel/node_modules/babel-core/lib/babel/api/node.js:108:42)
    at compile (…/1-liners/1-liners/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:109:20)
    at Object._fs2.default.readFileSync (…/1-liners/1-liners/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:143:18)
    at Module._extensions.(anonymous function) (…/1-liners/1-liners/node_modules/istanbul/lib/hook.js:103:29)
    at istanbulLoader (…/1-liners/1-liners/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:156:3)
    at Object.require.extensions.(anonymous function) [as .js] (…/1-liners/1-liners/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:173:7)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at …/1-liners/1-liners/node_modules/mocha/lib/mocha.js:192:27
    at Array.forEach (native)
    at Mocha.loadFiles (…/1-liners/1-liners/node_modules/mocha/lib/mocha.js:189:14)
    at Mocha.run (…/1-liners/1-liners/node_modules/mocha/lib/mocha.js:422:31)
    at Object.<anonymous> (…/1-liners/1-liners/node_modules/mocha/bin/_mocha:398:16)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Object.Module._extensions.(anonymous function) (…/1-liners/1-liners/node_modules/istanbul/lib/hook.js:107:37)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at runFn (…/1-liners/1-liners/node_modules/istanbul/lib/command/common/run-with-cover.js:122:16)
    at …/1-liners/1-liners/node_modules/istanbul/lib/command/common/run-with-cover.js:248:17
    at …/1-liners/1-liners/node_modules/istanbul/lib/util/file-matcher.js:68:16
    at …/1-liners/1-liners/node_modules/istanbul/node_modules/async/lib/async.js:251:17
    at done (…/1-liners/1-liners/node_modules/istanbul/node_modules/async/lib/async.js:132:19)
    at …/1-liners/1-liners/node_modules/istanbul/node_modules/async/lib/async.js:32:16
    at …/1-liners/1-liners/node_modules/istanbul/node_modules/async/lib/async.js:248:21
    at LOOP (fs.js:1474:14)
    at process._tickCallback (node.js:355:11)

Idea: have only one simple curry function

import curry from '1-liners/curry2';

const f = (a, b, c, d) => a * b - c * d + e;
const  = curry(f)                       // ~= curry2       
const  = curry(curry(f))                // ~= curry3                
const  = curry(curry(curry(f)))         // ~= curry4                           
const  = curry(curry(curry(curry(f))))  // ~= curry5

f(1, 2, 3, 4, 5)  === -5

(1)(2, 3, 4, 5) === -5
(1, 2)(3, 4, 5) === -5
(1, 2, 3)(4, 5) === -5
(1, 2, 3, 4)(5) === -5

(1)(2)(3, 4, 5) === -5
(1)(2, 3)(4, 5) === -5
(1, 2)(3, 4)(5) === -5

(1)(2)(3)(4, 5) === -5
(1)(2)(3, 4)(5) === -5
(1)(2, 3)(4)(5) === -5
(1, 2)(3)(4)(5) === -5

(1)(2)(3)(4)(5) === -5

Let’s just leave curry2 and name it curry.

Less is more.

Date functions

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.