Skip to content

Instantly share code, notes, and snippets.

View joaomilho's full-sized avatar
🐫

Yo Lulkin joaomilho

🐫
  • Choco.com
  • Berlin, Germany
View GitHub Profile
const calculatorReducer = guard({ add, subtract, multiply, divide })
const calculatorReducer = guard({
add: add
subtract: subtract
multiply: multiply
divide: divide
})
const calculatorReducer = guard({
add: (state, payload) => state + payload],
subtract: (state, payload) => state - payload],
multiply: (state, payload) => state * payload],
divide: (state, payload) => state / payload],
})
{
add: (state, payload) => state + payload],
subtract: (state, payload) => state - payload],
...
}
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)
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 June 3, 2016 23:16
A calculatorReducer in 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 June 3, 2016 23:15
A calculatorReducer in Haskell
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 February 5, 2015 09:27
Basic react + cjsx converter
'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() {
Counter = React.createClass
getInitialState: ->
count: 0
increment: ->
@setState(count: @state.count + 1)
render: ->
<div>
{ @state.count }