2 Replies - 430 Views - Last Post: 28 October 2017 - 09:51 PM

#1 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,073
  • Joined: 08-June 10

functional equivalent of if()

Posted 21 August 2017 - 02:40 AM

Hello,

I have a list that is filtered for a certain condition and if that list doesn't contain more than one result another function should be applied. With imperative programming that is done using if() but I'd like to know if that's also possible with functional programming.

// imperative
$candidates = $('select').find('option').filter(/* ... */);

if ($candidates.length === 1) { // would work equally well with `< 2`
    $candidates.prop('selected', true);
}



Is This A Good Question/Topic? 0
  • +

Replies To: functional equivalent of if()

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,491
  • Joined: 08-August 08

Re: functional equivalent of if()

Posted 28 October 2017 - 05:10 PM

Have you found a solution? Probably not what you're looking for, but I was think something like this might be useful:
$candidates = $('select').find('option').filter(/* ... */);
$canditates.prop('selected', $candidates.length < 2);

Was This Post Helpful? 1
  • +
  • -

#3 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,578
  • Joined: 13-June 14

Re: functional equivalent of if()

Posted 28 October 2017 - 09:51 PM

If really what you want to express is that there is a single value or no value, you use the Maybe (or Option) type in functional programming. I don't know what libraries exist in JS for functional ideas, though.

As an aside, I just wrote a tutorial on Java's implementation of Maybe, called Optional. You might like to have a look to get an idea of the concepts.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1