Skip to content

Instantly share code, notes, and snippets.

@Wintus
Last active March 14, 2023 01:26
Show Gist options
  • Save Wintus/81a825762239dc4b821e8afbc97ca7f5 to your computer and use it in GitHub Desktop.
Save Wintus/81a825762239dc4b821e8afbc97ca7f5 to your computer and use it in GitHub Desktop.
type Apartment = {
meterToStation: number;
ageOfBuilding: number;
options: string[];
};
type Optional<T> = [] | [T];
const apartment = {
meterToStation: 400,
ageOfBuilding: 4,
options: ["ShoeLocker", "AutoLock"],
} satisfies Apartment;
const isNearStation = (apartment: Apartment) => apartment.meterToStation < 1000;
const isNewBuilding = (apartment: Apartment) => apartment.ageOfBuilding < 5;
const isAutoLock = (apartment: Apartment) =>
apartment.options.includes("AutoLock");
const toOptional: <S, T>(
predicate: (_: S) => boolean,
value: T
) => (s: S) => Optional<T> = (predicate, value) => (s) =>
predicate(s) ? [value] : [];
const tags: string[] = [
toOptional(isNearStation, "駅近"),
toOptional(isNewBuilding, "築浅"),
toOptional(isAutoLock, "セキュリティ充実"),
].flatMap((f) => f(apartment));
@Wintus
Copy link
Author

Wintus commented Mar 14, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment