Last active
March 14, 2023 01:26
-
-
Save Wintus/81a825762239dc4b821e8afbc97ca7f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
original Tweet: https://twitter.com/t__keshi/status/1635267214008897537