reduce
이 페이지에서

reduce()

함수, 작성 분류

Fold a value over a C-style loop: the value-returning spelling of the var acc = init; for (...) { acc = f(acc, i) } accumulator.

import { reduce } from 'typeshade'

시그니처, 설명, 예제는 커밋 c66579bf의 컴파일러 소스에서 그대로 가져온 영어 원문입니다.

구문

function reduce<K extends string, J extends string>(
init: ReadonlyNode<K>,
loopInit: ReadonlyNode<J>,
cond: (i: Node<J>) => ReadonlyNode<'bool'>,
body: (acc: Node<K>, i: Node<J>) => ReadonlyNode<K>,
step?: ReadonlyNode<ScalarKey> | number,
): Node<K>

매개변수

init ReadonlyNode<K>

the accumulator’s initial value, which also fixes its type.

loopInit ReadonlyNode<J>

the counter’s initial value.

cond (i: Node<J>) => ReadonlyNode<'bool'>

the continue test, receiving the counter.

body (acc: Node<K>, i: Node<J>) => ReadonlyNode<K>

receives the accumulator and the counter, and returns the next accumulator.

step선택 사항 ReadonlyNode<ScalarKey> | number

the per-iteration increment. Defaults to +1.

반환값

Node<K>

a node reading the accumulator after the loop.

설명

The body returns the next accumulator value, so the call site declares no Var and writes no assign; reduce materialises the variable, the loop and the assignment internally, and the emitted statements are the same as the hand-written form. Returns the accumulator node for use after the loop.

예제

예제

import { reduce, toF32, f32, u32 } from 'typeshade'
const sum = reduce(f32(0), u32(0), (i) => i.lt(u32(8)), (acc, i) => acc.add(toF32(i)))

관련 가이드

함께 보기

소스

src/core/ir/builder.ts, 1943행, 커밋 c66579bf 기준

이 페이지 편집 문제 보고