Giter Site home page Giter Site logo

effing's Introduction

Build Status

effing.js

Some handy function functions for JavaScript, minus the docs and examples and such.

  • easy partial application: f(func, arg)
  • easy function binding: f(obj, method)
  • easy both at the same time: f(obj, method, arg)
  • function equivalents for operators: [1, 2, 3].reduce(f.add)
  • partial application for operators: [-3, 4, 2].filter(f.gt(0))

Functionoids

The following things are coerced into functions by effing.js:

  • null becomes a no-op function
  • undefined becomes a no-op function
  • [fn, args...] becomes a partially applied function
  • [context, method, args...] becomes a partially applied function with a bound context
  • [context, 'methodName', args...] becomes a partially applied function with a bound context by looking up the specified key on the context

Anywhere effing.js expects a function argument, it will be happy with a functionoid instead.

Note that the [context, ...] functionoids don't allow Functions to be used as contexts. effing.js can't read your mind.

Functionoid conversion

effing.js exports a function, usually called f, that will convert functionoids into normal functions.

When called with one argument, it follows the rules above. When called with no arguments, it returns a no-op function. When called with more than one argument, it behaves as if it were invoked with the arguments in a list.

So f(fn, arg) produces a partially applied function, for example, as if you had called f([fn, arg]).

Operators

Logical operators

operator name
&& and
`
! not

Comparison operators

operator name
< lt
> gt
<= lte
>= gte
=== eq
!== neq

Numeric operators

operator name alias
+ add
- subtract sub
* multiply mult
/ divide div
% remainder rem
- negate neg

effing.js also has some numeric operators that JavaScript doesn't have:

name alias description
modulo mod Like JavaScript's remainder operator, but will never produce a negative value.
intDivide idiv Floor division.

Partial application for binary operators

In Haskell you can say:

isMinor = (< 18)

In f'ing JavaScript, you have to say:

var isMinor = f.lt(18);

When you invoke any of the binary operator functions with a single argument, it will alaways fill in the right side of the operator. To fill in the left argument, you can use normal functionoid partial application: f(f.lt, 18).

effing's People

Contributors

ianthehenry 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.