Skip to content

Instantly share code, notes, and snippets.

View shotamatsuda's full-sized avatar

Shota Matsuda shotamatsuda

  • Takram
  • Tokyo, Japan
View GitHub Profile
export function handleAction (action) {
const args = action.payload || []
switch (action.type) {
case DO_SOMETHING:
return doSomething(...args)
default:
break
}
}
@shotamatsuda
shotamatsuda / Semaphore.js
Last active February 5, 2017 11:16
Promise based semaphore
import { Namespace } from '../core/Namespace'
export const internal = Namespace('Semaphore')
class Task {
constructor(semaphore, callback) {
const promises = [
new Promise((resolve, reject) => {
this.resolve = resolve
this.reject = reject
export function Namespace(name) {
const symbol = Symbol(name)
return function namespace(object, init = () => ({})) {
if (object[symbol] === undefined) {
object[symbol] = init()
}
return object[symbol]
}
}
class Multiplier {
constructor(value) {
const coefficient = value
return {
by(other) {
return other * coefficient
},
}
}
class Base {
constructor() {
const privateVariable
this._protectedVariable
this.publicVariable
function privateFunction() {}
}
_protectedFunction() {}
function Base() {
var privateVariable;
this._protectedVariable;
this.publicVariable;
function privateFunction() {}
}
Base.prototype._protectedFunction = function () {};
Base.prototype.publicFunction = function () {};
import Namespace from '../core/Namespace'
const self = undefined
const internal = Namespace()
class Task {
constructor(semaphore, callback) {
const promises = [
new Promise((resolve, reject) => {
this.resolve = resolve
#include <array>
#include "ratio.h"
template <
class A_, class B_, class C_,
class D_, class E_, class F_,
class G_, class H_, class I_
>
struct RatioMatrix {
#include <ratio>
namespace detail {
template <class... Args>
struct RatioAdd;
template <class R1, class R2>
struct RatioAdd<R1, R2> {
using Type = std::ratio_add<R1, R2>;
@shotamatsuda
shotamatsuda / main.cpp
Created July 4, 2016 14:03
Concurrent partition
// Given
const auto size = ...;
auto begin = ...;
std::list<std::future<void>> futures;
const auto concurrency = std::thread::hardware_concurrency();
const auto partition = std::imaxdiv(size, concurrency);
for (int i{}; i < concurrency; ++i) {
const auto end = begin + partition.quot + (i < partition.rem);
futures.emplace_back(std::async([this, begin, end]() {