
    [ck                        d dl Z d dlZd dlZd dlZd dlmZ d dl mZmZ d dlm	Z	 d dl
mZ dZd ZefdZd	 Zd
 Zd Zd Zd Zd1dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z eed          Zd Z efdZ!d Z"d Z#d Z$d Z%d  Z&d! Z'efd"Z(d# Z)d$ Z*d%Z+e+fd&Z,d' Z-d( Z.efd)Z/d* Z0eefd+Z1d, Z2d1d-Z3d. Z4d/ Z5d1d0Z6dS )2    N)partial)filterfalsezip_longest)Sequence)
no_default)$remove
accumulategroupbymerge_sorted
interleaveunique
isiterable
isdistincttakedroptake_nthfirstsecondnthlastgetconcatconcatvmapcatcons	interposefrequenciesreducebyiteratesliding_window	partitionpartition_allcountpluckjointaildifftopkpeekpeeknrandom_samplec                 "    t          | |          S )z Return those items of sequence for which predicate(item) is False

    >>> def iseven(x):
    ...     return x % 2 == 0
    >>> list(remove(iseven, [1, 2, 3, 4]))
    [1, 3]
    )r   )	predicateseqs     /lib/python3.11/site-packages/toolz/itertoolz.pyr   r      s     y#&&&    c              #      K   t          |          }|t          k    r"	 t          |          }n# t          $ r Y dS w xY w|}|V  |D ]} | ||          }|V  dS )a   Repeatedly apply binary function to a sequence, accumulating results

    >>> from operator import add, mul
    >>> list(accumulate(add, [1, 2, 3, 4, 5]))
    [1, 3, 6, 10, 15]
    >>> list(accumulate(mul, [1, 2, 3, 4, 5]))
    [1, 2, 6, 24, 120]

    Accumulate is similar to ``reduce`` and is good for making functions like
    cumulative sum:

    >>> from functools import partial, reduce
    >>> sum    = partial(reduce, add)
    >>> cumsum = partial(accumulate, add)

    Accumulate also takes an optional argument that will be used as the first
    value. This is similar to reduce.

    >>> list(accumulate(add, [1, 2, 3], -1))
    [-1, 0, 2, 5]
    >>> list(accumulate(add, [], 1))
    [1]

    See Also:
        itertools.accumulate :  In standard itertools for Python 3.2+
    N)iterr   nextStopIteration)binopr.   initialresultelems        r/   r	   r	      s      6 s))C* 	#YYFF 	 	 	FF	 
LLL  vt$$ s   . 
<<c                     t          |           st          |           } t          j        d           }|D ]} | | |                   |           i }|                                D ]\  }}|j        ||<   |S )aV   Group a collection by a key function

    >>> names = ['Alice', 'Bob', 'Charlie', 'Dan', 'Edith', 'Frank']
    >>> groupby(len, names)  # doctest: +SKIP
    {3: ['Bob', 'Dan'], 5: ['Alice', 'Edith', 'Frank'], 7: ['Charlie']}

    >>> iseven = lambda x: x % 2 == 0
    >>> groupby(iseven, [1, 2, 3, 4, 5, 6, 7, 8])  # doctest: +SKIP
    {False: [1, 3, 5, 7], True: [2, 4, 6, 8]}

    Non-callable keys imply grouping on a member.

    >>> groupby('gender', [{'name': 'Alice', 'gender': 'F'},
    ...                    {'name': 'Bob', 'gender': 'M'},
    ...                    {'name': 'Charlie', 'gender': 'M'}]) # doctest:+SKIP
    {'F': [{'gender': 'F', 'name': 'Alice'}],
     'M': [{'gender': 'M', 'name': 'Bob'},
           {'gender': 'M', 'name': 'Charlie'}]}

    Not to be confused with ``itertools.groupby``

    See Also:
        countby
    c                      g j         S N)append r0   r/   <lambda>zgroupby.<locals>.<lambda>b   s    	 r0   )callablegettercollectionsdefaultdictitems__self__)keyr.   ditemrvkvs          r/   r
   r
   G   s    2 C== Skk 1 122A  ##d))T	B		  1
1Ir0   c                     t          |           dk    rt          g           S t          |           dk    rt          | d                   S |                    dd          }|t          |           S t	          | |          S )a   Merge and sort a collection of sorted collections

    This works lazily and only keeps one value from each iterable in memory.

    >>> list(merge_sorted([1, 3, 5], [2, 4, 6]))
    [1, 2, 3, 4, 5, 6]

    >>> ''.join(merge_sorted('abc', 'abc', 'abc'))
    'aaabbbccc'

    The "key" function used to sort the input may be passed as a keyword.

    >>> list(merge_sorted([2, 3], [1, 3], key=lambda x: x // 3))
    [2, 1, 3, 3]
    r      rE   N)lenr2   r   _merge_sorted_binary_merge_sorted_binary_key)seqskwargsrE   s      r/   r   r   k   s|      4yyA~ Bxx	Ta DG}}
**UD
!
!C
 3#D)))'c222r0   c              #     K   t          |           dz  }| d |         }t          |          dk    rt          |d                   }nt          |          }| |d          }t          |          dk    rt          |d                   }nt          |          }	 t          |          }n# t          $ r |D ]}|V  Y d S w xY w|D ](}||k     r|V  |D ]}||k     r|V  |V   n n$|V  )|V  |D ]}|V  d S |V  |D ]}|V  d S N   rL   r   )rM   r2   rN   r3   r4   )rP   midL1seq1L2seq2val2val1s           r/   rN   rN      s     
d))q.C	dsdB
2ww!| (BqE{{#B''	cddB
2ww!| (BqE{{#B''Dzz    	 	DJJJJ
   $; 	JJJ  $; JJJJJJJE  JJJJ


 	 	DJJJJ
JJJ  



 s   B* *C Cc              #   `  K   t          |           dz  }| d |         }t          |          dk    rt          |d                   }nt          ||          }| |d          }t          |          dk    rt          |d                   }nt          ||          }	 t          |          }n# t          $ r |D ]}|V  Y d S w xY w ||          }	|D ]>} ||          }
|	|
k     r'|V  |D ]} ||          }	|	|
k     r|V  |V   n n:|V  ?|V  |D ]}|V  d S |V  |D ]}|V  d S rS   )rM   r2   rO   r3   r4   )rP   rE   rU   rV   rW   rX   rY   rZ   r[   key2key1s              r/   rO   rO      s     
d))q.C	dsdB
2ww!| 1BqE{{'C00	cddB
2ww!| 1BqE{{'C00Dzz    	 	DJJJJ 3t99D  s4yy$; 	JJJ  s4yy$; JJJJJJJE  JJJJ


 	 	DJJJJ
JJJ  



 s   B, ,CCc              #   ,  K   t          j        t          t          |                     }	 	 |D ]}t	          |          V  dS # t
          $ rD t          t          j        |          }t          j        t          j	        ||                    }Y nw xY wk)a   Interleave a sequence of sequences

    >>> list(interleave([[1, 2], [3, 4]]))
    [1, 3, 2, 4]

    >>> ''.join(interleave(('ABC', 'XY')))
    'AXBYC'

    Both the individual sequences and the sequence of sequences may be infinite

    Returns a lazy iterator
    TN)
	itertoolscyclemapr2   r3   r4   r   operatoris_not	takewhile)rP   itersitrr-   s       r/   r   r      s       OCdOO,,EK	K    3iiF 	K 	K 	K55IOI$7	5$I$IJJEEE	KKs   A ABBc              #      K   t                      }|j        }|| D ]}||vr ||           |V  dS | D ] } ||          }||vr ||           |V  !dS )a   Return only unique elements of a sequence

    >>> tuple(unique((1, 2, 3)))
    (1, 2, 3)
    >>> tuple(unique((1, 2, 1, 3)))
    (1, 2, 3)

    Uniqueness can be defined by key keyword

    >>> tuple(unique(['cat', 'mouse', 'dog', 'hen'], key=len))
    ('cat', 'mouse')
    N)setadd)r.   rE   seenseen_addrG   vals         r/   r   r      s       55DxH
 
 	 	D4 


	 	
  	 	D#d))C$ 


		 	r0   c                 H    	 t          |            dS # t          $ r Y dS w xY w)z Is x iterable?

    >>> isiterable([1, 2, 3])
    True
    >>> isiterable('abc')
    True
    >>> isiterable(5)
    False
    TF)r2   	TypeErrorxs    r/   r   r     s9    Qt   uus    
!!c                     t          |           | u r.t                      }|j        }| D ]}||v r dS  ||           dS t          |           t          t          |                     k    S )z All values in sequence are distinct

    >>> isdistinct([1, 2, 3])
    True
    >>> isdistinct([1, 2, 1])
    False

    >>> isdistinct("Hello")
    False
    >>> isdistinct("World")
    True
    FT)r2   ri   rj   rM   )r.   rk   rl   rG   s       r/   r   r   %  sz     CyyC 	)uu8 	 	Dt| uuHTNNNNt3xx3s3xx==((r0   c                 ,    t          j        ||           S )z The first n elements of a sequence

    >>> list(take(2, [10, 20, 30, 40, 50]))
    [10, 20]

    See Also:
        drop
        tail
    r`   islicenr.   s     r/   r   r   >  s     C###r0   c                     	 ||  d         S # t           t          f$ r% t          t          j        ||                     cY S w xY w)z The last n elements of a sequence

    >>> tail(2, [10, 20, 30, 40, 50])
    [40, 50]

    See Also:
        drop
        take
    N)ro   KeyErrortuplerA   dequerv   s     r/   r&   r&   K  sW    0A233xx  0 0 0[&sA../////0s   
 3AAc                 .    t          j        || d          S )z The sequence following the first n elements

    >>> list(drop(2, [10, 20, 30, 40, 50]))
    [30, 40, 50]

    See Also:
        take
        tail
    Nrt   rv   s     r/   r   r   [  s     CD)))r0   c                 0    t          j        |dd|           S )z] Every nth item in seq

    >>> list(take_nth(2, [10, 20, 30, 40, 50]))
    [10, 30, 50]
    r   Nrt   rv   s     r/   r   r   h  s     CD!,,,r0   c                 :    t          t          |                     S )zC The first element in a sequence

    >>> first('ABC')
    'A'
    )r3   r2   r.   s    r/   r   r   q  s     S		??r0   c                 \    t          |           } t          |            t          |           S )zE The second element in a sequence

    >>> second('ABC')
    'B'
    )r2   r3   r   s    r/   r   r   z  s&     s))CIII99r0   c                     t          |t          t          t          f          r||          S t	          t          j        || d                    S )zB The nth element in a sequence

    >>> nth(1, 'ABC')
    'B'
    N)
isinstancerz   listr   r3   r`   ru   rv   s     r/   r   r     sC     #tX.// 41vI$S!T22333r0   c                 .    t          d|           d         S )zA The last element in a sequence

    >>> last('ABC')
    'C'
    rL   r   )r&   r   s    r/   r   r     s     3<<?r0   rL   c                 F    	 ||          S # t           t          f$ r |cY S w xY wr;   )ry   
IndexErrorindr.   defaults      r/   _getr     s9    3xj!   s   
   c                    	 |          S # t           $ r t          | t                    rkt          k    rBt	          |           dk    r t          j        |            cY S | r| d                  fcY S Y dS t          fd| D                       cY S t          k    rcY S  t          t          f$ r t          k    r cY S w xY w)a   Get element in a sequence or dict

    Provides standard indexing

    >>> get(1, 'ABC')       # Same as 'ABC'[1]
    'B'

    Pass a list to get multiple values

    >>> get([1, 2], 'ABC')  # ('ABC'[1], 'ABC'[2])
    ('B', 'C')

    Works on any value that supports indexing/getitem
    For example here we see that it works with dictionaries

    >>> phonebook = {'Alice':  '555-1234',
    ...              'Bob':    '555-5678',
    ...              'Charlie':'555-9999'}
    >>> get('Alice', phonebook)
    '555-1234'

    >>> get(['Alice', 'Bob'], phonebook)
    ('555-1234', '555-5678')

    Provide a default for missing values

    >>> get(['Alice', 'Dennis'], phonebook, None)
    ('555-1234', None)

    See Also:
        pluck
    rL   r   r=   c              3   :   K   | ]}t          |          V  d S r;   r   ).0ir   r.   s     r/   	<genexpr>zget.<locals>.<genexpr>  s/      @@qT!S'22@@@@@@r0   )
ro   r   r   r   rM   rc   
itemgetterrz   ry   r   r   s    ``r/   r   r     s   B3x   c4   	*$ As88a< 48.4S99999 s1v;<'''22@@@@@C@@@@@@@@
" 	NNNj!   j  	NNN	s(    AC"C8CC%CCc                 @    t           j                            |           S )a{   Concatenate zero or more iterables, any of which may be infinite.

    An infinite sequence will prevent the rest of the arguments from
    being included.

    We use chain.from_iterable rather than ``chain(*seqs)`` so that seqs
    can be a generator.

    >>> list(concat([[], [1], [2, 3]]))
    [1, 2, 3]

    See also:
        itertools.chain.from_iterable  equivalent
    )r`   chainfrom_iterablerP   s    r/   r   r     s     ?((...r0   c                       t          |           S )z Variadic version of concat

    >>> list(concatv([], ["a"], ["b", "c"]))
    ['a', 'b', 'c']

    See also:
        itertools.chain
    )r   r   s    r/   r   r     s     $<<r0   c                 <    t          t          | |                    S )z Apply func to each sequence in seqs, concatenating results.

    >>> list(mapcat(lambda s: [c.upper() for c in s],
    ...             [["a", "b"], ["c", "d", "e"]]))
    ['A', 'B', 'C', 'D', 'E']
    )r   rb   )funcrP   s     r/   r   r     s     #dD//"""r0   c                 .    t          j        | g|          S )zk Add el to beginning of (possibly infinite) sequence seq.

    >>> list(cons(1, [2, 3]))
    [1, 2, 3]
    )r`   r   )elr.   s     r/   r   r     s     ?B4%%%r0   c                     t          t          t          j        |           |                    }t	          |           |S )z} Introduce element between each pair of elements in seq

    >>> list(interpose("a", [1, 2, 3]))
    [1, 'a', 2, 'a', 3]
    )r   zipr`   repeatr3   )r   r.   inposeds      r/   r   r     s6     S)"--s3344GMMMNr0   c                 |    t          j        t                    }| D ]}||xx         dz  cc<   t          |          S )z Find number of occurrences of each value in seq

    >>> frequencies(['cat', 'cat', 'ox', 'pig', 'pig', 'cat'])  #doctest: +SKIP
    {'cat': 3, 'ox': 1, 'pig': 2}

    See Also:
        countby
        groupby
    rL   )rA   rB   intdict)r.   rF   rG   s      r/   r   r     sE     	$$A  	$177Nr0   c                 
   |t           k    }|st          |          s|fd}t          |           st          |           } i }|D ];} | |          }||vr|r|||<    |            ||<    |||         |          ||<   <|S )a   Perform a simultaneous groupby and reduction

    The computation:

    >>> result = reduceby(key, binop, seq, init)      # doctest: +SKIP

    is equivalent to the following:

    >>> def reduction(group):                           # doctest: +SKIP
    ...     return reduce(binop, group, init)           # doctest: +SKIP

    >>> groups = groupby(key, seq)                    # doctest: +SKIP
    >>> result = valmap(reduction, groups)              # doctest: +SKIP

    But the former does not build the intermediate groups, allowing it to
    operate in much less space.  This makes it suitable for larger datasets
    that do not fit comfortably in memory

    The ``init`` keyword argument is the default initialization of the
    reduction.  This can be either a constant value like ``0`` or a callable
    like ``lambda : 0`` as might be used in ``defaultdict``.

    Simple Examples
    ---------------

    >>> from operator import add, mul
    >>> iseven = lambda x: x % 2 == 0

    >>> data = [1, 2, 3, 4, 5]

    >>> reduceby(iseven, add, data)  # doctest: +SKIP
    {False: 9, True: 6}

    >>> reduceby(iseven, mul, data)  # doctest: +SKIP
    {False: 15, True: 8}

    Complex Example
    ---------------

    >>> projects = [{'name': 'build roads', 'state': 'CA', 'cost': 1000000},
    ...             {'name': 'fight crime', 'state': 'IL', 'cost': 100000},
    ...             {'name': 'help farmers', 'state': 'IL', 'cost': 2000000},
    ...             {'name': 'help farmers', 'state': 'CA', 'cost': 200000}]

    >>> reduceby('state',                        # doctest: +SKIP
    ...          lambda acc, x: acc + x['cost'],
    ...          projects, 0)
    {'CA': 1200000, 'IL': 2100000}

    Example Using ``init``
    ----------------------

    >>> def set_add(s, i):
    ...     s.add(i)
    ...     return s

    >>> reduceby(iseven, set_add, [1, 2, 3, 4, 1, 2, 3], set)  # doctest: +SKIP
    {True:  set([2, 4]),
     False: set([1, 3])}
    c                       S r;   r=   )_inits   r/   r>   zreduceby.<locals>.<lambda>i  s    u r0   )r   r?   r@   )	rE   r5   r.   initis_no_defaultrF   rG   rI   r   s	           @r/   r   r   )  s    z J&M $ }}}C== Skk
A ! !CIIA: 	 !tvv!uQqT4  !Hr0   c              #   (   K   	 |V   | |          })a   Repeatedly apply a function func onto an original input

    Yields x, then func(x), then func(func(x)), then func(func(func(x))), etc..

    >>> def inc(x):  return x + 1
    >>> counter = iterate(inc, 0)
    >>> next(counter)
    0
    >>> next(counter)
    1
    >>> next(counter)
    2

    >>> double = lambda x: x * 2
    >>> powers_of_two = iterate(double, 1)
    >>> next(powers_of_two)
    1
    >>> next(powers_of_two)
    2
    >>> next(powers_of_two)
    4
    >>> next(powers_of_two)
    8
    r=   )r   rq   s     r/   r   r   y  s&      2DGGr0   c           	      h    t          d t          t          j        ||                     D              S )ah   A sequence of overlapping subsequences

    >>> list(sliding_window(2, [1, 2, 3, 4]))
    [(1, 2), (2, 3), (3, 4)]

    This function creates a sliding window suitable for transformations like
    sliding means / smoothing

    >>> mean = lambda seq: float(sum(seq)) / len(seq)
    >>> list(map(mean, sliding_window(2, [1, 2, 3, 4])))
    [1.5, 2.5, 3.5]
    c              3   p   K   | ]1\  }}t          j        t          j        ||          d           p|V  2dS )r   N)rA   r{   r`   ru   )r   r   its      r/   r   z!sliding_window.<locals>.<genexpr>  sY       > >1b "9#3B#:#:A>>D" > > > > > >r0   )r   	enumerater`   teerv   s     r/   r    r      sA      > >%imC&;&;<<> > > ? ?r0   __no__pad__c                 d    t          |          g| z  }|t          u r	t          | S t          |d|iS )a   Partition sequence into tuples of length n

    >>> list(partition(2, [1, 2, 3, 4]))
    [(1, 2), (3, 4)]

    If the length of ``seq`` is not evenly divisible by ``n``, the final tuple
    is dropped if ``pad`` is not specified, or filled to length ``n`` by pad:

    >>> list(partition(2, [1, 2, 3, 4, 5]))
    [(1, 2), (3, 4)]

    >>> list(partition(2, [1, 2, 3, 4, 5], pad=None))
    [(1, 2), (3, 4), (5, None)]

    See Also:
        partition_all
    	fillvalue)r2   no_padr   r   )rw   r.   padargss       r/   r!   r!     s>    $ II;?D
f} 1DzD0C000r0   c              #     K   t          |          g| z  }t          |dt          i}	 t          |          }n# t          $ r Y dS w xY w|D ]}|V  |}	|d         t          u rk	 |dt          |          | z           V  dS # t          $ r? d| }}||k     r%||z   dz  }||         t          u r|}n|dz   }||k     %|d|         V  Y dS w xY w|V  dS )a;   Partition all elements of sequence into tuples of length at most n

    The final tuple may be shorter to accommodate extra elements.

    >>> list(partition_all(2, [1, 2, 3, 4]))
    [(1, 2), (3, 4)]

    >>> list(partition_all(2, [1, 2, 3, 4, 5]))
    [(1, 2), (3, 4), (5,)]

    See Also:
        partition
    r   Nr   rT   rL   )r2   r   r   r3   r4   rM   ro   )	rw   r.   r   r   prevrG   lohirU   s	            r/   r"   r"     sQ      II;?D	d	-f	-	-BBxx     


Bx6 	 }C1}%%%%%% 	 	 	
 Br' !Bw1n9& !BBqB r' ! ss)OOOOOO	 




s"   8 
AA$B AC
Cc                 r    t          | d          rt          |           S t          d | D                       S )z Count the number of items in seq

    Like the builtin ``len`` but works on lazy sequences.

    Not to be confused with ``itertools.count``

    See also:
        len
    __len__c              3      K   | ]}d V  dS )rL   Nr=   )r   r   s     r/   r   zcount.<locals>.<genexpr>  s"      Qqr0   )hasattrrM   sumr   s    r/   r#   r#     s>     sI 3xx#r0   c                      t           k    rt                     }t          ||          S t           t                    r fd|D             S  fd|D             S )a   plucks an element or several elements from each item in a sequence.

    ``pluck`` maps ``itertoolz.get`` over a sequence and returns one or more
    elements of each item in the sequence.

    This is equivalent to running `map(curried.get(ind), seqs)`

    ``ind`` can be either a single string/index or a list of strings/indices.
    ``seqs`` should be sequence containing sequences or dicts.

    e.g.

    >>> data = [{'id': 1, 'name': 'Cheese'}, {'id': 2, 'name': 'Pies'}]
    >>> list(pluck('name', data))
    ['Cheese', 'Pies']
    >>> list(pluck([0, 1], [[1, 2, 3], [4, 5, 7]]))
    [(1, 2), (4, 5)]

    See Also:
        get
        map
    c              3   R   K   | ] t          fd D                       V  !dS )c              3   :   K   | ]}t          |          V  d S r;   r   )r   rG   r   r.   s     r/   r   z"pluck.<locals>.<genexpr>.<genexpr>  s/      ??4d4g..??????r0   N)rz   r   r.   r   r   s    @r/   r   zpluck.<locals>.<genexpr>  sX       ! ! ?????3????? ! ! ! ! ! !r0   c              3   :   K   | ]}t          |          V  d S r;   r   r   s     r/   r   zpluck.<locals>.<genexpr>  s/      44Dc7##444444r0   )r   r@   rb   r   r   )r   rP   r   r   s   ` ` r/   r$   r$      s    . * !Skk3~~	C		 !! ! ! ! !! ! ! 	!44444t4444r0   c                      t           t                    r3t                     dk    r d           fdS  rt          j          S d S t          j                   S )NrL   r   c                     |          fS r;   r=   )rq   indexs    r/   r>   zgetter.<locals>.<lambda>$  s    ah[ r0   c                     dS )Nr=   r=   rp   s    r/   r>   zgetter.<locals>.<lambda>(  s    R r0   )r   r   rM   rc   r   )r   s   `r/   r@   r@      sn    % 	*u::? 	 !HE((((( 	 &..<"5)))r0   c              #      K   t          |           st          |           } t          |          st          |          }t          | |          }|t          k    r2|t          k    r'|D ]"} ||          }||v r||         D ]}	|	|fV  	#dS |t          k    r9|t          k    r.|D ])} ||          }||v r||         D ]}	|	|fV  	#||fV  *dS |t          k    rt	                      }
|
j        }|t          k    r1|D ]-} ||          } ||           ||v r||         D ]}	|	|fV  	.n7|D ]4} ||          } ||           ||v r||         D ]}	|	|fV  	.||fV  5|                                D ]\  }}||
vr|D ]}||fV  	dS dS )a=   Join two sequences on common attributes

    This is a semi-streaming operation.  The LEFT sequence is fully evaluated
    and placed into memory.  The RIGHT sequence is evaluated lazily and so can
    be arbitrarily large.
    (Note: If right_default is defined, then unique keys of rightseq
        will also be stored in memory.)

    >>> friends = [('Alice', 'Edith'),
    ...            ('Alice', 'Zhao'),
    ...            ('Edith', 'Alice'),
    ...            ('Zhao', 'Alice'),
    ...            ('Zhao', 'Edith')]

    >>> cities = [('Alice', 'NYC'),
    ...           ('Alice', 'Chicago'),
    ...           ('Dan', 'Syndey'),
    ...           ('Edith', 'Paris'),
    ...           ('Edith', 'Berlin'),
    ...           ('Zhao', 'Shanghai')]

    >>> # Vacation opportunities
    >>> # In what cities do people have friends?
    >>> result = join(second, friends,
    ...               first, cities)
    >>> for ((a, b), (c, d)) in sorted(unique(result)):
    ...     print((a, d))
    ('Alice', 'Berlin')
    ('Alice', 'Paris')
    ('Alice', 'Shanghai')
    ('Edith', 'Chicago')
    ('Edith', 'NYC')
    ('Zhao', 'Chicago')
    ('Zhao', 'NYC')
    ('Zhao', 'Berlin')
    ('Zhao', 'Paris')

    Specify outer joins with keyword arguments ``left_default`` and/or
    ``right_default``.  Here is a full outer join in which unmatched elements
    are paired with None.

    >>> identity = lambda x: x
    >>> list(join(identity, [1, 2, 3],
    ...           identity, [2, 3, 4],
    ...           left_default=None, right_default=None))
    [(2, 2), (3, 3), (None, 4), (1, None)]

    Usually the key arguments are callables to be applied to the sequences.  If
    the keys are not obviously callable then it is assumed that indexing was
    intended, e.g. the following is a legal change.
    The join is implemented as a hash join and the keys of leftseq must be
    hashable. Additionally, if right_default is defined, then keys of rightseq
    must also be hashable.

    >>> # result = join(second, friends, first, cities)
    >>> result = join(1, friends, 0, cities)  # doctest: +SKIP
    N)r?   r@   r
   r   ri   rj   rC   )leftkeyleftseqrightkeyrightseqleft_defaultright_defaultrF   rG   rE   
left_match	seen_keysrk   matchesmatchs                 r/   r%   r%   -  s     v G "//H $(##!!Az! *1mz&A *1 	- 	-D(4..Cax -"#C& - -J%t,,,,,		- 	-
 
	# #1(C #1 	+ 	+D(4..Cax +"#C& - -J%t,,,,,- $T*****	+ 	+ 
*	$ 1EE	}:% 	/  1 1htnnS			!8 1&'f 1 1
)4000001 ! / /htnnS			!8 /&'f 1 1
)4000001 (.....GGII 	1 	1LC)# 1$ 1 1E -0000051 1.	1 	1r0   c               /   H  K   t          |           }|dk    r2t          | d         t                    r| d         } t          |           }|dk     rt          d          |                    dt
                    }|t
          k    r
t          |  }nt          | d|i}|                    dd          }|*|D ]%}|                    |d                   |k    r|V  &dS |D ]B}t          t          ||                    }|                    |d                   |k    r|V  CdS )	a   Return those items that differ between sequences

    >>> list(diff([1, 2, 3], [1, 2, 10, 100]))
    [(3, 10)]

    Shorter sequences may be padded with a ``default`` value:

    >>> list(diff([1, 2, 3], [1, 2, 10, 100], default=None))
    [(3, 10), (None, 100)]

    A ``key`` function may also be applied to each item to use during
    comparisons:

    >>> list(diff(['apples', 'bananas'], ['Apples', 'Oranges'], key=str.lower))
    [('bananas', 'Oranges')]
    rL   r   rT   z(Too few sequences given (min 2 required)r   r   rE   N)rM   r   r   ro   r   r   r   r   r#   rz   rb   )rP   rQ   Nr   rf   rE   rC   valss           r/   r'   r'     sS     " 	D		AAv *T!Wd++ AwII1u DBCCCjjJ//G* 6T
T5W55
**UD
!
!C
  	 	E{{58$$) 	 	  	 	ES%))Dzz$q'""a' 	 	r0   c                     |t          |          st          |          }t          t          j        | ||                    S )a:   Find the k largest elements of a sequence

    Operates lazily in ``n*log(k)`` time

    >>> topk(2, [1, 100, 10, 1000])
    (1000, 100)

    Use a key function to change sorted order

    >>> topk(2, ['Alice', 'Bob', 'Charlie', 'Dan'], key=len)
    ('Charlie', 'Alice')

    See also:
        heapq.nlargest
    N)rE   )r?   r@   rz   heapqnlargest)rI   r.   rE   s      r/   r(   r(     sC       x}} Skk3C000111r0   c                 n    t          |           }t          |          }|t          j        |f|          fS )a!   Retrieve the next element of a sequence

    Returns the first element and an iterable equivalent to the original
    sequence, still having the element retrieved.

    >>> seq = [0, 1, 2, 3, 4]
    >>> first, seq = peek(seq)
    >>> first
    0
    >>> list(seq)
    [0, 1, 2, 3, 4]
    )r2   r3   r`   r   )r.   iteratorrG   s      r/   r)   r)     s3     CyyH>>D$(3333r0   c                     t          |          }t          t          | |                    }|t          j        t          |          |          fS )a;   Retrieve the next n elements of a sequence

    Returns a tuple of the first n elements and an iterable equivalent
    to the original, still having the elements retrieved.

    >>> seq = [0, 1, 2, 3, 4]
    >>> first_two, seq = peekn(2, seq)
    >>> first_two
    (0, 1)
    >>> list(seq)
    [0, 1, 2, 3, 4]
    )r2   rz   r   r`   r   )rw   r.   r   peekeds       r/   r*   r*     sC     CyyH48$$%%F9?4<<::::r0   c                 p     t          d          sddlm}  |          t           fd|          S )a   Return elements from a sequence with probability of prob

    Returns a lazy iterator of random items from seq.

    ``random_sample`` considers each item independently and without
    replacement. See below how the first time it returned 13 items and the
    next time it returned 6 items.

    >>> seq = list(range(100))
    >>> list(random_sample(0.1, seq)) # doctest: +SKIP
    [6, 9, 19, 35, 45, 50, 58, 62, 68, 72, 78, 86, 95]
    >>> list(random_sample(0.1, seq)) # doctest: +SKIP
    [6, 44, 54, 61, 69, 94]

    Providing an integer seed for ``random_state`` will result in
    deterministic sampling. Given the same seed it will return the same sample
    every time.

    >>> list(random_sample(0.1, seq, random_state=2016))
    [7, 9, 19, 25, 30, 32, 34, 48, 59, 60, 81, 98]
    >>> list(random_sample(0.1, seq, random_state=2016))
    [7, 9, 19, 25, 30, 32, 34, 48, 59, 60, 81, 98]

    ``random_state`` can also be any object with a method ``random`` that
    returns floats between 0.0 and 1.0 (exclusive).

    >>> from random import Random
    >>> randobj = Random(2016)
    >>> list(random_sample(0.1, seq, random_state=randobj))
    [7, 9, 19, 25, 30, 32, 34, 48, 59, 60, 81, 98]
    randomr   )Randomc                 4                                     k     S r;   )r   )_probrandom_states    r/   r>   zrandom_sample.<locals>.<lambda>!  s    L//11D8 r0   )r   r   r   filter)r   r.   r   r   s   ` ` r/   r+   r+     sW    @ <** ,!!!!!!vl++88888#>>>r0   r;   )7r`   r   rA   rc   	functoolsr   r   r   collections.abcr   toolz.utilsr   __all__r   r	   r
   r   rN   rO   r   r   r   r   r   r&   r   r   r   r   r   r   restr   r   r   r   r   r   r   r   r   r   r    r   r!   r"   r#   r$   r@   r%   r'   r(   r)   r*   r+   r=   r0   r/   <module>r      sc                   . . . . . . . . $ $ $ $ $ $ " " " " " "M' ' ' $. & & & &R! ! !H3 3 38( ( (V+ + +\K K K0   8  ") ) )2
$ 
$ 
$0 0 0 
* 
* 
*- - -    	4 	4 	4   wtQ   % 6 6 6 6r/ / /$	 	 	# # #& & &      $. M M M M`  <? ? ?" 
 ! 1 1 1 12* * *Z   ( 5 5 5 5@
* 
* 
* !
l1 l1 l1 l1^% % %P2 2 2 2*4 4 4$; ; ;$$? $? $? $? $? $?r0   