Implement basic list operations.
In functional languages list operations like length
, map
, and
reduce
are very common. Implement a series of basic list operations,
without using existing functions.
The precise number and names of the operations to be implemented will be track dependent to avoid conflicts with existing names, but the general operations you will implement include:
append
(given two lists, add all items in the second list to the end of the first list);concatenate
(given a series of lists, combine all items in all lists into one flattened list);filter
(given a predicate and a list, return the list of all items for which predicate(item)
is True);length
(given a list, return the total number of items within it);map
(given a function and a list, return the list of the results of applying function(item)
on all items);foldl
(given a function, a list, and initial accumulator, fold (reduce) each item into the accumulator from the left using function(accumulator, item)
);foldr
(given a function, a list, and an initial accumulator, fold (reduce) each item into the accumulator from the right using function(item, accumulator)
);reverse
(given a list, return a list with all the original items, but in reversed order);Go through the setup instructions for TypeScript to install the necessary dependencies:
https://exercism.io/tracks/typescript/installation
Install assignment dependencies:
$ yarn install
Execute the tests with:
$ yarn test
In the test suites all tests but the first have been skipped.
Once you get a test passing, you can enable the next one by changing xit
to
it
.
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
import List from './list-ops'
describe('append entries to a list and return the new list', () => {
it('empty lists', () => {
const list1 = new List()
const list2 = new List()
expect(list1.append(list2)).toEqual(new List())
})
xit('empty list to list', () => {
const list1 = new List([1, 2, 3, 4])
const list2 = new List<number>()
expect(list1.append(list2)).toEqual(list1)
})
xit('non-empty lists', () => {
const list1 = new List([1, 2])
const list2 = new List([2, 3, 4, 5])
expect(list1.append(list2).values).toEqual([1, 2, 2, 3, 4, 5])
})
})
describe('concat lists and lists of lists into new list', () => {
xit('empty list', () => {
const list1 = new List()
const list2 = new List([])
expect(list1.concat(list2).values).toEqual([])
})
xit('list of lists', () => {
const list1 = new List([1, 2])
const list2 = new List([3])
const list3 = new List([])
const list4 = new List([4, 5, 6])
const listOfLists = new List([list2, list3, list4])
expect(list1.concat(listOfLists).values).toEqual([1, 2, 3, 4, 5, 6])
})
})
describe('filter list returning only values that satisfy the filter function', () => {
xit('empty list', () => {
const list1 = new List([])
expect(list1.filter((el: number) => el % 2 === 1).values).toEqual([])
})
xit('non empty list', () => {
const list1 = new List([1, 2, 3, 5])
expect(list1.filter((el: number) => el % 2 === 1).values).toEqual([1, 3, 5])
})
})
describe('returns the length of a list', () => {
xit('empty list', () => {
const list1 = new List()
expect(list1.length()).toEqual(0)
})
xit('non-empty list', () => {
const list1 = new List([1, 2, 3, 4])
expect(list1.length()).toEqual(4)
})
})
describe('returns a list of elements whose values equal the list value transformed by the mapping function', () => {
xit('empty list', () => {
const list1 = new List()
expect(list1.map((el: number) => ++el).values).toEqual([])
})
xit('non-empty list', () => {
const list1 = new List([1, 3, 5, 7])
expect(list1.map((el: number) => ++el).values).toEqual([2, 4, 6, 8])
})
})
describe('folds (reduces) the given list from the left with a function', () => {
xit('empty list', () => {
const list1 = new List()
expect(list1.foldl((acc: number, el: number) => el / acc, 2)).toEqual(2)
})
xit('division of integers', () => {
const list1 = new List([1, 2, 3, 4])
expect(list1.foldl((acc: number, el: number) => el / acc, 24)).toEqual(64)
})
})
describe('folds (reduces) the given list from the right with a function', () => {
xit('empty list', () => {
const list1 = new List()
expect(list1.foldr((acc: number, el: number) => el / acc, 2)).toEqual(2)
})
xit('division of integers', () => {
const list1 = new List([1, 2, 3, 4])
expect(list1.foldr((acc: number, el: number) => el / acc, 24)).toEqual(9)
})
})
describe('reverse the elements of a list', () => {
xit('empty list', () => {
const list1 = new List()
expect(list1.reverse().values).toEqual([])
})
xit('non-empty list', () => {
const list1 = new List([1, 3, 5, 7])
expect(list1.reverse().values).toEqual([7, 5, 3, 1])
})
})
type BooleanCallbackFunction = (...args: any[]) => boolean;
type AccumulatorFunction = (accumulator: any, item: any) => void;
class List<T> {
public values: T[] = [];
private count: number = 0;
constructor(...items: Array<T>){
if(items.length === 0){return;}
this.flattenArray(items).forEach((item)=>{this.add(item);});
}
public length(): number {return this.count;}
public concatenate(...items: List<T>[]): List<T>{
if(items.length === 0){return this;}
items.forEach(elementList=>{this.append(elementList);});
return this;
}
public append(list: List<T>): List<T> {
if (list.length() === 0){return this;}
list.values.forEach(el=>{this.add(el);})
return this;
}
public map(afunc: Function){
const applied = new List<T>();
this.values.forEach(element => {
const UpdatedElement:T = afunc(element);
applied.add(UpdatedElement);
});
return applied;
}
public filter(afunc: BooleanCallbackFunction) {
const applied = new List<T>();
this.values.forEach(element => {
const isElement: boolean = afunc(element);
if (isElement === true) {applied.add(element);}
});
return applied;
}
// public
public print(): void {
console.log(this.values);
console.log(this.count);
}
protected add(item: T) {
this.values.push(item);
this.count++;
}
protected flattenArray(items: Array<T>): T[] {
const flattenedArray = ([] as T[]).concat(...items);
return flattenedArray;
}
public foldl(fn: (acc: any, el: any) => any, init: any): any{
const [head, ...tails] = this.values;
return head ? new List(tails).foldl(fn, fn(init, head)) : init
}
public foldr(fn: (acc: any, el: any) => any, init: any): any {
const [head, ...tail] = this.values
return head ? fn(new List(tail).foldr(fn, init), head) : init
}
}
let list = new List<number>();
//
let list2 = (
new List("January", "Febuary", "March")
.append(new List("April", "May", "June", "July", "August"))
.concatenate(
new List("September", "October"),
new List("November", "December"))
.map((element: string) => {
return element.toUpperCase();
})
);
let fodled = list2.foldl((acc: string, el: string) => {
return acc + " " + el + "\n";
}, "");
const fodred = list2.foldr((acc: string, el: string) => {
return acc + " " + el + "\n";
}, "");
console.log(fodled);
console.log(fodred);
I could have really shortened this codebase using the foldl method throughout. I only came to that conclusion after seeing arachnid's code. I would say you should check out her stuff if that's what you're looking for.
Level up your programming skills with 3,105 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments