| map | flatMap | fold | |
|---|---|---|---|
| Some(x) |
This file contains hidden or 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
| const getUserFromLocalStorage = keyName => { | |
| const str = localStorage.getItem(keyName); | |
| try { | |
| return Right(JSON.parse(str)); | |
| } catch (e) { | |
| return Left("Invalid or Corrupted User Data"); | |
| } | |
| }; |
This file contains hidden or 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
| const transformCage = () => { /* ... */ }; | |
| const handleError = e => "Invalid or Corrupted User Data"; | |
| const handleSuccess = data => "Success"; | |
| const doSomethingWithUserData = () => | |
| getUserFromLocalStorage('nickCage') | |
| .map(transformCage) | |
| .fold(handleError, handleSuccess); |
This file contains hidden or 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
| const transformCage = () => { /* ... */ }; | |
| const handleError = e => "failed to parse Cage"; | |
| const handleSuccess = data => "Success"; | |
| const ifEmpty = () => "Cage does not exist!"; | |
| const doSomethingWithUserData = () => | |
| getUserFromLocalStorage('nickCage') | |
| .fold(handleError, | |
| data => | |
| data.map(transformCage) |
This file contains hidden or 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
| const getFeatures = data => { | |
| if (data && data.listing && data.listing.features) { | |
| const { bedrooms, bathrooms } = data.listing.features; | |
| const beds = bedrooms && bedrooms.value || 0; | |
| const baths = bathrooms && bathrooms.value || 0; | |
| if (beds === 0 && baths === 0) return null; | |
| return { | |
| bedrooms: beds, | |
| bathrooms: baths | |
| }; |
This file contains hidden or 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
| /* Option types */ | |
| const Option = x => (x === undefined || x === null) ? None : Some(x); | |
| const Some = x => ({ | |
| filter: f => f(x) ? Some(x) : None, | |
| map: f => Some(f(x)), | |
| fold: (ifEmpty, f) => f(x), | |
| getOrElse: defaultValue => x | |
| }); |
This file contains hidden or 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
| const getUserFromLocalStorage = keyName => { | |
| const str = localStorage.getItem(keyName); | |
| try { | |
| return Right(Option(JSON.parse(str))); | |
| } catch (e) { | |
| return Left("Invalid or Corrupted User Data"); | |
| } | |
| }; |
This file contains hidden or 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
| const getBedrooms = data => { | |
| const bedrooms = data.listing && data.listing.features && data.listing.features.bedrooms; | |
| if (bedrooms) { | |
| return bedrooms.value; | |
| } | |
| return 0; | |
| }; |
| Some(x) | None | |
|---|---|---|
| map | map<V>(f: (value: U) => V): Some<V> |