idlize

Форк
0
118 строк · 3.5 Кб
1
/*
2
 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15

16
import { KoalaCallsiteKey, int32 } from "@koalaui/common"
17
import { __context, __id } from "../internals"
18
import { memoEntry1, memoEntry2 } from "./entry"
19

20
/**
21
 * Sequentially repeats the {@link action}.
22
 *
23
 * An {@link action} unit accepts a sequential index as an argument.
24
 *
25
 * @param count the number of {@link action}'s to repeat, 0 by default
26
 * @param action an action to repeat
27
 *
28
 * @see RepeatWithKey
29
 * @see RepeatByArray
30
 * @see Items
31
 *
32
 * @memo
33
 */
34
export function Repeat(
35
    count: int32,
36
    /** @memo */
37
    action: (index: int32) => void
38
) {
39
    for (let i = 0; i < count; i++) {
40
        memoEntry1<int32, void>(__context(), i, action, i)
41
    }
42
}
43

44
/**
45
 * Sequentially repeats the {@link action} using the {@link key} callback.
46
 *
47
 * By default, each {@link action} has an implicit key which provides the {@link action}'s
48
 * result uniqueness. The {@link key} callback can be used to provide a custom key per
49
 * {@link action}, based on the {@link action}'s index accepted as an argument.
50
 *
51
 * @param count the number of {@link action}'s to repeat, 0 by default
52
 * @param key a callback to provide custom keys
53
 * @param action an action to repeat
54
 *
55
 * @see Repeat
56
 *
57
 * @memo
58
 */
59
export function RepeatWithKey(
60
    count: int32,
61
    key: (index: int32) => KoalaCallsiteKey,
62
    /** @memo */
63
    action: (index: int32) => void
64
) {
65
    for (let i = 0; i < count; i++) {
66
        memoEntry1<int32, void>(__context(), key(i), action, i)
67
    }
68
}
69

70
/**
71
 * Sequentially repeats the {@link action} by iterating the {@link array},
72
 * passing an element of the {@link array} as the {@link action}'s argument.
73
 *
74
 * @param array the array to iterate
75
 * @param key a callback to provide custom keys
76
 * @param action an action to repeat
77
 *
78
 * @see RepeatWithKey
79
 *
80
 * @memo
81
 */
82
export function RepeatByArray<T>(
83
    array: ReadonlyArray<T>,
84
    key: (element: T, index: int32) => KoalaCallsiteKey,
85
    /** @memo */
86
    action: (element: T, index: int32) => void
87
) {
88
    const length = array.length
89
    for (let i = 0; i < length; i++) {
90
        const e: T = array[i]
91
        memoEntry2<T, int32, void>(__context(), key(e, i), action, e, i)
92
    }
93
}
94

95
/**
96
 * Sequentially repeats the {@link action} for every element in the given range.
97
 *
98
 * @param start - the start index, inclusive
99
 * @param end - the end index, exclusive
100
 * @param element - the function to provide an element by its index
101
 * @param key - the function to provide a key for the element
102
 * @param action - the memo function to be invoked for every element
103
 *
104
 * @memo
105
 */
106
export function RepeatRange<T>(
107
    start: int32,
108
    end: int32,
109
    element: (index: int32) => T,
110
    key: (element: T, index: int32) => KoalaCallsiteKey,
111
    /** @memo */
112
    action: (element: T, index: int32) => void
113
) {
114
    for (let i: int32 = start; i < end; i++) {
115
        const e: T = element(i)
116
        memoEntry2<T, int32, void>(__context(), key(e, i), action, e, i)
117
    }
118
}
119

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.