Skip to content

Instantly share code, notes, and snippets.

View dewey92's full-sized avatar
🌴

Jihad D. Waspada dewey92

🌴
View GitHub Profile
@dewey92
dewey92 / home.js
Created March 3, 2016 07:17
Vue router
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
Vue.use(require('vue-resource'))
const router = new VueRouter({
history : true,
saveScrollPosition: true
});
@dewey92
dewey92 / 01-dependencies.php
Last active February 20, 2024 11:37
Slim 3 controller
<?php
$c = new \App\Base\Container();
/**
* Register the CallableResolver we just wrote
* to overwrite the default resolving behaviour of the Slim app
*/
$c['callableResolver'] = function ($c) {
return new \App\Base\CallableResolver($c); // See number 04-CallableResolver.php
@dewey92
dewey92 / compose.js
Created March 26, 2017 04:06
compose
const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x)
@dewey92
dewey92 / compose-steps.js
Last active March 26, 2017 04:42
Compose step by step
const wedhus = compose(hargaKambing, diskon10, times3, add2)
// sama dengan
const wedhus = x => [hargaKambing, diskon10, times3, add2].reduceRight((acc, fn) => fn(acc), x)
// dan
const hasil = wedhus(77)
// sama dengan
const hasil = [hargaKambing, diskon10, times3, add2].reduceRight((acc, fn) => fn(acc), 77)
const wedhus = x => [hargaKambing, diskon10, times3, add2].reduceRight((acc, fn) => fn(acc), x)
wedhus(77) = [hargaKambing, diskon10, times3, add2].reduceRight((acc, fn) => fn(acc), 77)
// STEP BY STEP
// Iterasi 1: fn = add2, acc = 77
wedhus(77) = [hargaKambing, diskon10, times3].reduceRight((acc, fn) => add2(77), 77)
// Iterasi 2: fn = times3, acc = add2(77)
wedhus(77) = [hargaKambing, diskon10].reduceRight((acc, fn) => times3(add2(77)), 77)
const map = (fn, arr) => {
const [head, ...tail] = arr
if (tail[0] === undefined) return [fn(head)]
return [fn(head), ...map(fn, tail)]
}
const filter = (fn, arr) => {
const [head, ...tail] = arr
if (tail[0] === undefined) return []
if (fn(head)) {
return [head, ...filter(fn, tail)]
} else {
return [...filter(fn, tail)]
}
const reduce = (fn, init, arr) => {
const [head, ...tail] = arr
if (tail[0] === undefined) return fn(init, head)
return reduce(fn, fn(init, head), tail)
}
const length = arr => {
const [head, ...tail] = arr
if (tail[0] === undefined) return 1
return 1 + length(tail)
}
const Just = value => ({
map: f => Just(f(value)),
bind(f) { return this.map(f).join() },
join: () => value,
isJust: () => true,
isNothing: () => false,
orJust: _ => value
})
const Nothing = () => ({