Skip to content

Instantly share code, notes, and snippets.

@benadamstyles
Last active February 18, 2018 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benadamstyles/c1ae1467c737a039df21d16861ffcaeb to your computer and use it in GitHub Desktop.
Save benadamstyles/c1ae1467c737a039df21d16861ffcaeb to your computer and use it in GitHub Desktop.
Examples for Rapt Medium story
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()
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