ramda#reduce TypeScript Examples

The following examples show how to use ramda#reduce. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: L1-eval.ts    From interpreters with MIT License 6 votes vote down vote up
applyPrimitive = (proc: PrimOp, args: Value[]): Result<Value> =>
    // @ts-ignore: the rhs of an arithmetic operation must be a number
    proc.op === "+" ? makeOk(reduce((x, y) => x + y, 0, args)) :
    // This implementation is wrong - no type checking and no verification
    // of associativity: what should be (- 1 2 3): (- 1 (- 2 3)) i.e. 2 or (- (- 1 2) 3) i.e. -4
    // proc.op === "-" ? makeOk(reduce((x, y) => x - y, 0, args)) :
    // @ts-ignore: the rhs of an arithmetic operation must be a number
    proc.op === "-" ? makeOk(args[0] - args[1]) :
    // @ts-ignore: the rhs of an arithmetic operation must be a number
    proc.op === "*" ? makeOk(reduce((x, y) => x * y, 1, args)) :
    // This implementation is wrong - no type checking and no verification
    // of associativity: what should be (/ 1 2 3): (/ 1 (/ 2 3)) i.e. 1.5 or (/ (/ 1 2) 3) i.e. 1/6
    // proc.op === "/" ? makeOk(reduce((x, y) => x / y, 1, args)) :
    // @ts-ignore: the rhs of an arithmetic operation must be a number
    proc.op === "/" ? makeOk(args[0] / args[1]) :
    proc.op === ">" ? makeOk(args[0] > args[1]) :
    proc.op === "<" ? makeOk(args[0] < args[1]) :
    proc.op === "=" ? makeOk(args[0] === args[1]) :
    proc.op === "not" ? makeOk(!args[0]) :
    makeFailure("Bad primitive op " + proc.op)
Example #2
Source File: evalPrimitive.ts    From interpreters with MIT License 6 votes vote down vote up
applyPrimitive = (proc: PrimOp, args: Value[]): Result<Value> =>
    proc.op === "+" ? (allT(isNumber, args) ? makeOk(reduce((x, y) => x + y, 0, args)) : makeFailure(`+ expects numbers only: ${JSON.stringify(args, null, 2)}`)) :
    proc.op === "-" ? minusPrim(args) :
    proc.op === "*" ? (allT(isNumber, args) ? makeOk(reduce((x, y) => x * y, 1, args)) : makeFailure(`* expects numbers only: ${JSON.stringify(args, null, 2)}`)) :
    proc.op === "/" ? divPrim(args) :
    proc.op === ">" ? makeOk(args[0] > args[1]) :
    proc.op === "<" ? makeOk(args[0] < args[1]) :
    proc.op === "=" ? makeOk(args[0] === args[1]) :
    proc.op === "not" ? makeOk(!args[0]) :
    proc.op === "and" ? isBoolean(args[0]) && isBoolean(args[1]) ? makeOk(args[0] && args[1]) : makeFailure(`Arguments to "and" not booleans: ${JSON.stringify(args, null, 2)}`) :
    proc.op === "or" ? isBoolean(args[0]) && isBoolean(args[1]) ? makeOk(args[0] || args[1]) : makeFailure(`Arguments to "or" not booleans: ${JSON.stringify(args, null, 2)}`) :
    proc.op === "eq?" ? makeOk(eqPrim(args)) :
    proc.op === "string=?" ? makeOk(args[0] === args[1]) :
    proc.op === "cons" ? makeOk(consPrim(args[0], args[1])) :
    proc.op === "car" ? carPrim(args[0]) :
    proc.op === "cdr" ? cdrPrim(args[0]) :
    proc.op === "list" ? makeOk(listPrim(args)) :
    proc.op === "pair?" ? makeOk(isPairPrim(args[0])) :
    proc.op === "number?" ? makeOk(typeof (args[0]) === 'number') :
    proc.op === "boolean?" ? makeOk(typeof (args[0]) === 'boolean') :
    proc.op === "symbol?" ? makeOk(isSymbolSExp(args[0])) :
    proc.op === "string?" ? makeOk(isString(args[0])) :
    makeFailure(`Bad primitive op: ${JSON.stringify(proc.op, null, 2)}`)
Example #3
Source File: freeVars.ts    From interpreters with MIT License 6 votes vote down vote up
height = (exp: Program | Exp): number =>
    isAtomicExp(exp) ? 1 :
    isLitExp(exp) ? 1 :
    isDefineExp(exp) ? 1 + height(exp.val) :
    isIfExp(exp) ? 1 + Math.max(height(exp.test), height(exp.then), height(exp.alt)) :
    isProcExp(exp) ? 1 + reduce(Math.max, zero,
                                map((bodyExp) => height(bodyExp), exp.body)) :
    isLetExp(exp) ? 1 + Math.max(
                            reduce(Math.max, zero,
                                   map((binding) => height(binding.val), exp.bindings)),
                            reduce(Math.max, zero,
                                   map((bodyExp) => height(bodyExp), exp.body))) :
    isAppExp(exp) ? Math.max(height(exp.rator),
                             reduce(Math.max, zero,
                                    map((rand) => height(rand), exp.rands))) :
    isProgram(exp) ? 1 + reduce(Math.max, zero,
                                map((e) => height(e), exp.exps)) :
    exp
Example #4
Source File: freeVars.ts    From interpreters with MIT License 6 votes vote down vote up
referencedVars = (e: Program | Exp): VarRef[] =>
    isBoolExp(e) ? Array<VarRef>() :
    isNumExp(e) ? Array<VarRef>() :
    isStrExp(e) ? Array<VarRef>() :
    isLitExp(e) ? Array<VarRef>() :
    isPrimOp(e) ? Array<VarRef>() :
    isVarRef(e) ? [e] :
    isIfExp(e) ? reduce(varRefUnion, Array<VarRef>(),
                        map(referencedVars, [e.test, e.then, e.alt])) :
    isAppExp(e) ? union(referencedVars(e.rator),
                        reduce(varRefUnion, Array<VarRef>(), map(referencedVars, e.rands))) :
    isProcExp(e) ? reduce(varRefUnion, Array<VarRef>(), map(referencedVars, e.body)) :
    isDefineExp(e) ? referencedVars(e.val) :
    isProgram(e) ? reduce(varRefUnion, Array<VarRef>(), map(referencedVars, e.exps)) :
    isLetExp(e) ? Array<VarRef>() : // TODO
    e
Example #5
Source File: evalPrimitive-box.ts    From interpreters with MIT License 6 votes vote down vote up
applyPrimitive = (proc: PrimOp, args: Value[]): Result<Value> =>
    proc.op === "+" ? (allT(isNumber, args) ? makeOk(reduce((x: number, y: number) => x + y, 0, args)) : makeFailure("+ expects numbers only")) :
    proc.op === "-" ? minusPrim(args) :
    proc.op === "*" ? (allT(isNumber, args) ? makeOk(reduce((x: number, y: number) => x * y, 1, args)) : makeFailure("* expects numbers only")) :
    proc.op === "/" ? divPrim(args) :
    proc.op === ">" ? ((allT(isNumber, args) || allT(isString, args)) ? makeOk(args[0] > args[1]) : makeFailure("> expects numbers or strings only")) :
    proc.op === "<" ? ((allT(isNumber, args) || allT(isString, args)) ? makeOk(args[0] < args[1]) : makeFailure("< expects numbers or strings only")) :
    proc.op === "=" ? makeOk(args[0] === args[1]) :
    proc.op === "not" ? makeOk(! args[0]) :
    proc.op === "and" ? isBoolean(args[0]) && isBoolean(args[1]) ? makeOk(args[0] && args[1]) : makeFailure('Arguments to "and" not booleans') :
    proc.op === "or" ? isBoolean(args[0]) && isBoolean(args[1]) ? makeOk(args[0] || args[1]) : makeFailure('Arguments to "or" not booleans') :
    proc.op === "eq?" ? makeOk(eqPrim(args)) :
    proc.op === "string=?" ? makeOk(args[0] === args[1]) :
    proc.op === "cons" ? makeOk(consPrim(args[0], args[1])) :
    proc.op === "car" ? carPrim(args[0]) :
    proc.op === "cdr" ? cdrPrim(args[0]) :
    proc.op === "list" ? makeOk(listPrim(args)) :
    proc.op === "list?" ? makeOk(isListPrim(args[0])) :
    proc.op === "pair?" ? makeOk(isPairPrim(args[0])) :
    proc.op === "number?" ? makeOk(typeof(args[0]) === 'number') :
    proc.op === "boolean?" ? makeOk(typeof(args[0]) === 'boolean') :
    proc.op === "symbol?" ? makeOk(isSymbolSExp(args[0])) :
    proc.op === "string?" ? makeOk(isString(args[0])) :
    makeFailure(`Bad primitive op: ${JSON.stringify(proc.op)}`)
Example #6
Source File: evalPrimitive.ts    From interpreters with MIT License 6 votes vote down vote up
applyPrimitive = (proc: PrimOp, args: Value[]): Result<Value> =>
    proc.op === "+" ? (allT(isNumber, args) ? makeOk(reduce((x: number, y: number) => x + y, 0, args)) : makeFailure("+ expects numbers only")) :
    proc.op === "-" ? minusPrim(args) :
    proc.op === "*" ? (allT(isNumber, args) ? makeOk(reduce((x: number, y: number) => x * y, 1, args)) : makeFailure("* expects numbers only")) :
    proc.op === "/" ? divPrim(args) :
    proc.op === ">" ? makeOk(args[0] > args[1]) :
    proc.op === "<" ? makeOk(args[0] < args[1]) :
    proc.op === "=" ? makeOk(args[0] === args[1]) :
    proc.op === "not" ? makeOk(! args[0]) :
    proc.op === "and" ? isBoolean(args[0]) && isBoolean(args[1]) ? makeOk(args[0] && args[1]) : makeFailure('Arguments to "and" not booleans') :
    proc.op === "or" ? isBoolean(args[0]) && isBoolean(args[1]) ? makeOk(args[0] || args[1]) : makeFailure('Arguments to "or" not booleans') :
    proc.op === "eq?" ? makeOk(eqPrim(args)) :
    proc.op === "string=?" ? makeOk(args[0] === args[1]) :
    proc.op === "cons" ? makeOk(consPrim(args[0], args[1])) :
    proc.op === "car" ? carPrim(args[0]) :
    proc.op === "cdr" ? cdrPrim(args[0]) :
    proc.op === "list" ? makeOk(listPrim(args)) :
    proc.op === "list?" ? makeOk(isListPrim(args[0])) :
    proc.op === "pair?" ? makeOk(isPairPrim(args[0])) :
    proc.op === "number?" ? makeOk(typeof(args[0]) === 'number') :
    proc.op === "boolean?" ? makeOk(typeof(args[0]) === 'boolean') :
    proc.op === "symbol?" ? makeOk(isSymbolSExp(args[0])) :
    proc.op === "string?" ? makeOk(isString(args[0])) :
    makeFailure(`Bad primitive op: ${JSON.stringify(proc.op)}`)
Example #7
Source File: evalPrimitive.ts    From interpreters with MIT License 6 votes vote down vote up
applyPrimitive = (proc: PrimOp, args: Value[]): Result<Value> =>
    proc.op === "+" ? (allT(isNumber, args) ? makeOk(reduce((x, y) => x + y, 0, args)) : makeFailure("+ expects numbers only")) :
    proc.op === "-" ? minusPrim(args) :
    proc.op === "*" ? (allT(isNumber, args) ? makeOk(reduce((x, y) => x * y, 1, args)) : makeFailure("* expects numbers only")) :
    proc.op === "/" ? divPrim(args) :
    proc.op === ">" ? makeOk(args[0] > args[1]) :
    proc.op === "<" ? makeOk(args[0] < args[1]) :
    proc.op === "=" ? makeOk(args[0] === args[1]) :
    proc.op === "not" ? makeOk(! args[0]) :
    proc.op === "eq?" ? makeOk(eqPrim(args)) :
    proc.op === "string=?" ? makeOk(args[0] === args[1]) :
    proc.op === "cons" ? makeOk(consPrim(args[0], args[1])) :
    proc.op === "car" ? carPrim(args[0]) :
    proc.op === "cdr" ? cdrPrim(args[0]) :
    proc.op === "list" ? makeOk(listPrim(args)) :
    proc.op === "list?" ? makeOk(isListPrim(args[0])) :
    proc.op === "pair?" ? makeOk(isPairPrim(args[0])) :
    proc.op === "number?" ? makeOk(typeof(args[0]) === 'number') :
    proc.op === "boolean?" ? makeOk(typeof(args[0]) === 'boolean') :
    proc.op === "symbol?" ? makeOk(isSymbolSExp(args[0])) :
    proc.op === "string?" ? makeOk(isString(args[0])) :
    // display, newline
    makeFailure(`Bad primitive op: ${proc.op}`)
Example #8
Source File: useNextQuestion.tsx    From nosgestesclimat-site with MIT License 6 votes vote down vote up
export function getNextSteps(
	missingVariables: Array<MissingVariables>
): Array<DottedName> {
	const byCount = ([, [count]]: [unknown, [number]]) => count
	const byScore = ([, [, score]]: [unknown, [unknown, number]]) => score

	const missingByTotalScore = reduce<MissingVariables, MissingVariables>(
		mergeWith(add),
		{},
		missingVariables
	)

	const innerKeys = flatten(map(keys, missingVariables)),
		missingByTargetsAdvanced = Object.fromEntries(
			Object.entries(countBy(identity, innerKeys)).map(
				// Give higher score to top level questions
				([name, score]) => [
					name,
					score + Math.max(0, 4 - name.split('.').length),
				]
			)
		)

	const missingByCompound = mergeWith(
			pair,
			missingByTargetsAdvanced,
			missingByTotalScore
		),
		pairs = toPairs<number>(missingByCompound),
		sortedPairs = sortWith([descend(byCount), descend(byScore) as any], pairs)
	return map(head, sortedPairs) as any
}
Example #9
Source File: utils.ts    From notabug with MIT License 6 votes vote down vote up
listingNodesToIds = pipe<
  readonly GunNode[],
  ReadonlyArray<readonly string[]>,
  readonly string[]
>(
  map(listingNodeToIds),
  reduce<readonly string[], readonly string[]>(union, [])
)