Skip to content

Instantly share code, notes, and snippets.

View KMNowak's full-sized avatar
☄️

Chris Nowak KMNowak

☄️
View GitHub Profile
@KMNowak
KMNowak / promiseArrayResolve.js
Created October 10, 2017 11:54
Resolving array of promises
const getProductImagesByIds = productImagesIds =>
Promise.all(productImagesIds.map(PROMISE_TO_SOLVE_WITH_ID_FROM_ARRAY))
.then(results => Array.isArray(results) ?
results.reduce((acc, row) => [...acc, row.url], []) :
[results]
)
.catch(e => {
throw new Error(e)
})
const addTwo = prop => {
console.log(`In addTwo, prop: ${prop}`)
return prop + 2
}
const addTen = prop => {
console.log(`In addTen, prop: ${prop}`)
return prop + 10
}
@KMNowak
KMNowak / 1_algorithmic_task.ts
Created December 13, 2019 21:22
1_algorithmic_task.ts
// Write a function that receives two sequences:
// A and B of integers and returns one sequence C.
// Sequence C should contain all elements from sequence A (maintaining the order)
// except those, that are present in sequence B p times, where p is a prime number.
// Example:
// A=[2,3,9,2,5,1,3,7,10]
// B=[2,1,3,4,3,10,6,6,1,7,10,10,10]
// C=[2,9,2,5,7,10]
@KMNowak
KMNowak / apply-to-all-leaves.ts
Created May 6, 2020 17:13
Allows to apply given function to all values of given object including elements of an array and sub objects
const isObject = (subj: any) => typeof subj === 'object' && !Array.isArray(subj) && subj !== null;
export const applyToAllProps = (obj: Record<any, any>, fun: (props: any) => any) => {
const appliedEntries: any = Object
.entries(obj)
.map(([key, value]) => ([key, applyToProp(value, fun)]));
return Object.fromEntries(appliedEntries);
};
@KMNowak
KMNowak / codility-mushroom-picker.js
Last active August 19, 2022 12:06
Solution to 'Mushroom Picker' Codility task with O(n+m) complexity thanks to use of prefix sums. Source: https://codility.com/media/train/3-PrefixSums.pdf
const a = [2,3,7,5,1,3,9]
const startI = 4
const moves = 6
const mushroomPicker = (A, k, m) => {
let maxSum = 0
let sumFromMovesL = 0 // stores prefix sum for each step moving left
for (
let movesL = 0;