Skip to content

Instantly share code, notes, and snippets.

View DanShappir's full-sized avatar

Dan Shappir DanShappir

  • Wix
View GitHub Profile
@DanShappir
DanShappir / pipe_button_new.js
Created November 1, 2020 10:10
Processing DOM events with pipe function
const myButton = document.getElementById("myButton");
pipe(
fromEvent(myButton, "click"),
reduce(acc => acc + 1, 0),
forEach(count => console.log(count))
);
@DanShappir
DanShappir / pipe_forEach_new.js
Created November 1, 2020 10:09
forEach for use with pipe function
function forEach(op) {
return async function (src) {
let index = 0;
for await (const value of src) {
if (op(value, index++) === false) {
break;
}
}
};
}
@DanShappir
DanShappir / pipe_forEach_orig.js
Created November 1, 2020 10:06
forEach for use with pipeline operator
async function forEach(src, op) {
let index = 0;
for await (const value of src) {
if (op(value, index++) === false) {
break;
}
}
}
@DanShappir
DanShappir / pipe_button_orig.js
Created November 1, 2020 10:00
Processing DOM events with pipeline operator
const myButton = document.getElementById("myButton");
fromEvent(myButton, "click")
|> reduce(#, acc => acc + 1, 0)
|> forEach(#, count => console.log(count));
@DanShappir
DanShappir / pipe_filter_new.js
Created November 1, 2020 09:58
Filter for use with pipe function
function filter(op) {
return function* (src) {
for (const value of src) {
if (op(value)) {
yield value;
}
}
};
}
@DanShappir
DanShappir / pipe_filter_orig.js
Last active November 1, 2020 09:59
Filter for use with pipeline operator
function* filter(src, op) {
for (const value of src) {
if (op(value)) {
yield value;
}
}
}
@DanShappir
DanShappir / pipe_using_pipe.js
Created November 1, 2020 09:56
Using the pipe() function to invoke a sequence of functions
const result = pipe(
numbers,
filter(v => v % 2 == 0),
map(v => v + 1),
slice(0, 3),
Array.from
);
@DanShappir
DanShappir / pipe_pipe.js
Created November 1, 2020 09:55
Implementation of piping function
function pipe(arg, ...fns) {
return fns.reduce((v, fn) => fn(v), arg);
}
@DanShappir
DanShappir / pipe_pipeline.js
Created November 1, 2020 09:54
Sequence of functions using pipeline operator
const result = numbers
|> filter(#, v => v % 2 === 0)
|> map(#, v => v + 1)
|> slice(#, 0, 3)
|> Array.from;
@DanShappir
DanShappir / pipe_func_stack.js
Created November 1, 2020 09:52
Functions called sequentially
const result = Array.from(slice(map(filter(numbers, v => v % 2 === 0), v => v + 1), 0, 3));