コード例 #1
0
def qs2(ls):
    '''
    Now, we take away the return statement for space.  
    '''
    pivot = middle(ls)

    p(ls, iff(len, (qs2, {_ < pivot}) + [pivot] + (qs2, {_ > pivot})))
コード例 #2
0
def count_prob_array(ls,discount=0):

    return p( ls,
              np.array,
              unique_counts,
              _-discount,
              _+L,
              _/np.sum)
コード例 #3
0
def prob_dct(valDct):

    return p( valDct,
              (zip_dct,dct_keys,
                       ep(dct_values,
                          np.array,
                          prob_vec,
                         )),
            )  
コード例 #4
0
def fib1(x):
    '''
    Here, however, we use the iff macro.  So for a conditional fArg cond, and a 
    statement, iff(cond,statement) evaluates as:

              {cond:statement,
               'else':_}

    So, we only apply the sum to x if it's above 1.
    '''
    return p(x, iff(_ > 1, (fib1, _ - 1) + (fib1, _ - 2)))
コード例 #5
0
def fib0(x):
    '''
    Here, we have a switch dict:

              {_ > 1:(fib0,_-1)+(fib0,_-2),
               'else':_})

    This just means, if x > 1, then return the sum of the function called on x-1 and x-2.
    Otherwise, just return x, which will be 1 or 0.  
    '''
    return p(x, {_ > 1: (fib0, _ - 1) + (fib0, _ - 2), 'else': _})
コード例 #6
0
def weighted_count_prob_array(ls=[[1,0.5],[2,0.3]],
                              discount=0):

    return p( ls,
              np.array,
              aggregate_by_key,
              _0,
              row_sum,
              _-discount,
              _+L,
              _/np.sum,
           )
コード例 #7
0
def qs1(ls):
    '''
    Now, we are going to apply the iff macro. So for a conditional fArg cond, and a 
    statement, iff(cond,statement) evaluates as:

              {cond:statement,
               'else':_}
.
    So, if len evaluates as True (nonzero), then we return the statement, otherwise
    we return the empty list.  
    '''
    pivot = middle(ls)

    return p(ls, iff(len, (qs1, {_ < pivot}) + [pivot] + (qs1, {_ > pivot})))
コード例 #8
0
def qs0(ls):
    '''
    middle takes the middle element of the list.  Let's explore the expression:

              {len:(qs0,{_ < pivot}) + [pivot] + (qs0,{_ > pivot}),
               'else':_}

    First, this is a switch dict, meaning that if one of the keys evaluates as true,
    then the corresponding expression also evaluates as true.  So if ls is not
    empty, then len evaluates as true, and we apply the function recursively.  Otherwise
    we return the emply ls.  

              (qs0,{_ < pivot})

    This applies a filter to ls, taking anything below the pivot.  Then we recursively
    apply the function to this list.  
    

              [pivot]

    This constructs a singleton list with pivot.

              (qs0,{_ > pivot})

    This applies a filter to ls, taking anything above the pivot.  Then we recursively
    apply the function to this list.  

              (qs0,{_ < pivot}) + [pivot] + (qs0,{_ > pivot})

    We concatenate these three lists, and return.
    '''
    pivot = middle(ls)

    return p(ls, {
        len: (qs0, {_ < pivot}) + [pivot] + (qs0, {_ > pivot}),
        'else': _
    })
コード例 #9
0
def prob_vec(a):
    
    return p( a,
              _+L,
              _/np.sum)
コード例 #10
0
def fib2(x):
    '''
    Now, we are stripping out the return statement, since the compiler includes it.
    '''
    p(x, iff(_ > 1, (fib2, _ - 1) + (fib2, _ - 2)))