functionaljs / functional-js Goto Github PK
View Code? Open in Web Editor NEWA functional JavaScript library that facilitates currying and point-free programming
A functional JavaScript library that facilitates currying and point-free programming
I like this and I'm thinking of using it in my next project but I'm struggling to understand why currying increases arity and whether this is a good idea. Is it taken from another functional language? The only one I am familiar with is Haskell which does not do that.
When I try currying a function with 3 inputs and then increasing the arity, I get strange results:
addAndMult = fjs.curry((x, y, z) => (x + y) * z
addAndMult (2, 6, 3)
24 // as expected: 8 times 3
addAndMult(2, 6, 3, 1)
0 // why?
addAndMult(2, 6, 3, 1, 2)
2 // why?
addAndMult(2, 6, 3, 1, 2, 3)
10 // why?
I am attempting to fold a curried function across an array in the following way:
let curryGrow = fjs.curry(function grow(separator, acc, elem) {
return acc + '' + elem + separator;
});
let growComma = curryGrow(',');
let result = fjs.fold(growComma, 'z')(['a', 'b', 'c']);
The final value of result in this case is the String '201000za,b,c,', while I would have expected the value to be 'za,b,c,'.
This can be worked around with an explicit function:
let grow = function(separator, acc, elem) {
return acc + '' + elem + separator;
};
let growComma = function(acc, elem) {
return grow(',', acc, elem);
};
let result = fjs.fold(growComma, 'z')(['a', 'b', 'c']);
Am I using the library correctly? I was attempting to do something the equivalent of in Haskell:
grow :: String -> String -> String -> String
grow separator acc n = acc ++ n ++ separator
foldl (grow ",") "z" ["a", "b", "c"]
The code used is just intended as a minimalist example, I do realise there are better ways to do it.
Edit: This is in Node.js 4.4.5
Array.isArray is native JavaScript and easy to read.
The isArray function is part of the Array.prototype object in ES2015
The bower search package points to https://github.com/leecrossley/functional-js which doesn't exist.
It should point to https://github.com/functionaljs/functional-js
First of all, thank you for contributing this library, I find it nice and useful!
I ran into an issue trying to fold a NodeList. Consider this:
fjs.fold(function(data, field) {
data[field.name] = field.value;
return data;
}, {}, form.querySelector('[name]'));
This is failing silently, apparently because fjs.isArray
returns false when given a NodeList. It might be a rash suggestion, but why not remove that check in fjs.each
? Then (1) array-like structures (another example is function arguments) would be handled OK, and (2) some kind of error will be raised when trying to use a non-array (I would argue it is preferable not to let stuff like fjs.each(console.log, 42)
go through without a complaint).
Not an issue, but what do you think about including a "project" function to return a projection over a dataset, something like this:
projectAddress = project(["house", "street"]);
data = [{house: 24, street: "Main", name: "John"}, {house: 35, street: "Park", name: "Phil"}]
projectAddress(data)
// [ { house: 24, street: 'Main' }, { house: 35, street: 'Park' } ]
Here's a quick implementation:
projectOne = fields => obj => fields.reduce((x,y) => {x[y] = obj[y]; return x}, {})
project = fields => fjs.map(projectOne(fields))
Reminding you this is valid now:
const f = (a, b) => a + b;
http://functionaljs.com is inaccessible and it looks to be a DNS issue.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.