$iiff()

By Degausser on Nov 13, 2016

It's probably been done, but here's a varient of the $iif identifier which helps keep everything cleaner and easier

$iiff(1 > 2 || 1 > 3, $true, 2 > 5 && 2 > 1, $true, $abs(-5) > 6, $true) <-- returns $false
$iiff(1 == 1 && 2 > 3, $false, 4 isnum, $true) <-- returns $true
$iiff(1 < 0 || 1 > 3, $true, $abs(-6) > 7, $true, findme) <-- returns findme
var %var = 5
$iiff(1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2 || 1 < %var, %var, $false) <-- returns 5

You can also utilise booleon comparison

$iiff(7 isnum 6-8).bool <-- returns $true
$iiff(1 > 2, $true, cat == cat).bool <-- returns $true
$iiff(1 > 2 && 1 < 3 && 1 < 4, $true, 1 > 2 || word !isalpha, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2 || 1 < 5, 5, $false).bool <-- returns 5

Operations with a one return value are more consistant

$iiff(1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 < 2, thereturn).bool <-- returns thereturn
$iiff(1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 < 2, thereturn) <-- returns thereturn
$iiff(1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2, thereturn).bool <-- returns $false
$iiff(1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2, thereturn) <-- returns $false

However, these lines return different things

$iiff(1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 < 2).bool <-- returns $true
$iiff(1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 < 2) <-- returns 1 < 2
$iiff(1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2).bool <-- returns $false
$iiff(1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2, $true, 1 > 2) <-- returns 1 > 2

Limitations:

Does not support text containing multiple words
$iiff(i walk the dog isalpha, $true)
Does not support || and && operators in a return value when using .bool
Does not support encapsulation with brackets, such as
((1 == 1 && 1 < 2) && 5 > 7)
Does not support the mixed use of || and && in a single comparison, such as
(1 == 1 && 1 < 2 || 5 < 7)

iiff {  
  if ($isid) {
    if ($prop == ev) { if ($1 $2) return $true | return $false }
    var %i = 1 , %n = $0
    while (%i < %n) {
      if ($eilifbreak($eval($+($,%i),2),%a)) return $eval($+($,$calc(%i + 1)),2)
      inc %i 2
    }
    return $iif($prop == bool,$eilifbreak($eval($+($,%i),2)).ev,$iif($eval($+($,%i),2),$ifmatch,$false))
  }
}
eilifbreak {
  if ($prop == ev) {
    tokenize 32 $1-
    if ($0 == 3 && ($1 $2 $3)) return $true
    if ($0 == 2 && ($1 $2)) return $true
    if ($0 == 1 && ($1)) return $true
    return $false 
  }
  var %c = $1 , %x = 1 , %n = $numtok($1-, 32), %q , %t, %t2, %u, %o, %y , %v = $true , %% = $str(|,2) , %& = $str(&,2)
  while (%x <= %n) {
    %o = $gettok(%c,%x,32) | %t = $gettok(%c,$calc(%x + 1),32) | %t2 = $gettok(%c,$calc(%x + 3),32) | %u = $gettok(%c,$calc(%x + 2),32)
    %q = $iif($istok($+(%%,.,%&),%t,46) || !%t, $eval(%o), $iif($istok($+(%%,.,%&),%u,46) || !%u, $iiff($eval(%o), %t).ev, $iif($eval(%o) %t $eval(%u),$true,$false))))
    %y = $iif(%t == %% || %u == %%,%%, $iif(%t == %& || %u == %&,%&, $iif(%t2 == %%,%%, $iif(%t2 == %&,%&,%y))))
    if (%y == %%) && (%q) return $true
    elseif (%y == %&) && (%q != %v) return $false
    inc %x 4
    if ($istok($+(%%,.,%&),%t,46)) dec %x 2
    elseif ($istok($+(%%,.,%&),%u,46)) dec %x
    %v = %q
  }
  return $iif(%y == %%,$false,$iif(%y == %&, $true, %v))
}

Comments

Sign in to comment.
raccoon   -  Nov 17, 2016

You could make this a lot more powerful with regular expressions, and take care of some of your limitations.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.