1. Python / Говнокод #27354

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    """
    This module provides a function merge_sort, which is one of the simplest and the
    most robust sorting algorithms, being relatively fast, having (n*log(n))
    complexity. Also, this module counts the number of inversions in a given array.
    Usage: import this module and use the function merge_sort. The first element in
    a returned tuple shall be a sorted array, while the second one will be a number
    of inversions in the array.
    
    Copyright (C) 2021 Sergay Gayorgyevich.
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
    """
    
    import argparse
    
    def merge_sort(array : list) -> (list, int):
        """
        MergeSort algorithm with additional inversion counting.
        input: array.
        output: sorted array and number of inversions in it.
        """
    
        if len(array) <= 1:
            return (array, 0)
        elif len(array) == 2:
            if array[0] > array[1]:
                array[0], array[1] = array[1], array[0]
                return (array, 1)
            return (array, 0)
    
        mid = len(array) // 2
        left, left_inversions = merge_sort(array[:mid])
        right, right_inversions = merge_sort(array[mid:])
    
        merged, split_inversions = _merge(left, right)
        return (merged, right_inversions + left_inversions + split_inversions)
    
    def _merge(left_array : list, right_array : list) -> (list, int):
        """
        This function isn't supposed to be called, it's an inner function of the
        module, and not a part of its API! 
    
        The purpose of this function is to merge two arrays. Due to the nature of
        the MergeSort algorithm, it operates two arrays, both of which are sorted.
        input: two sotrted arrays.
        output: sorted array, consisting of elements of both operated arrays.
        """
    
        resulting_array = list()
        inversion_count = 0
        c_len = len(left_array) + len(right_array)
    
        for i in range(c_len):
            if (len(left_array) != 0) and (len(right_array) != 0):
                if left_array[0] > right_array[0]:
                    inversion_count += len(left_array)
                    resulting_array.append(right_array.pop(0))
                else:
                    resulting_array.append(left_array.pop(0))
            elif len(left_array) == 0:
                resulting_array.append(right_array.pop(0))
            elif len(right_array) == 0:
                resulting_array.append(left_array.pop(0))
    
        return (resulting_array[:], inversion_count)
    
    # For testing purposes only! Do not use in production!
    if __name__ == '__main__':
        DESC = "Sort an array and print an amount of inversions it has." # Description for argparse.
        argparser = argparse.ArgumentParser(description = DESC)
        argparser.add_argument("elements", type = int, nargs = '+', help = "A list to be sorted.")
        args = argparser.parse_args()
        print(merge_sort(args.elements))

    Изучаю алгоритм "Сортировка Слиянием".

    KoWe4Ka_l7porpaMMep, 12 Апреля 2021

    Комментарии (159)
  2. Куча / Говнокод #27353

    +1

    1. 1
    Питушня #15

    #1: https://govnokod.ru/26692 https://govnokod.xyz/_26692
    #2: https://govnokod.ru/26891 https://govnokod.xyz/_26891
    #3: https://govnokod.ru/26893 https://govnokod.xyz/_26893
    #4: https://govnokod.ru/26935 https://govnokod.xyz/_26935
    #5: (vanished) https://govnokod.xyz/_26954
    #6: (vanished) https://govnokod.xyz/_26956
    #7: https://govnokod.ru/26964 https://govnokod.xyz/_26964
    #8: https://govnokod.ru/26966 https://govnokod.xyz/_26966
    #9: https://govnokod.ru/27017 https://govnokod.xyz/_27017
    #10: https://govnokod.ru/27045 https://govnokod.xyz/_27045
    #11: https://govnokod.ru/27058 https://govnokod.xyz/_27058
    #12: https://govnokod.ru/27182 https://govnokod.xyz/_27182
    #13: https://govnokod.ru/27260 https://govnokod.xyz/_27260
    #14: https://govnokod.ru/27343 https://govnokod.xyz/_27343

    nepeKamHblu_nemyx, 11 Апреля 2021

    Комментарии (2532)
  3. Куча / Говнокод #27352

    +1

    1. 1
    11 апреля - всемирный день анимешника

    Всех поздравляем!!!

    OCETuHCKuu_nemyx, 11 Апреля 2021

    Комментарии (27)
  4. Python / Говнокод #27351

    +3

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    """
    A module for printing funny frames.
    Copyright (C) 2021 Ingostnus.
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
    """
    
    from math import sqrt
    
    _str = "sample"                  # String to be turned into a funny frame.
    _sep = ' '                       # Separator, i.e. a string to be inserted between each letter of the _str.
    _vertical_separator_mode = 'F'   # Determines whether separator should be printed between each row.
                                     # ... 'F' --> off. 'T' --> on.
    
    def get_user_input() -> None:
        """
        This function lets user input desired values and override the defaults.
        """
        global _str; _str = str(input("String: "))
        global _sep; _sep = str(input("Character: "))
        global _vertical_separator_mode; _vertical_separator_mode = str((input("VSM (T/F):")))
        if _vertical_separator_mode == 'T':
            print("VMS is ON!!!")
    
    def print_frame(printer = print) -> list:
        """
        This function is designed for printing a frame. Custom printer function
        may be supplied to process the output in a specific way.
        """
    
        buffer_len = (len(_str) + len(_sep) * (len(_str) - 1))**2
    
        # First line.
        printer(''.join([_str[i] + _sep if i < (len(_str) - 1) else _str[-1] for i in range(len(_str))]))
        
        # Second -- pre last lines.
        empty_space = ' ' * (int(sqrt(buffer_len)) - 2)
        for i in range(1, len(_str) - 1):
            # If vertical separator mode is toggled, print vertical separator.
            if _vertical_separator_mode == 'T':
                printer(_sep + empty_space + _sep)
            printer(_str[i] + empty_space + _str[-(i + 1)])
        
        # Last line.
        printer(''.join([_str[-(i + 1)] + _sep if i < (len(_str) - 1) else _str[0] for i in range(len(_str))]))
    
    # To give the best perfomance and flexibility, this module should be used as
    # an imported library. Though, its basic functionality can be used even if
    # it's executed directly.
    if __name__ == '__main__':
        get_user_input()
        print_frame()

    Переписала код https://govnokod.ru/27348 на питон, добавив чуть-чуть улучшений и немноже4ко документацци.

    KoWe4Ka_l7porpaMMep, 11 Апреля 2021

    Комментарии (26)
  5. JavaScript / Говнокод #27350

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    type User = {
        status: 'lamer' | 'junior' | 'govnokoder';
        login: string;
        iq: number;
    }
    
    type ChangeListener<T, K extends keyof T> = {
        name: `${K & string}_Listener`;
        on(newValue: T[K])
    }
    
    const UserIqListener: ChangeListener<User, 'iq'> = {
        name: "iq_Listener", //ничто другое не скомпилируется
        on(event: number) { //понятно, что string тут не скомпилируется
        }
    }
    const UserStatusListener: ChangeListener<User, 'status'> = {
        name: "status_Listener",
        on(newValue: User["status"]) {
            switch (newValue) {
                case "govnokoder": { //понятно, что неверный тип тут не скомпилируется
    
                }
            }
        }
    
    }

    Почему у нас нет "TypeScript"?

    MAKAKA, 11 Апреля 2021

    Комментарии (38)
  6. C++ / Говнокод #27349

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    // https://govnokod.ru/27340#comment621647
    
    #include <iostream>
    #include <cstdlib>
    
    #define SPLICE(a,b) SPLICE_1(a,b)
    #define SPLICE_1(a,b) SPLICE_2(a,b)
    #define SPLICE_2(a,b) a##b
    
    
    #define PP_ARG_N( \
              _1,  _2,  _3,  _4,  _5,  _6,  _7,  _8,  _9, _10, \
             _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
             _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
             _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
             _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
             _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
             _61, _62, _63, N, ...) N
    
    /* Note 63 is removed */
    #define PP_RSEQ_N()                                        \
             62, 61, 60,                                       \
             59, 58, 57, 56, 55, 54, 53, 52, 51, 50,           \
             49, 48, 47, 46, 45, 44, 43, 42, 41, 40,           \
             39, 38, 37, 36, 35, 34, 33, 32, 31, 30,           \
             29, 28, 27, 26, 25, 24, 23, 22, 21, 20,           \
             19, 18, 17, 16, 15, 14, 13, 12, 11, 10,           \
              9,  8,  7,  6,  5,  4,  3,  2,  1,  0
    
    #define PP_NARG_(...)    PP_ARG_N(__VA_ARGS__)    
    
    /* Note dummy first argument _ and ##__VA_ARGS__ instead of __VA_ARGS__ */
    // note: using __VA_OPT__ shit instead ##__VA_ARGS__
    #define PP_NARG(...)     PP_NARG_(_ __VA_OPT__(,) __VA_ARGS__, PP_RSEQ_N())
    
    #define MK_VAR_T(type, name) \
    typeof(type) name
    
    #define TYPE_ADD_0()
    
    #define TYPE_ADD_1(VAR) \
      MK_VAR_T VAR;
    #define TYPE_ADD_2(VAR, ...) \
      MK_VAR_T VAR; TYPE_ADD_1(__VA_ARGS__)
    #define TYPE_ADD_3(VAR, ...) \
      MK_VAR_T VAR; TYPE_ADD_2(__VA_ARGS__)
    #define TYPE_ADD_4(VAR, ...) \
      MK_VAR_T VAR; TYPE_ADD_3(__VA_ARGS__)
    #define TYPE_ADD_5(VAR, ...) \
      MK_VAR_T VAR; TYPE_ADD_4(__VA_ARGS__)
    #define TYPE_ADD_6(VAR, ...) \
      MK_VAR_T VAR; TYPE_ADD_5(__VA_ARGS__)
    #define TYPE_ADD_7(VAR, ...) \
      MK_VAR_T VAR; TYPE_ADD_6(__VA_ARGS__)
    #define TYPE_ADD_8(VAR, ...) \
      MK_VAR_T VAR; TYPE_ADD_7(__VA_ARGS__)
    #define TYPE_ADD_9(VAR, ...) \
      MK_VAR_T VAR; TYPE_ADD_8(__VA_ARGS__)
    #define TYPE_ADD_10(VAR, ...) \
      MK_VAR_T VAR; TYPE_ADD_9(__VA_ARGS__)
    #define TYPE_ADD_11(VAR, ...) \
      MK_VAR_T VAR; TYPE_ADD_10(__VA_ARGS__)
    
    
    #define TYPE_ADD_(N, ...) \
      SPLICE(TYPE_ADD_, N)(__VA_ARGS__)
    
    #define TYPE_ADD(...) \
      TYPE_ADD_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
    
    #define REM_BRACKET(...) __VA_ARGS__
    
    #define LAMBDA(name, vars, code) \
    struct name {TYPE_ADD vars void operator()() const REM_BRACKET code}
    
    int main()
    {
      LAMBDA
      (
        some_type_name,
        ((int, a), (float, b), (double, c)),
        ({
          std::cout << "Hello, world! " << a << " " << b << " " << c << std::endl;
        })
      );
      
      auto lambda = some_type_name(1, 3.14f, 0.25);
      lambda();
    }

    Вот вам говнолямбда на препроцессоре.

    j123123, 11 Апреля 2021

    Комментарии (9)
  7. C# / Говнокод #27348

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    using System;
    
    namespace MainNamespace
    {
        class MainClass
        {
            static string str, sep;
            static void Sep()
            {
                int k = 0;
                while (k < str.Length * 2 - 5)
                {
                    if (sep.Length * (k + 1) > str.Length * 2 - 5)
                        break;
                    Console.Write(sep);
                    k++;
                }
                for (int l = 0; l < ((str.Length * 2 - 5) - (k * sep.Length)) ; l++)
                    Console.Write(sep[l]);
            }
            static void Main(string[] args)
            {
                Console.Write("str: ");
                str = Console.ReadLine();
                Console.Write("sep: ");
                sep = Console.ReadLine();
                for (int i = 0; i < str.Length-1; i++)
                    Console.Write(str[i] + " ");
                Console.Write(str[str.Length-1] + "\n\n");
                for (int j = 0; j < str.Length - 2; j++)
                {
                    Console.Write(str[j + 1] + " ");
                    Sep();
                    Console.WriteLine(" " + str[str.Length - j - 2]);
                    Console.Write("  ");
                    if(j < str.Length - 3)
                    {
                        Sep();
                        Console.WriteLine("  ");
                        continue;
                    }
                    Console.WriteLine();
                }
                for (int m = str.Length-1; m >= 1; m--)
                    Console.Write(str[m] + " ");
                Console.WriteLine(str[0]);
                Console.ReadKey();
            }
        }
    }

    Переписал код http://govnokod.ru/27324 на Шарп с небольшими улучшениями.

    BelCodeMonkey, 10 Апреля 2021

    Комментарии (2)
  8. Python / Говнокод #27347

    −1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    def karatsuba_multiplication(x : int, y : int) -> int:
        sx, sy = map(lambda x: '0' + str(x) if len(str(x)) % 2 != 0 else str(x), (x, y))
        return _karatsuba_multiplication(sx, sy, max(len(sx), len(sy)))
    
    def _prepend_nils(string : str, amount_of_nils : int) -> str:
        return ('0' * amount_of_nils + string)
    
    def _karatsuba_multiplication(x : str, y : str, n : int) -> int:
        x, y = map(lambda x: _prepend_nils(x, (n - len(x))), (x, y))
    
        if (n == 1):
            return (int(x) * int(y))
    
        mid = n // 2
        a, b = int(x[:mid]), int(x[mid:])
        c, d = int(y[:mid]), int(y[mid:])
    
        p = a + b
        q = c + d
        ac = _karatsuba_multiplication(str(a), str(c), max(len(str(a)), len(str(c))))
        bd = _karatsuba_multiplication(str(b), str(d), max(len(str(b)), len(str(d))))
        pq = _karatsuba_multiplication(str(p), str(q), max(len(str(p)), len(str(q))))
        adbc = pq - ac - bd
        return 10**n * ac + 10**(mid + n % 2) * adbc + bd

    Как-то не очень получилось...

    JloJle4Ka, 10 Апреля 2021

    Комментарии (24)
  9. Куча / Говнокод #27346

    0

    1. 1
    Пиздец-оффтоп #17

    #1: https://govnokod.ru/26503 https://govnokod.xyz/_26503
    #2: https://govnokod.ru/26541 https://govnokod.xyz/_26541
    #3: https://govnokod.ru/26583 https://govnokod.xyz/_26583
    #4: https://govnokod.ru/26689 https://govnokod.xyz/_26689
    #5: https://govnokod.ru/26784 https://govnokod.xyz/_26784
    #5: https://govnokod.ru/26839 https://govnokod.xyz/_26839
    #6: https://govnokod.ru/26986 https://govnokod.xyz/_26986
    #7: https://govnokod.ru/27007 https://govnokod.xyz/_27007
    #8: https://govnokod.ru/27023 https://govnokod.xyz/_27023
    #9: https://govnokod.ru/27098 https://govnokod.xyz/_27098
    #10: https://govnokod.ru/27125 https://govnokod.xyz/_27125
    #11: https://govnokod.ru/27129 https://govnokod.xyz/_27129
    #12: https://govnokod.ru/27184 https://govnokod.xyz/_27184
    #13: https://govnokod.ru/27286 https://govnokod.xyz/_27286
    #14: https://govnokod.ru/27298 https://govnokod.xyz/_27298
    #15: https://govnokod.ru/27322 https://govnokod.xyz/_27322
    #16: https://govnokod.ru/27328 https://govnokod.xyz/_27328

    nepeKamHblu_nemyx, 10 Апреля 2021

    Комментарии (2402)
  10. Си / Говнокод #27345

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    96. 96
    97. 97
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SPLICE(a,b) SPLICE_1(a,b)
    #define SPLICE_1(a,b) SPLICE_2(a,b)
    #define SPLICE_2(a,b) a##b
    
    
    #define PP_ARG_N( \
              _1,  _2,  _3,  _4,  _5,  _6,  _7,  _8,  _9, _10, \
             _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
             _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
             _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
             _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
             _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
             _61, _62, _63, N, ...) N
    
    /* Note 63 is removed */
    #define PP_RSEQ_N()                                        \
             62, 61, 60,                                       \
             59, 58, 57, 56, 55, 54, 53, 52, 51, 50,           \
             49, 48, 47, 46, 45, 44, 43, 42, 41, 40,           \
             39, 38, 37, 36, 35, 34, 33, 32, 31, 30,           \
             29, 28, 27, 26, 25, 24, 23, 22, 21, 20,           \
             19, 18, 17, 16, 15, 14, 13, 12, 11, 10,           \
              9,  8,  7,  6,  5,  4,  3,  2,  1,  0
    
    #define PP_NARG_(...)    PP_ARG_N(__VA_ARGS__)    
    
    /* Note dummy first argument _ and ##__VA_ARGS__ instead of __VA_ARGS__ */
    #define PP_NARG(...)     PP_NARG_(_, ##__VA_ARGS__, PP_RSEQ_N())
    
    #define FIND_NONNULL_1(RES) \
      ((RES = (char *)(NULL)))
    
    #define FIND_NONNULL_2(RES, VAR) \
      ((RES = (char *)(VAR)))
    
    #define FIND_NONNULL_3(RES, VAR, ...) \
      (((RES = (char *)(VAR)) != NULL)?RES:FIND_NONNULL_2(RES,__VA_ARGS__))
    
    #define FIND_NONNULL_4(RES, VAR, ...) \
      (((RES = (char *)(VAR)) != NULL)?RES:FIND_NONNULL_3(RES,__VA_ARGS__))
    
    #define FIND_NONNULL_5(RES, VAR, ...) \
      (((RES = (char *)(VAR)) != NULL)?RES:FIND_NONNULL_4(RES,__VA_ARGS__))
    
    #define FIND_NONNULL_6(RES, VAR, ...) \
      (((RES = (char *)(VAR)) != NULL)?RES:FIND_NONNULL_5(RES,__VA_ARGS__))
    
    #define FIND_NONNULL_7(RES, VAR, ...) \
      (((RES = (char *)(VAR)) != NULL)?RES:FIND_NONNULL_6(RES,__VA_ARGS__))
    
    #define FIND_NONNULL_8(RES, VAR, ...) \
      (((RES = (char *)(VAR)) != NULL)?RES:FIND_NONNULL_7(RES,__VA_ARGS__))
    
    #define FIND_NONNULL_9(RES, VAR, ...) \
      (((RES = (char *)(VAR)) != NULL)?RES:FIND_NONNULL_8(RES,__VA_ARGS__))
    
    #define FIND_NONNULL_10(RES, VAR, ...) \
      (((RES = (char *)(VAR)) != NULL)?RES:FIND_NONNULL_9(RES,__VA_ARGS__))
    
    #define FIND_NONNULL_11(RES, VAR, ...) \
      (((RES = (char *)(VAR)) != NULL)?RES:FIND_NONNULL_10(RES,__VA_ARGS__))
    
    #define FIND_NONNULL_12(RES, VAR, ...) \
      (((RES = (char *)(VAR)) != NULL)?RES:FIND_NONNULL_11(RES,__VA_ARGS__))
    
    // etc ...
    
    #define FIND_NONNULLS_(N, ...) \
      SPLICE(FIND_NONNULL_, N)(__VA_ARGS__)
    
    #define FIND_NONNULLS(...) \
    ({ \
      char *FIND_NONNULLS; \
      FIND_NONNULLS_(PP_NARG(FIND_NONNULLS, __VA_ARGS__), FIND_NONNULLS, __VA_ARGS__); \
      FIND_NONNULLS; \
    })
    
    char *side_effect_null(void)
    {
      printf("!!null!!\n");
      return NULL;
    }
    
    char *side_effect_test(void)
    {
      printf("!!test!!\n");
      return "test";
    }
    
    int main(void)
    {
      printf( "result:%s\n", FIND_NONNULLS(0,side_effect_null(),0,side_effect_test(),0,0,side_effect_test(),"govno", side_effect_test()) );
      return EXIT_SUCCESS;
    }

    Это типа как short-circuit evaluation чтоб по цепочке хрень возвращающую строку вызывать, и там те функции хуйпойми сколько аргументов могут принимать (но там может быть константа, тогда естественно нихрена не надо вызывать) пока оно не вернет не-NULL. Как только вернуло не-NULL то вернуть это и дальше ничего не вызывать, а то там сайд эффекты всякие ненужные будут. А если не-NULL так и не нашло, вернуть NULL

    j123123, 09 Апреля 2021

    Комментарии (24)