Skip to content

Instantly share code, notes, and snippets.

@gohkhoonhiang
Last active June 10, 2023 12:57
Show Gist options
  • Save gohkhoonhiang/c6b0f664b221f8fcb8e78e905dbfa961 to your computer and use it in GitHub Desktop.
Save gohkhoonhiang/c6b0f664b221f8fcb8e78e905dbfa961 to your computer and use it in GitHub Desktop.
Ruby Meetup July 2023 Lightning Talk
function x1(value) {
return (
Object.is(value, undefined) ||
Object.is(value, null)
);
}
function x2(val1, val2, val3) {
return (
val1 >= val2 &&
val1 <= val3
);
}
function x3(array1) {
return array1.filter((ele) => {
return !Object.is(ele, null);
});
}
function x4(array1) {
return [array1 || []].flat();
}
function x5(array1, cond) {
return array1.reduce((res, ele) => {
if (cond(ele)) {
res[0].push(ele);
} else {
res[1].push(ele);
}
return res;
}, [[], []]);
}
function x6(hash, keys) {
return keys.reduce((h, key) => {
h[key] = hash[key];
return h;
}, {});
}
function x7(str, other) {
return str.split('').reduce((a, c) => {
const lastChar = a[a.length - 1];
if (
!lastChar ||
lastChar !== c ||
!c.match(other)
) {
a.push(c);
}
return a;
}, []).join('');
}
def x1(value)
!defined?(value) || value == nil
end
def x2(val1, val2, val3)
val1 >= val2 &&
val1 <= val3
end
def x3(array1)
array1.filter do |ele|
ele != nil
end
end
def x4(array1)
[array1 || []].flatten
end
def x5(array1, cond)
array1.reduce([[], []]) do |res, ele|
if cond.(ele)
res[0].push(ele)
else
res[1].push(ele)
end
res
end
end
def x6(hash, keys)
keys.reduce({}) do |h, key|
h[key] = hash[key]
h
end
end
def x7(str, other)
str.split('').reduce([]) do |a, c|
lastChar = a.last
if !lastChar ||
lastChar != c ||
!c.match(other)
a.push(c)
end
a
end.join('')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment