Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var reduce_1 = __importDefault(require("lodash/reduce"));
function testReduction() {
var tempArray = [
{ a: 'foo', b: 'bar' },
{ a: 'baz', b: 'quux' }
];
var reduction = reduce_1["default"](tempArray, function (acc, value) {
console.log("value: a='" + value.a + "', b='" + value.b + "'");
console.log("acc before=" + JSON.stringify(acc));
var newAcc = acc.set(value.a, value);
console.log("acc after=" + JSON.stringify(acc));
console.log("newAcc=" + JSON.stringify(newAcc));
return acc;
}, new Map());
console.log("reduction=" + JSON.stringify(reduction));
}
testReduction();
import reduce from 'lodash/reduce'
type ReductionTest = {
a: string
b: string
}
function testReduction () {
const tempArray: ReductionTest[] = [
{ a: 'foo', b: 'bar' },
{ a: 'baz', b: 'quux' }
]
const reduction = reduce(
tempArray,
(acc: Map<string, ReductionTest>, value: ReductionTest) => {
console.log(`value: a='${value.a}', b='${value.b}'`)
console.log(`acc before=${JSON.stringify(acc)}`)
const newAcc = acc.set(value.a, value)
console.log(`acc after=${JSON.stringify(acc)}`)
console.log(`newAcc=${JSON.stringify(newAcc)}`)
return acc
},
new Map<string, ReductionTest>()
)
console.log(`reduction=${JSON.stringify(reduction)}`)
}
testReduction()
@CraigCottingham
Copy link
Author

CraigCottingham commented Aug 30, 2021

% tsc --esModuleInterop reduction.ts && node reduction.js
value: a='foo', b='bar'
acc before={}
acc after={}
newAcc={}
value: a='baz', b='quux'
acc before={}
acc after={}
newAcc={}
reduction={}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment