putCount(value: Int, previous: Map) (out: Map) { key = length(previous) out = put(previous, key, value + key) } loop growMap(values: List, seed: Map) (out: Map) | each value in values | result | | out | fold(acc = seed, putCount(value, acc)) | keepMap(values: List, seed: Map) (updated: Map, original: Map) { updated = growMap(values, seed) original = seed } putKey(previous: Map, value: Int) (out: Map) { out = put(previous, value % 2, value) } loop replaceKeys(start: Int, end: Int) (out: Map) | each value in range(start, end) | result | | out | fold(acc = {}, putKey(acc, value)) | loop growBytes(values: Bytes) (out: List) | each value in values | result | | out | fold(acc = [], addValue(value, acc)) | mapKeyFault(value: Int, previous: Map) (out: Map) { key = value + 9_223_372_036_854_775_807 out = put(previous, key, value) } loop mapFault(values: List) (out: Map) | each value in values | result | | out | fold(acc = {}, mapKeyFault(value, acc)) | accumulate(value: Int, previous: Int) (out: Int) { out = previous + value } type Cursor { position: Int, end: Int } rule cursorNext(more: Bool, cursor: Cursor) (out: Walk) | more | cursor | out | | true | - | Next(cursor.position, Cursor { position = cursor.position + 1, end = cursor.end }) | | false | - | Done | advance(cursor: Cursor) (out: Walk) { out = cursorNext(cursor.position < cursor.end, cursor) } advanceChecked(cursor: Cursor) (out: Walk) { check cursor.position <= 1 else Invalid out = advance(cursor) } loop walked(start: Int, end: Int) (items: List, total: Int) | each value in walk(Cursor { position = start, end = end }, advance) | result | | items | list value | | total | sum value | loop walkFirst() (out: Int?) | each value in walk(Cursor { position = 0, end = 5 }, advanceChecked) | result | | out | first value | loop walkMixed() (first: Int?, total: Int) | each value in walk(Cursor { position = 0, end = 5 }, advanceChecked) | result | | first | first value | | total | sum value | loop walkNone() (out: List) | each value in walk(Cursor { position = 2, end = 5 }, advanceChecked) | take | result | | out | 0 | list value | rule textNext(state: Text) (out: Walk) | state | out | | "" | Walk.Done | | other | Walk.Next(state, "") | loop walkText(seed: Text) (out: List) | each value in walk(seed, textNext) | result | | out | list value | loop walkTextSize(seed: Text) (out: Int) | each value in walk(seed, textNext) | result | | out | sum length(value) | decimalAdd(value: Decimal, previous: Decimal) (out: Decimal) { out = previous + value } floatAdd(value: Float, previous: Float) (out: Float) { out = previous + value } loop decimalFold(values: List) (out: Decimal) | each value in values | result | | out | fold(acc = 0, decimalAdd(value, acc)) | loop floatFold(values: List) (out: Float) | each value in values | result | | out | fold(acc = 0.0, floatAdd(value, acc)) | type FoldState { title: Text, count: Int } countNext(value: Int, previous: FoldState) (out: FoldState) { out = FoldState { title = previous.title, count = previous.count + value } } loop countState(values: List, seed: FoldState) (out: FoldState) | each value in values | result | | out | fold(acc = seed, countNext(value, acc)) | sharedState(values: List, seed: FoldState) (out: FoldState, original: FoldState) { out = countState(values, seed) original = seed } loop folded(values: List, seed: Int) (out: Int) | each value in values | result | | out | fold(acc = seed, accumulate(value, acc)) | accumulateUntil(value: Int, previous: Int, limit: Int) (acc: Int, stop: Bool) { acc = previous + value stop = acc >= limit } loop bounded(values: List, limit: Int) (first: Int, total: Int) | each value in values | result | | first | foldUntil(acc = 0, accumulateUntil(value, acc, limit)) | | total | fold(acc = 0, accumulate(value, acc)) | addValue(value: Int, previous: List) (out: List) { out = append(previous, value) } loop foldList(values: List) (out: List) | each value in values | value | result | | out | > 0 | fold(acc = [], addValue(value, acc)) | keepText(previous: Text) (out: Text) { out = previous } loop keepSeed(values: List, seed: Text) (out: Text) | each value in values | result | | out | fold(acc = seed, keepText(acc)) | findValue(value: Int) (acc: Int?, stop: Bool) { acc = value stop = value > 0 } loop findFold(values: List) (out: Int?) | each value in values | result | | out | foldUntil(previous = null, findValue(value)) | checkedAdd(value: Int, previous: Int) (out: Int) { out = previous + checked(value) } loop checkedFold(values: List) (out: Int) | each value in values | result | | out | fold(acc = 0, checkedAdd(value, acc)) | badSource() (out: List) { fail(Invalid) } badSeed() (out: Int) { fail(Forbidden) } badUpdate(previous: Int) (acc: Int, stop: Bool) { fail(Conflict) } loop updateFailure(values: List) (out: Int) | each value in values | result | | out | foldUntil(acc = 7, badUpdate(acc)) | loop rangeFailure() (out: Int) | each value in range(badSeed(), 10) | result | | out | fold(acc = 0, accumulate(value, acc)) | loop rangeEndFailure() (out: Int) | each value in range(0, badSeed()) | result | | out | fold(acc = 0, accumulate(value, acc)) | loop sourceBeforeSeed() (out: Int) | each value in badSource() | result | | out | fold(acc = badSeed(), accumulate(value, acc)) | loop seedBeforeConsume(values: List) (out: Int) | each value in values | result | | out | fold(acc = badSeed(), accumulate(value, acc)) | loop indexed(values: List) (out: Map) | each value in values | result | | out | index value | type IndexResult { Ready(value: Map), error Duplicate(reason: dever.collection.Error) } captureIndex(values: List) (out: IndexResult) { out = result(indexed(values)) } loop indexNames(values: List) (out: Map) | each value in values | result | | out | index length(value) | loop indexFiltered(values: List) (out: Map) | each value in values | value | result | | out | >= 0 | index checked(value) | loop indexFailure(values: List) (out: Map) | each value in values | result | | out | index checked(value) | loop grouped(values: List) (out: Map>) | each value in values | result | | out | group value % 2 | loop groupNames(values: List) (out: Map>) | each value in values | result | | out | group length(value) | loop groupCopies(values: List) (copy: List, out: Map>) | each value in values | result | | copy | list value | | out | group value | loop groupFiltered(values: List) (out: Map>) | each value in values | value | result | | out | >= 0 | group checked(value) | loop groupFailure(values: List) (out: Map>) | each value in values | result | | out | group checked(value) | loop totals(values: List) (total: Int, items: List, count: Int >= 0, first: Int?, smallest: Int?, largest: Int?) | each value in values | result | | largest | max value | | count | count | | total | sum value | | items | list value | | first | first value | | smallest | min value | loop ranged(start: Int, end: Int) (total: Int, count: Int) | each value in range(start, end) | result | | total | sum value | | count | count | positive(value: Int) (out: Bool) { check value >= 0 else Invalid out = value > 0 } checked(value: Int) (out: Int) { check value >= 0 else Invalid out = value } loop anyPositive(values: List) (out: Bool) | each value in values | result | | out | any positive(value) | loop allPositive(values: List) (out: Bool) | each value in values | result | | out | all positive(value) | loop firstPositive(values: List) (out: Int?) | each value in values | result | | out | first checked(value) | loop mixed(values: List) (first: Int?, total: Int) | each value in values | result | | first | first checked(value) | | total | sum checked(value) | loop names(values: List) (copied: List, first: Text?, smallest: Text?) | each value in values | result | | copied | list value | | first | first value | | smallest | min value | loop repeated(values: List, label: Text) (out: List) | each value in values | result | | out | list label | loop decimalTotal(values: List) (out: Decimal) | each value in values | result | | out | sum value | loop floatTotal(values: List) (out: Float) | each value in values | result | | out | sum value | loop action(values: List) () | each value in values | result | | - | do checked(value) | loop actionCount(values: List) (count: Int) | each value in values | result | | count | do checked(value) | flow main() (out: Int) | step | kind | do | | result | loop | ranged(0, 5) | | out | calc | result.total | loop filtered(values: List) (positive: List, first: Int?, count: Int, total: Int) | each value in values | value | 12 / value | result | | positive | > 0 | - | list value | | first | > 0 | >= 2 | first value | | count | - | - | count | | total | < 0, > 0 | - | sum value | loop optionalNames(values: List) (missing: Int, selected: List) | each value in values | value | result | | missing | null | count | | selected | "甲", "乙" | list value | loop filteredFailure(values: List) (out: Int?) | each value in values | value | result | | out | >= 0 | first checked(value) | loop filteredOwned(values: List) (copied: List, selected: Int) | each value in values | length(value + "!") | result | | copied | > 1 | list value | | selected | > 1 | count | positiveOnly(value: Int >= 1) (out: Int) { out = value } loop refined(values: List) (out: List) | each value in values | value | result | | out | >= 1 | list positiveOnly(value) | loop refinedNames(values: List) (out: List, count: Int) | each value in values | value | result | | out | "甲", "乙" | list value | | count | - | count | type Event { Named(label: Text), Tagged(label: Text), Empty } type Envelope { event: Event } type MaybeEnvelope { event: Event? } loop payloads(values: List) (labels: List, size: Int) | each value in values | value | result | | labels | Named(label), Tagged(label) | list label | | size | Named(label), Tagged(label) | sum length(label) | loop payloadFields(values: List) (labels: List, size: Int) | each value in values | value.event | result | | labels | Named(label), Tagged(label) | list label | | size | Named(label), Tagged(label) | sum length(label) | loop optionalPayloads(values: List) (labels: List) | each value in values | value.event | result | | labels | Named(label), Tagged(label) | list label | wrap(value: Int) (out: Event?) { out = Event.Named("名") } loop computedPayloads(values: List) (labels: List) | each value in values | wrap(value) | result | | labels | Named(label), Tagged(label) | list label | loop movingPayloads(values: List) (labels: List) | each value in values | value | result | | labels | Named(label), Tagged(label) | list label | type InputState { Ready, error Broken } loop rejectBroken(values: List) () | each value in values | value | result | | - | Broken | do fail(value) | loop limited(values: List, limit: Int >= 0) (out: List, count: Int) | each value in values | value | take | result | | out | >= 0 | limit | list checked(value) | | count | - | - | count | loop takeOnly(values: List, limit: Int >= 0) (out: List) | each value in values | take | result | | out | limit | list checked(value) | loop takeZero(values: List) (out: List) | each value in values | 1 / value | take | result | | out | > 0 | 0 | list checked(value) | loop initializeLimit(values: List, size: Int >= 0) (out: List) | each value in values | take | result | | out | size + 1 | list value | type Ranked { group: Int, score: Decimal, label: Text } loop ranked(values: List, limit: Int >= 0) (out: List, count: Int) | each value in values | sort | take | result | | out | [value.group.asc, value.score.desc] | limit | list value.label | | count | - | - | count | loop sortedTexts(values: List) (out: List) | each value in values | sort | result | | out | [length(value).asc] | list value | loop sortedChecked(values: List, limit: Int >= 0) (out: List) | each value in values | sort | take | result | | out | [value.desc] | limit | list checked(value) | loop sortBeforeResult(values: List, limit: Int >= 0) (out: List) | each value in values | sort | take | result | | out | [(1 / value).asc] | limit | list checked(-1) | type ListState { items: List, left: Int, right: Int } inlineNext(previous: ListState, value: Int) (out: ListState) { out = ListState { items = append(previous.items, value), left = previous.right, right = previous.left } } loop inlineState(values: List, seed: ListState) (out: ListState) | each value in values | result | | out | fold(acc = seed, inlineNext(acc, value)) | keepListState(values: List, seed: ListState) (updated: ListState, original: ListState) { updated = inlineState(values, seed) original = seed } prefixNext(value: Int, previous: ListState) (out: ListState) { count = length(previous.items) out = ListState { left = previous.right + count, items = append(previous.items, value + count), right = previous.left } } loop prefixState(values: List, seed: ListState) (out: ListState) | each value in values | result | | out | fold(acc = seed, prefixNext(value, acc)) | aliasNext(value: Int, previous: ListState) (out: ListState) { items = append(previous.items, length(previous.items) + value) count = previous.left + 1 out = ListState { right = previous.left, left = count, items = items } } loop aliasState(values: List, seed: ListState) (out: ListState) | each value in values | result | | out | fold(acc = seed, aliasNext(value, acc)) | oldListNext(value: Int, previous: ListState) (out: ListState) { items = append(previous.items, value) count = length(previous.items) out = ListState { items = items, left = count, right = length(items) } } loop oldListState(values: List, seed: ListState) (out: ListState) | each value in values | result | | out | fold(acc = seed, oldListNext(value, acc)) | stateCount(previous: ListState) (out: Int) { out = length(previous.items) } wholeStateNext(value: Int, previous: ListState) (out: ListState) { out = ListState { items = append(previous.items, stateCount(previous) + value), left = previous.left, right = previous.right } } loop wholeState(values: List, seed: ListState) (out: ListState) | each value in values | result | | out | fold(acc = seed, wholeStateNext(value, acc)) | recordFaultNext(previous: ListState, value: Int) (out: ListState) { out = ListState { left = previous.left + value, items = append(previous.items, 1 // value), right = previous.right } } loop recordFault(values: List, seed: ListState) (out: ListState) | each value in values | result | | out | fold(acc = seed, recordFaultNext(acc, value)) | type ReadState { items: List, first: Int? } readStateNext(previous: ReadState, value: Int) (out: ReadState) { first = get(previous.items, 0) out = ReadState { items = append(previous.items, value), first = first } } loop readState(values: List, seed: ReadState) (out: ReadState) | each value in values | result | | out | fold(acc = seed, readStateNext(acc, value)) | type TextState { items: List, count: Int } textStateNext(previous: TextState, value: Int) (out: TextState) { out = TextState { items = append(previous.items, "next"), count = previous.count + value } } loop textState(values: List, seed: TextState) (out: TextState) | each value in values | result | | out | fold(acc = seed, textStateNext(acc, value)) | loop sumChecked(values: List) (out: Int) | each value in values | result | | out | sum value | loop sumBeforeFault(values: List) (out: Int) | each value in values | result | | out | sum 9_223_372_036_854_775_807 // value | appendTextPart(value: Text, previous: Text) (out: Text) { out = previous + value } loop joinTextParts(values: List, seed: Text) (out: Text) | each value in values | result | | out | fold(acc = seed, appendTextPart(value, acc)) | keepTextParts(values: List, seed: Text) (out: Text, original: Text) { out = joinTextParts(values, seed) original = seed } loop projectedKeys(rows: List>) (out: List) | each row in rows | result | | out | list row.key | loop projectedValues(rows: List>) (out: List) | each row in rows | result | | out | list row.value | projectEntries(table: Map) (keys: List, values: List, original: Map) { keys = projectedKeys(entries(table)) values = projectedValues(entries(table)) original = table } loop projectedLists(rows: List>>) (out: List>) | each row in rows | result | | out | list row.value | projectLists(table: Map>) (values: List>, original: Map>) { values = projectedLists(entries(table)) original = table }