2

everyone. I'd like to find an element in an array of records in Purescript but since I'm not familiar with Purescripot, I can't solve it.

I have an array banks which contains bank records.

This is the type of bank record.

type Bank = {
  id :: Int,
  name :: String
}

I want to get a bank in banks whose id is the same as a given search id.

I tried as following:

find (_.id == searchId) banks

but getting this error.


Could not match type
    Int
  with type
    Function
      { id :: t0
      | t1
      }

Please help me with this simple issue.

2 Answers 2

5

The expression _.id is a function that takes a Bank and returns its id (a bit oversimplifying, but good enough for now).

To illustrate:

getId = _.id
bank = { id: 42, name: "my bank" }
getId bank == 42

And then you take that function and try to compare it with searchId, which I'm assuming is a number.

Well, you can't compare functions with numbers, and that's what the compiler is telling you: "Could not match type Int with type Function"

The function find expects to get as its first argument a function that takes a Bank and returns a Boolean. There are many ways to produce such a function, but the most obvious one would be with a lambda abstraction:

\bank -> bank.id == searchId

So to plug it into your code:

find (\bank -> bank.id == searchId) banks
Sign up to request clarification or add additional context in comments.

Comments

1

You can change your code like this.

find(\{id} -> id == searchId) banks

So you can get the result object.

Comments

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.