Skip to content

Instantly share code, notes, and snippets.

@krisalyssa
Created August 30, 2021 15:39
Show Gist options
  • Save krisalyssa/3289681eff899fcc7f0eef40851c02d1 to your computer and use it in GitHub Desktop.
Save krisalyssa/3289681eff899fcc7f0eef40851c02d1 to your computer and use it in GitHub Desktop.
"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()
@krisalyssa
Copy link
Author

% 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