Skip to content

Instantly share code, notes, and snippets.

View jasonbellamy's full-sized avatar

Jason Bellamy jasonbellamy

View GitHub Profile
@jasonbellamy
jasonbellamy / javasscript-assessment.md
Created April 14, 2020 15:17
JavaScript Assessment

Question One:

Create a function that when given the following input:

[
    {
        a: [ 1, 2, 3, 4, 5 ]
    },
    {
@jasonbellamy
jasonbellamy / collatz.js
Created November 16, 2016 12:19
Recursive implementation of the Collatz conjecture
const collatz = (x, y = 0) => {
if (x === 1) {
return y;
}
return (x % 2) ? collatz((x * 3 + 1), y + 1) : collatz((x / 2), y + 1);
}
@jasonbellamy
jasonbellamy / swap.js
Created November 15, 2016 17:26
Some black magic with the XOR operator to swap values.
function swap(a, b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
return [a, b];
}
swap(1, 5); //=> [5, 1]
@jasonbellamy
jasonbellamy / foldWith.js
Created October 19, 2016 04:32
recursive implementation of foldWith
const foldWith = (fn, terminal, [head, ...tail]) => (
(head === void 0) ? terminal : fn(head, foldWith(fn, terminal, tail))
);
@jasonbellamy
jasonbellamy / mapWith.js
Created October 19, 2016 03:43
recursive implementation of mapWith
const mapWith = (fn, [head, ...tail]) => (
(head === void 0) ? [] : [fn(head), ...mapWith(fn, tail)]
);
@jasonbellamy
jasonbellamy / length.js
Created October 19, 2016 01:56
recursive implementation of length
const length = ([head, ...tail]) => (
(head === void 0) ? 0 : 1 + length(tail)
);
@jasonbellamy
jasonbellamy / compose.js
Created October 18, 2016 02:36
2 different implementations of compose
const compose = (...rest) => (
(z) => rest.reverse().reduce((x, y) => y(x), z)
);
const compose2 = (fn, ...rest) => (
(rest.length === 0) ? fn :(z) => compose2(...rest)(fn(z))
);
@jasonbellamy
jasonbellamy / tail-with-filter-and-index-and-length.js
Created September 3, 2016 19:39
Get the value of the last item in an array using desctructing and the arrays .filter(), index, and length
const xs = [1, 2, 3, 4, 5];
const [ x ] = xs.filter((x, index, array) => {
return index === array.length);
}); // => 5
@jasonbellamy
jasonbellamy / tail-with-reverse-shift.js
Created September 3, 2016 16:41
Get the value of the last item in an array using the arrays .reverse() method
const xs = [1, 2, 3, 4, 5];
const x = xs.reverse().shift() //=> 5
@jasonbellamy
jasonbellamy / tail-with-reverse.js
Created September 3, 2016 16:31
Get the value of the last item in an array using the arrays .reverse() method
const xs = [1, 2, 3, 4, 5];
const [ x ] = xs.reverse() //=> 5