Skip to content

Instantly share code, notes, and snippets.

Avatar
🐫

Yo Lulkin joaomilho

🐫
  • Choco.com
  • Berlin, Germany
View GitHub Profile
View calculatorReducerWithGuard3.js
const calculatorReducer = guard({ add, subtract, multiply, divide })
View calculatorReducerWithGuard2.js
const calculatorReducer = guard({
add: add
subtract: subtract
multiply: multiply
divide: divide
})
View calculatorReducerWithGuard.js
const calculatorReducer = guard({
add: (state, payload) => state + payload],
subtract: (state, payload) => state - payload],
multiply: (state, payload) => state * payload],
divide: (state, payload) => state / payload],
})
View exampleConfig.js
{
add: (state, payload) => state + payload],
subtract: (state, payload) => state - payload],
...
}
View calculatorReducerWithCond.js
const typeHandlers = cond([
[equals("add"), (type, state, payload) => state + payload],
[equals("subtract"), (type, state, payload) => state - payload],
[equals("multiply"), (type, state, payload) => state * payload],
[equals("divide"), (type, state, payload) => state / payload],
[T, (type, state, payload) => state]
])
const calculatorReducer = (state, { type, payload }) =>
typeHandlers(type, state, payload)
View cond.js
const fn = cond([
[equals(0), always('water freezes at 0°C')],
[equals(100), always('water boils at 100°C')],
[T, (temp) => `nothing special happens at ${temp}°C`]
])
fn(0) //=> 'water freezes at 0°C'
fn(50) //=> 'nothing special happens at 50°C'
fn(100) //=> 'water boils at 100°C'
@joaomilho
joaomilho / calculatorReducer.js
Created Jun 3, 2016
A calculatorReducer in JS
View calculatorReducer.js
const calculatorReducer = (state, { type, payload }) {
switch (type) {
case 'add':
return state + payload
case 'subtract':
return state - payload
case 'multiply':
return state * payload
case 'divide':
return state / payload
@joaomilho
joaomilho / calculatorReducer.hs
Created Jun 3, 2016
A calculatorReducer in Haskell
View calculatorReducer.hs
calculatorReducer "add" state payload = state + payload
calculatorReducer "subtract" state payload = state - payload
calculatorReducer "multiply" state payload = state * payload
calculatorReducer "divide" state payload = state / payload
calculatorReducer _ state _ = state
-- or
calculatorReducer type state payload
| eq "add" = state + payload
@joaomilho
joaomilho / gulpfile
Created Feb 5, 2015
Basic react + cjsx converter
View gulpfile
'use strict';
var gulp = require('gulp'),
browserify = require('browserify'),
source = require('vinyl-source-stream'),
runSequence = require('run-sequence'),
react = require('gulp-react'),
cjsx = require('gulp-cjsx');
gulp.task('app', function() {
View counter.cjsx
Counter = React.createClass
getInitialState: ->
count: 0
increment: ->
@setState(count: @state.count + 1)
render: ->
<div>
{ @state.count }