Last active
February 18, 2018 18:25
-
-
Save benadamstyles/c1ae1467c737a039df21d16861ffcaeb to your computer and use it in GitHub Desktop.
Examples for Rapt Medium story
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
import {Map} from 'immutable' | |
// without Rapt | |
const processUser = user => { | |
log(user) | |
let userMap = Map(user) | |
if (emailHasBeenVerified) { | |
userMap = userMap.set('verified', true) | |
} | |
syncWithServer(userMap) | |
return userMap | |
} | |
// with Rapt | |
const processUser = user => | |
rapt(user) | |
.tap(log) | |
.map(Map) | |
.mapIf(emailHasBeenVerified, u => u.set('verified', true)) | |
.tap(syncWithServer) | |
.val() |
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
import _ from 'lodash' | |
// without Rapt | |
const countItems = (shouldFilter, hugeArrayOfItems) => { | |
let arr = _.compact(hugeArrayOfItems) | |
if (shouldFilter) { | |
arr = arr.filter(expensiveFilterFunction) | |
} | |
const count = arr.length | |
console.log(`We have ${count} items`) | |
return count | |
} | |
// with Rapt | |
const countItems = (shouldFilter, hugeArrayOfItems) => | |
rapt(hugeArrayOfItems) | |
.map(compact) | |
.mapIf(shouldFilter, arr => arr.filter(expensiveFilterFunction)) | |
.map(items => items.length) | |
.tap(count => console.log(`We have ${count} items`)) | |
.val() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment