|
832 | 832 | (set _i (+ 1 _i)) }) |
833 | 833 | _output })) |
834 | 834 |
|
835 | | -# @brief Compute permutations of length _r from a given list |
| 835 | +# @brief Compute combinations of length _r from a given list |
836 | 836 | # @details The original list is not modified. |
837 | 837 | # @param _L list to get values from |
838 | 838 | # @param _r number of elements per permutation |
|
873 | 873 | (set _continue false)) } |
874 | 874 | (set _continue false)) }) }) }) })) |
875 | 875 |
|
876 | | -# @brief Compute permutations of length _r from a given list |
877 | | -# @details The original list is not modified. |
878 | | -# @param _L list to get values from |
879 | | -# @param _r number of elements per permutation |
880 | | -# @param _f function to call on each permutation. It can return list:stopIteration to stop iteration early |
881 | | -# @deprecated Use list:combinations. Will be removed in ArkScript 4.5.0 |
882 | | -# =begin |
883 | | -# (let data [0 1 2 3]) |
884 | | -# (list:permutations data 3 (fun (perm) (print perm))) |
885 | | -# # [0 1 2] |
886 | | -# # [0 1 3] |
887 | | -# # [0 2 3] |
888 | | -# # [1 2 3] |
889 | | -# =end |
890 | | -# @author https://github.com/SuperFola |
891 | | -(let permutations combinations) |
892 | | - |
893 | | -# @brief Compute permutations of length _r from a given list, allowing individual elements to be repeated more than once |
| 876 | +# @brief Compute combinations of length _r from a given list, allowing individual elements to be repeated more than once |
894 | 877 | # @details The original list is not modified. |
895 | 878 | # @param _L list to get values from |
896 | 879 | # @param _r number of elements per permutation |
|
933 | 916 | (set _continue false)) } |
934 | 917 | (set _continue false)) }) }) }) })) |
935 | 918 |
|
936 | | -# @brief Compute permutations of length _r from a given list, allowing individual elements to be repeated more than once |
| 919 | +# @brief Compute permutations of length _r from a given list |
937 | 920 | # @details The original list is not modified. |
938 | 921 | # @param _L list to get values from |
939 | 922 | # @param _r number of elements per permutation |
940 | 923 | # @param _f function to call on each permutation. It can return list:stopIteration to stop iteration early |
941 | | -# @deprecated Use list:combinationsWithReplacement. Will be removed in ArkScript 4.5.0 |
942 | 924 | # =begin |
943 | | -# (let data [0 1 2]) |
944 | | -# (list:permutationsWithReplacement data 2 (fun (perm) (print perm))) |
945 | | -# # [0 0] |
946 | | -# # [0 1] |
947 | | -# # [0 2] |
948 | | -# # [1 1] |
949 | | -# # [1 2] |
950 | | -# # [2 2] |
| 925 | +# (let data [0 1 2 3]) |
| 926 | +# (mut out []) |
| 927 | +# (list:permutations data 3 (fun (perm) (append! out perm))) |
| 928 | +# (print out) # length: 24 |
| 929 | +# # [[0 1 2] [0 1 3] [0 2 1] [0 2 3] [0 3 1] [0 3 2] [1 0 2] [1 0 3] [1 2 0] [1 2 3] [1 3 0] [1 3 2] |
| 930 | +# # [2 0 1] [2 0 3] [2 1 0] [2 1 3] [2 3 0] [2 3 1] [3 0 1] [3 0 2] [3 1 0] [3 1 2] [3 2 0] [3 2 1]] |
951 | 931 | # =end |
952 | 932 | # @author https://github.com/SuperFola |
953 | | -(let permutationsWithReplacement combinationsWithReplacement) |
| 933 | +(let permutations (fun ((ref _L) _r _f) { |
| 934 | + (let _len (len _L)) |
| 935 | + (if (and (<= _r _len) (> _r 0) (> _len 0)) |
| 936 | + { |
| 937 | + (let _ind_first_r (iota 0 _r)) |
| 938 | + (mut _indices (iota 0 _len)) |
| 939 | + (mut _cycles (iterate _len (fun (x) (- x 1)) _r)) |
| 940 | + (if (!= stopIteration (_f (select _L _ind_first_r))) |
| 941 | + { |
| 942 | + (mut _continue true) |
| 943 | + (let _reversed_indices (reverse _ind_first_r)) |
| 944 | + (while _continue { |
| 945 | + (if |
| 946 | + (not (forEach |
| 947 | + _reversed_indices |
| 948 | + (fun (_i) { |
| 949 | + (@= _cycles _i (- (@ _cycles _i) 1)) |
| 950 | + (if (= 0 (@ _cycles _i)) |
| 951 | + { |
| 952 | + (let _at_i (@ _indices _i)) |
| 953 | + (mut _k _i) |
| 954 | + (while (< _k _len) { |
| 955 | + (if (< (+ 1 _k) _len) |
| 956 | + (@= _indices _k (@ _indices (+ 1 _k)))) |
| 957 | + (set _k (+ 1 _k)) }) |
| 958 | + (@= _indices -1 _at_i) |
| 959 | + (@= _cycles _i (- _len _i)) } |
| 960 | + { |
| 961 | + (let _j (* -1 (@ _cycles _i))) |
| 962 | + (let _ind_j (@ _indices _j)) |
| 963 | + (@= _indices _j (@ _indices _i)) |
| 964 | + (@= _indices _i _ind_j) |
| 965 | + (if (= stopIteration (_f (select _L (select _indices _ind_first_r)))) |
| 966 | + (set _continue false)) |
| 967 | + stopIteration }) }))) |
| 968 | + (set _continue false)) }) }) }) })) |
0 commit comments