functoolz

Functions –

  • apply(*func_and_args, **kwargs) – It simply applies a function and returns the result.




    import toolz
      
      
    def double(n):
        return n + n
      
    print(toolz.functoolz.apply(double, 2))

    
    

    Output –

    4
    
  • complement(func) – As its name suggests, it converts returns the logical complement of the input provided.




    import toolz
      
      
    def is_mulitple_of_5(n):
        return n % 5 == 0
      
    not_multiple_of_5 = toolz.functoolz.complement(is_mulitple_of_5)
      
    print(is_mulitple_of_5(10))
    print(not_multiple_of_5(10))

    
    

    Output –

    True
    False
    
  • compose(*funcs) – It returns a function that applies other functions in sequence. Functions are applied from right to left. If no arguments are provided, the identity function (f(x) = x) is returned.




    import toolz
      
      
    def func(n):
        return n + n
      
    def square(n):
        return n * n
      
    x = toolz.functoolz.compose(func, square)(3)
    print(x)

    
    

    Output –

    18
    
  • compose_left(*funcs) – It returns a function that applies other functions in sequence. Functions are applied from left to right. If no arguments are provided, the identity function (f(x) = x) is returned.




    import toolz
      
      
    def func(n):
        return n + n
      
    def square(n):
        return n * n
      
    x = toolz.functoolz.compose_left(func, square)(3)
    print(x)

    
    

    Output –

    36
    
  • flip – Call the function with the arguments in reverse order.




    import toolz
      
      
    def mod(a, b):
        return a % b
      
    print('7 % 3 :', toolz.functoolz.flip(mod, 3, 7))

    
    

    Output –

    7 % 3 : 1
    
  • identity(x) – Identity function, simply returns x.




    import toolz
      
      
    print(toolz.functoolz.identity(6))

    
    

    Output –

    6
    
  • pipe(data, *funcs) – Pipe a value through a sequence of functions. It is equivalent to compose_left(*funcs)




    import toolz
      
      
    print(toolz.functoolz.pipe(3, double, square))

    
    

    Output –

    36
    
  • thread_first(val, *forms) – Thread value through a sequence of functions/forms.




    import toolz
      
      
    def mod(a, b):
        return a % b
      
    def double(n):
        return n + n
      
    print(toolz.functoolz.thread_first(3, (mod, 2), double))

    
    

    Output –

    2
    
  • thread_last(val, *forms) – Thread value through a sequence of functions/forms.




    import toolz
      
      
    def mod(a, b):
        return a % b
      
    def double(n):
        return n + n
      
    print(toolz.functoolz.thread_last(3, (mod, 2), double))

    
    

    Output –

    4
    

Toolz module in Python

Toolz package provides a set of utility functions for iterators, functions, and dictionaries. These functions extend the standard libraries itertools and functools and borrow heavily from the standard libraries of contemporary functional languages. This package consists of following modules –

  • dicttoolz
  • functoolz
  • itertoolz
  • recipes
  • sandbox

Similar Reads

dicttoolz

Functions –...

functoolz

Functions –...

itertoolz

Functions –...

recipes

Functions –...

sandbox

Functions –...