0

I'm new to functional programming and I'm playing around with the ideas of currying and partial implementation in javascript to wrap my head around them. At the moment I'm trying to workout how I can pass a filter function. Below is a line I saw in a tutorial

var seperate = arr => key => val => arr.filter(obj => obj[key] === val);

say I want to make the filter function a curried parameter so I can create partial implementations with different filter functions applied, such as a var isHuman or var isLessThan50 etc.
Maybe I might want to pass as one argument for the partial implementation the dataset its working on so var filterMembers(membershipData); then I would want to make different filtered implementations var femaleMembers = filterMembers(isFemaleFilter); which would be an array.filter() function.

How would one achieve this?


edit:

I wasn't very clear in my qeustion, apologies. I would like for the array and the filter to be variable. I want to compose seperate functions from the partial implementations.

so I might create a partial implementation that works on the membership array or another that works on census data or something. Then I would like to compose functions from those partial implementations passing different filter functions to the partial implentations that work on the different data sets.

I'm afraid I don't know how to write working code that demonstrates my question.

3
  • 1
    What's the content of arr? Running code and a small explanation is better than a long paragraph Commented Feb 14, 2018 at 12:29
  • 1
    const filterMembers = pred => members => members.filter(pred) in which pred is a function of any -> boolean? Commented Feb 14, 2018 at 12:36
  • Your question is not clear enough. Please clearly provide the problem you try to solve (in code) so input data, output data. Also, show us what you've tried but didn't work. Commented Feb 14, 2018 at 12:39

1 Answer 1

2

You need a partial implementation of the filter function:

const filter = fn => arr =>
    arr.filter(fn)

const members = [
  {name: 'eminem', sex: 'male'},
  {name: 'rihanna', sex: 'female'}
]

const filter = fn => arr =>
  arr.filter(fn)
  
const isFemale = x =>
  x.sex === 'female'
  
const filterFemaleMembers = filter(isFemale)

console.log(filterFemaleMembers(members))

It is better to first pass the fn and than the arr to filter because it has a benefit when you compose functions:

const members = [
  {name: 'Eminem', sex: 'male'},
  {name: 'Rihanna', sex: 'female', age: 28},
  {name: 'Lana Del Rey', sex: 'female', age: 31}
]

const filter = fn => arr =>
  arr.filter(fn)
  
const compose = f => g => x =>
  f(g(x))
  
const isFemale = x =>
  x.sex === 'female'

const isOlder30 = x =>
  x.age > 30

const isFemaleAndOlder30 = compose
  (filter (isFemale))
  (filter (isOlder30))


console.log(
  isFemaleAndOlder30 (members)
)

Sign up to request clarification or add additional context in comments.

8 Comments

This is pretty interesting. But the compose function isn't curried, is there no way to achieve the behaviour with fully curried functions?
shure. I changed it to a curried version.
I added some more infromation to my question. I would like to create partial implementations that work on different datasets and then construct different functions that fitler their respective datasets by passing a filter function as one of the function parameters
thanks for the update to your sample. Is there a way I can edit the snippet to see how it works a bit clearer or should I copy it into an external tool?
I've been checking over the code and its rpetty cool, thanks. Took a minute to understand though, not used to this : )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.