Skip to content

Instantly share code, notes, and snippets.

@YuraKostin
Last active December 30, 2020 13:10
Show Gist options
  • Save YuraKostin/d71c26e06d0340e2353f05a531a41444 to your computer and use it in GitHub Desktop.
Save YuraKostin/d71c26e06d0340e2353f05a531a41444 to your computer and use it in GitHub Desktop.
First using of fp-ts
import {fold, Monoid} from "fp-ts/Monoid";
const enum EventProperties {
eventLabel = 'eventLabel',
eventContext = 'eventContext',
eventContent = 'eventContent',
eventLocation = 'eventLocation'
}
type EventAttributes = {
[EventProperties.eventLabel]: string,
[EventProperties.eventContent]: string,
[EventProperties.eventContext]: string,
[EventProperties.eventLocation]: string
};
const eventAttributesPropertyMonoid: Monoid<string> = {
empty: '',
concat: (x, y) => {
return x.length ? x : y;
}
};
const concatEventAttributesProperties = (x: string, y: string): string => eventAttributesPropertyMonoid.concat(x, y);
const eventAttributesMonoid: Monoid<EventAttributes> = {
empty: {
eventLabel: '',
eventContent: '',
eventContext: '',
eventLocation: '',
},
concat: (x, y) => {
return {
eventLabel: concatEventAttributesProperties(x.eventLabel, y.eventLabel),
eventContent: concatEventAttributesProperties(x.eventContent, y.eventContent),
eventContext: concatEventAttributesProperties(x.eventContext, y.eventContext),
eventLocation: concatEventAttributesProperties(x.eventLocation, y.eventLocation),
};
}
};
const data: Array<EventAttributes> = [
{
eventLabel: '',
eventContent: 'b',
eventContext: '',
eventLocation: ''
},
{
eventLabel: 'a',
eventContent: 'b',
eventContext: 'c',
eventLocation: ''
},
{
eventLabel: '',
eventContent: '',
eventContext: '',
eventLocation: ''
},
{
eventLabel: 'a1',
eventContent: '',
eventContext: '',
eventLocation: ''
}
];
const result = fold(eventAttributesMonoid)(data);
console.log(result);
import {fold, getStructMonoid, Monoid} from "fp-ts/Monoid";
const enum EventProperties {
eventLabel = 'eventLabel',
eventContext = 'eventContext',
eventContent = 'eventContent',
eventLocation = 'eventLocation'
}
type EventAttributes = {
[EventProperties.eventLabel]: string,
[EventProperties.eventContent]: string,
[EventProperties.eventContext]: string,
[EventProperties.eventLocation]: string
};
const eventAttributesPropertyMonoid: Monoid<string> = {
empty: '',
concat: (x, y) => {
return x.length ? x : y;
}
};
const eventAttributesMonoid = getStructMonoid<EventAttributes>({
eventLabel: eventAttributesPropertyMonoid,
eventContent: eventAttributesPropertyMonoid,
eventContext: eventAttributesPropertyMonoid,
eventLocation: eventAttributesPropertyMonoid,
});
const data: Array<EventAttributes> = [
{
eventLabel: '',
eventContent: 'b',
eventContext: '',
eventLocation: ''
},
{
eventLabel: 'a',
eventContent: 'b',
eventContext: 'c',
eventLocation: ''
},
{
eventLabel: '',
eventContent: '',
eventContext: '',
eventLocation: ''
},
{
eventLabel: 'a1',
eventContent: '',
eventContext: '',
eventLocation: ''
}
];
const result = fold(eventAttributesMonoid)(data);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment