Skip to content

Instantly share code, notes, and snippets.

@oncomouse
Created June 5, 2019 16:26
Show Gist options
  • Save oncomouse/0b86a0c514385b0419895018b5ec9501 to your computer and use it in GitHub Desktop.
Save oncomouse/0b86a0c514385b0419895018b5ec9501 to your computer and use it in GitHub Desktop.
import * as R from 'ramda';
/*
* DHSI 2019, Day 1 Exercises
*
* Here are a few programming challenges that ask you to write some functions
* using what we learned in class today. All of these can be solved without
* using Ramda, but they can also be solved more easily using Ramda's functions.
*
* You may need to consult Ramda's documentation (https://ramdajs.com/docs/) to
* figure out what all these functions do.
*
* If you want an extra special challenge, you can use Ramda to write all of
* these in point-free style
* (http://randycoulman.com/blog/2016/06/21/thinking-in-ramda-pointfree-style/),
* which is a way to earn some solid functional programming geek cred.
*
* Good luck!
*
*/
/*
* Exercise 1 - Lots of Smiles
*
* Write a function named `lotsOfSmiles` (and `export` it) that takes an array
* of numbers and returns a string containing a number of smile emoji (πŸ˜€)
* equal to the sum of all the numbers.
*
*/
const lotsOfSmilesRamda = R.compose(
R.join(''),
R.times(R.always('πŸ˜€')),
R.sum
);
const lotsOfSmilesRamda2 = R.compose(
R.join(''),
R.repeat('πŸ˜€'),
R.sum
);
const lotsOfSmilesNoRamda = (array) => Array(
array.reduce((sum, item) => sum + item, 0),
).fill('πŸ˜€').join('');
const lotsOfSmilesIterative = (array) => {
let output = '';
const sum = array.reduce((runningTotal, item) => runningTotal + item, 0);
for (let i = 0; i < sum; i++) {
output = output.concat('πŸ˜€');
}
return output;
}
export const lotsOfSmiles = lotsOfSmilesRamda2;
/*
* Exercise 2 - Find Longest Word
*
* Write a function named `longestWord` (and `export` it) that takes a string
* as a parameter and returns the longest word in the sentence. If there are
* two or more words in the sentence of the same length, return the first
* alphabetically.
*
*/
const longestWordRamda1 = R.compose(
R.reduce((acc, cur) => {
if(cur.length === acc.length) {
return cur < acc ? cur : acc;
}
return cur.length > acc.length ? cur : acc;
}, ''),
R.split(/\W+/)
);
const longestWordRamda2 = R.compose(
R.prop(0),
R.sortWith([
R.descend(R.length),
R.comparator(R.lt)
]),
R.split(/\W+/)
);
const longestWordNoRamda = (string) => {
return string.split(/\W+/).sort((a, b) => {
if(a.length === b.length) {
if(a < b) {
return -1;
}
if(a === b) {
return 0;
}
return 1;
}
return b.length - a.length
})[0];
}
const longestWordNoRamda2 = (string) => {
return string.split(/\W+/).reduce((acc, cur) => {
if (cur.length === acc.length) {
return cur < acc ? cur : acc;
}
return cur.length > acc.length ? cur : acc;
}, '');
}
export const longestWord = longestWordRamda2; //(sentence) => '';
/*
* Exercise 3 - Reverse Strings in an Array
*
* Write a function named `reverseStrings` (and `export` it) that will take an
* array and will, for each string in the array, reverse the string. The
* function should not do anything to values that are not strings. Nor should
* it throw an Error if it encounters a string.
*
*/
const reverseStringsRamda = R.map(R.ifElse(
R.is(String),
R.reverse,
R.identity
));
const reverseStringsNoRamda = (array) => array.map(item => {
if(typeof item === 'string') {
return item.split('').reverse().join('');
}
return item;
});
export const reverseStrings = reverseStringsNoRamda;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment