lightness/type-comparator

Change `if` behavior

lightness opened this issue · 3 comments

Seems like if isn't good naming. Any ideas? :)

Here is example how it works: Link

Here is source code: Link

Proposed example:

// Example 1
cmp()
    .if(condition).then(CmpA)
    .else(CmpB);
 
// Example 2
// Suppose comparator called with (a,b)
// If condition1(a) && condition1(b) => return CmpA(a,b).
// If condition1(a) && !condition1(b) => return () => 1
// If !condition1(a) && condition1(b) => return () => -1
// If !condition1(a) && !condition1(b) => 
// If condition2(a) && condition2(b) => return CmpB(a,b).
// If condition2(a) && !condition2(b) => return () => 1
// If !condition2(a) && condition2(b) => return () => -1
// If !condition2(a) && !condition2(b) => 
// => CmpC(a,b)
cmp()
    .if(condition1).then(CmpA)  
    .elif(condition2).then(CmpB)  
    .else(CmpC);

// Example 3
cmp()
    .if(conditon).then(
        cmp().map(mapper1).use(asc)
    )
    .else(
        cmp().map(mapper2).use(desc)
    )

// .asc() should be equal to .use(asc)
// .desc() should be equal to .use(desc)

Second example looks good

If behavior was changed since this commit