This question already has answers here:
Loop until... with Ramda
(2 answers)
Closed 3 years ago.
I want a function that returns a value when a function returns a value which is not null.
const getSomeValue = R.filter(...);
const getSomeOtherValue = R.propEq('name');
The R.until function is probably the one to use.
Some pseudo code:
R.until(R.isNotNull, R...[getSomeValue, getSomeOtherValue]);
So run through the functions until the returned value is not null.
I don't find any suitable function in the Ramda docs to do this.
Any ideas?
If you don't mind to also ignore falsy values (false, undefined, 0, etc...) then you could use R.either:
const first = () => {
console.log("first");
return null;
};
const second = () => {
console.log("second");
return {a: "banana"};
};
const third = () => {
console.log("third");
return {a: "chicken"};
};
const fn = R.either(first, second, third);
console.log(fn());
<script src="https://unpkg.com/ramda#0.26.1/dist/ramda.min.js"></script>
Related
This question already has answers here:
When should I use a return statement in ES6 arrow functions
(6 answers)
Closed last year.
Why does the second assignment need return and the first returns variable total automatically?
const total = inventors.reduce(((total, inventor) => total += inventor.passed - inventor.born), 0);
const transportation = data.reduce(((prevVal, currentVal) => {
if (!prevVal[currentVal]) {
prevVal[currentVal] = 0;
}
prevVal[currentVal]++;
return prevVal;
}), {})
If return prevVal is omitted I get Uncaught TypeError: Cannot read properties of undefined
What is the difference in how reduce() acts with values. Is it effected by ternary operation? If so then why this returns 0?
const transportation = data.reduce(((prevVal, currentVal) =>
(!prevVal[currentVal]) ? prevVal[currentVal] = 0 : prevVal[currentVal]++), {})
When using curly braces with an arrow function, the return is not implicit, so you have to use return
I am trying to use a pure function that is using For Each, the function will change input array to return 'x'. Can someone help me explain why I am getting this error?
Functions:
let functions = {
helper: (x) => {
return x;
},
changeArray: (x) => {
let arr1 = [x];
arr1.forEach(functions.helper);
return arr1[0];
}
};
Test file:
test('For Each', () => {
expect(syntax.changeArray(['hey', 'hi']).toBe(['x','x']));
})
Result/Error:
TypeError: _syntax.default.changeArray is not a function
73 |
74 | test('For Each', () => {
> 75 | expect(syntax.changeArray(['hey', 'hi']).toBe(['x','x']));
| ^
76 | })
CHANGES:
const syntax{
helper: (x) => x,
changeArray: (arr) => {
return arr.map(syntax.helper);
}
}
TEST FILE:
test('For Each', () => {
expect(syntax.changeArray(['hey', 'hi'])).toBe(['x','x']);
})
RESULT:
expect(received).toBe(expected) // Object.is equality
- Expected
+ Received
Array [
- "x",
- "x",
+ "hey",
+ "hi",
]
There are multiple problems with this
Main one is, what is syntax.changeArray ? Your function is in functions.changeArray.
When you run a function through Array.forEach, the forEach function literally doesn't do anything with the returned value. I think what you want is Array.map
Also, your helper function returns x, not 'x' -- it will return whatever its given, so if you DID pass that helper function to array.map, it would just return the same unchanged value you send it.
This bit of code might hopefully give you an idea of what you should be doing.
function runMap(arr) {
return arr.map(val => 'x');
}
var testArray = [1,2,3];
console.log(runMap(testArray));
Why are we seeing references to both functions and syntax, which seem identical? Let's stick to one, and delete the other. I'll use syntax here.
Here's a definition of syntax that should fix your problems:
let syntax = {
// This change to the "helper" function solves problem #1;
// simply return the string 'x' regardless of the parameter
helper: x => 'x',
// This "changeArray" function return a new `Array` where
// every item has been mapped using `syntax.helper` (which
// will map every Array item to the string 'x'):
changeArray: arr => arr.map(syntax.helper)
};
Fix the logic error in your test suite. Change:
expect(syntax.changeArray(['hey', 'hi']).toBe(['x','x']))
To:
expect(syntax.changeArray(['hey', 'hi'])).toBe(['x','x']);
This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 3 years ago.
I was given the following solution to an exercise:
var s = [23, 65, 98, 5];
Array.prototype.myMap = function(callback) {
var newArray = [];
// Add your code below this line
this.forEach(a => newArray.push(callback(a)));
// Add your code above this line
return newArray;
};
var new_s = s.myMap(function(item) {
return item * 2;
});
the idea is to build your own replacement for .map() method.
My problem is to understand the callback part. So far my understand of a callback, is a funct that is passed into another function (as an arg) and is called after something else.
I cannot see where is another function being passed in as the callback in the solution, so I'm struggling to understand the exercise, mainly: this.forEach(a => newArray.push(callback(a)));
Is there anyone who could clarify this for me?
You can easily visualize synchronous callbacks by replacing the call with the functions code itself.
In your function call, callback is equal to:
function(item) { return item * 2; }
If we insert that into this line:
this.forEach(a => newArray.push(callback(a)));
we get:
this.forEach(a => newArray.push(
/*item = a*/
/*return*/ a * 2;
));
Or in other words, for each a in the array, a * 2 gets pushed to the newArray.
This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 3 years ago.
Its known that someone can make a one-line array function like this to return the single value:
var func = (x) => x + 1 //func(2) == 3
and you can also make multi-line array-functions, whose values need to be manually returned:
var funcMultiline = (x) => {
var result = 1;
result += x;
return result;
}
funcMultiline(4) == 5; //true
So the question:
let's say I want to return a new object in one line, if I use the brackets, then the array-function is treated like a multi-line function, and doesn't actually return the object-literal. Is there any direct way to create an object literal in, lets say, a map function? Like:
[...Array(25)].map(e => {x: 5, y:10}) //this is a syntax error, but how can I make this work
Returning object literals using the concise body syntax params => {object:literal} will not work as expected.
You have to wrap the object literal with parenthesis:
var res = [...Array(25)].map(e => ({x: 5, y:10}))
console.log(res);
This question already has answers here:
In Javascript, when is it necessary to assign a named function to a variable?
(3 answers)
Closed 6 years ago.
I'm learning some more ES6:
const _findCartItem = ( item ) => {
return _cartItems.find( cartItem => cartItem.id === item.id);
};
converts to:
var _findCartItem = function _findCartItem(item){
return _cartItems.find(function(cartItem){
return cartItem.id == item.id;
})
};
However, is there a difference between this and the following? Is this dual declaration necessary?
var _findCartItem = function(item){
return _cartItems.find(function(cartItem){
return cartItem.id == item.id;
})
};
Conceitualy, no, both will do the same. Syntactically, yes. First one is passing a reference of a named function to a variable, second a anonymous function.
Using first option is prefereble because you get better call stacks.