Skip to content

Instantly share code, notes, and snippets.

View lubien's full-sized avatar
💭
Teaching people 🔥🦩 LiveView 🔥🦩

Lubien lubien

💭
Teaching people 🔥🦩 LiveView 🔥🦩
View GitHub Profile
@lubien
lubien / sane-binary-functions.js
Created June 24, 2017 03:27
Meeting Lambda #12: Curry or Partial? - Sane Binary Functions
const add = (x, y) => x + y
const sub = (x, y) => x - y
console.log('10 + 3 - 4 =', sub( add(10, 3), 4 ) )
@lubien
lubien / why-did-u-even-curry.js
Created June 24, 2017 03:20
Meeting Lambda #12: Curry or Partial? - Why Did You Even Curry?
const add = x => y => x + y
const sub = x => y => x - y
console.log('10 + 3 - 4 =', sub( add(10)(3) )(4) )
@lubien
lubien / max-partial.js
Created June 24, 2017 03:06
Meeting Lambda #12: Curry or Partial? - Max Partial
// if the other argument is lower than 0, return 0
const atLeastZero = Math.max.bind(null, 0)
console.log(atLeastZero(10)) // 10
console.log(atLeastZero(-10)) // 0
console.log(atLeastZero(0)) // 0
@lubien
lubien / max-curry-helper.js
Created June 24, 2017 02:39
Meeting Lambda #12: Curry or Partial? - Max Curry Helper
const curry = fn =>
// if this function takes one argument
fn.length === 1
// it's already curry
? fn
// otherwise let's create a curry version
// return a function that takes one argument
// when we receive it we bind it to fn to create a new
// function then we curry the rest recursively
: x => curry(fn.bind(null, x))
@lubien
lubien / max-manual-curry.js
Created June 24, 2017 02:30
Meeting Lambda #12: Curry or Partial? - Max Manual Curry
const maxCurry = x => y =>
Math.max(x, y)
// if the other argument is lower than 0, return 0
const atLeastZero = maxCurry(0)
console.log(atLeastZero(10)) // 10
console.log(atLeastZero(-10)) // 0
console.log(atLeastZero(0)) // 0
@lubien
lubien / letter-to-message-partial.js
Created June 24, 2017 02:21
Meeting Lambda #12: Curry or Partial? - Letter to Message Partial
const letterToMessage = (letter, index) =>
`${letter} is at position ${index}`
const letterIsAlwaysZ = letterToMessage.bind(null, 'z')
const letters = ['a', 'b', 'c', 'd', 'e']
const messages = letters.map(letterToMessage)
// Using the full function
console.log(letterToMessage('a', 0)) // a is at position 0
@lubien
lubien / prop-eq-partial.js
Created June 24, 2017 01:07
Meeting Lambda #11: Partial Appplication - Prop Equals Partial
const partial = (fn, ...args) =>
fn.bind(null, ...args)
const propEq = (propName, expectedValue, object) =>
object[propName] === expectedValue
const users =
[ {username: 'lubien', online: false}
, {username: 'rian-vergara', online: true }
, {username: 'leochrisis', online: true }
@lubien
lubien / map-prop-partial.js
Created June 24, 2017 01:01
Meeting Lambda #11: Partial Appplication - Map Prop
const partial = (fn, ...args) =>
fn.bind(null, ...args)
const prop = (propName, object) =>
object[propName]
const users =
[ {username: 'lubien', name: 'João' }
, {username: 'rian-vergara', name: 'Rian' }
, {username: 'leochrisis', name: 'Leonardo'}
@lubien
lubien / get.js
Last active June 23, 2017 23:43 — forked from kaueDM/get.js
const getAll = userUID =>
database
.ref('user_data')
.child(userUID)
.child('customers')
// exemplo de uso:
getAll(myUserId).on('value', snap => {
const value = snap.val()
@lubien
lubien / partial-helper.js
Last active June 24, 2017 01:14
Meeting Lambda #11: Partial Application - Partial Helper
const partial = (fn, ...args) =>
fn.bind(null, ...args)
const add3 = (x, y, z) => x + y + z
const addOnePlusTwo = partial(add3, 1, 2)
console.log(addOnePlusTwo(1)) // 4
console.log(addOnePlusTwo(10)) // 13