I have this object: I need to remove all key/value pairs in this object where the value is blank i.e. ” So the caste: ” property should be removed in the above case. I have tried: But this doesn’t do anything. reject doesn’t work either. What am I doing wrong? Answer You can use R.reject (or R.filter) to remove properties
Tag: functional-programming
How do I `.filter()` an array/object and return a new array with the original keys and not as an indexed array/object as filter return?
I want to filter all (“isActive” === 0) but keep the key set the same (which is equal to the user id) when returning the newObj: This is what I have now: which returns indexed keys no for loops (unless the ES6 .forEach() is a must). I would like an ES6 approach to this problem using filter/map/reduce if possible in
Lodash reject point free
Is it possible to avoid point in this example? Answer Reject all items that are undefined or null using _.isNil():
How can I avoid nested ternary expressions in my code?
I have code like this. How can I write it in cleaner, more elegant way using functional programming in JavaScript? I want to get rid of nested ternary expressions. Any ideas? This is rest of that code: EDIT: Answer Yes, but my linter is not happy: 44:16 error Do not nest ternary expressions [no-nested-ternary] If that’s your only problem then
Currying a function that takes infinite arguments
Using ES5, how do you curry a function that takes infinite arguments. The function above takes only three arguments but we want our curried version to be able to take infinite arguments. Hence, of all the following test cases should pass: Here is the solution that I came up with: However, I have been told that it’s not very “functional”
reduce function composed of map function in JavaScript
Say we have and want to reduce() it like Now, I want to compose reduce() function itself from map() function. How would you do that? What is the smartest way? Edit: In the comments and answers, many have claimed fold/reduce can compose map, in shallow level, that can be true, however, in category theory, fundamentally reduce/fold is generalized to Catamorphism
Create an array with all numbers from min to max without a loop
I have two numbers, min and max, and I want to create an array that contains all number between them (including min and max). The most obvious approach is to use a for loop for this, and push the single values onto an array. Nevertheless, this seems to be a quite naive approach, i.e. it’s imperative programming. Now I was
Get first element of a collection that matches iterator function
I would like to achieve something like _.first with _.filter, that is, having a collection of elements, I’d like to get the first one (if exists) that matches a truth test (iterator). For example, given an array like the following: I would like to getthe first (and only first) element that matches my custom function: So far: Is there a
Can anyone explain me what is state and mutable data?
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. http://en.wikipedia.org/wiki/Functional_programming Can anyone explain me what is state and mutable data? Can anyone give me examples in either JAVA or JavaScript. Answer mutable suggest anything that can change, i.e. an int In java a string is
Uncaught TypeError: Illegal invocation in JavaScript
I’m creating a lambda function that executes a second function with a concrete params. This code works in Firefox but not in Chrome, its inspector shows a weird error, Uncaught TypeError: Illegal invocation. What’s wrong with my code? Answer The console’s log function expects this to refer to the console (internally). Consider this code which replicates your problem: Here is