Skip to content

Instantly share code, notes, and snippets.

View jdrew1303's full-sized avatar
probably drinking coffee

James Drew jdrew1303

probably drinking coffee
View GitHub Profile
@jdrew1303
jdrew1303 / GameState.elm
Created April 13, 2018 01:22 — forked from rupertlssmith/GameState.elm
Exploring State Machines with phantom types in Elm
module GameState
exposing
( Game(..)
, GameDefinition
, PlayState
, loading
, updateGameDefinition
, updatePlayState
, updateScore
, toReady
@jdrew1303
jdrew1303 / 1_Laravel_state-machine.md
Created April 13, 2018 01:20 — forked from iben12/1_Laravel_state-machine.md
Laravel: State-machine on Eloquent Model

Implementing State Machine On Eloquent Model*

* Update (12.09.2017): I have improved the trait so that it can be used with objects other than Eloquent Models.

Some days ago I came across a task where I needed to implement managable state for an Eloquent model. This is a common task, actually there is a mathematical model called "Finite-state Machine". The concept is that the state machine (SM) "can be in exactly one of the finite number of states at any given time". Also changing from one state to another (called transition) depends on fulfilling the conditions defined by its configuration.

Practically this means you define each state that the SM can be in and the possible transitions. To define a transition you set the states on which the transition can be applied (initial conditions) and the only state in which the SM should be after the transition.

That's the theory, let's get to the work.

@jdrew1303
jdrew1303 / 01.FiniteAuditTrail.README.md
Created April 13, 2018 01:19 — forked from tortuetorche/01.FiniteAuditTrail.README.md
FiniteAuditTrail Trait For Laravel 4 with Example. Support for keeping an audit trail for any state machine, borrowed from the Ruby StateMachine audit trail gem.

FiniteAuditTrail Trait

Support for keeping an audit trail for any state machine, borrowed from the Ruby StateMachine audit trail gem. Having an audit trail gives you a complete history of the state changes in your model. This history allows you to investigate incidents or perform analytics, like: “How long does it take on average to go from state a to state b?”, or “What percentage of cases goes from state a to b via state c?”

Requirements

  1. PHP >= 5.4
  2. Laravel 4 framework
@jdrew1303
jdrew1303 / gist:8f2ba3d23ed7583b40672d28e24e26cd
Created April 13, 2018 01:18 — forked from smurphy8/gist:b0c8040284d3b874e6c5
Using IxMonad to create an IO StateMachine
@jdrew1303
jdrew1303 / tmux-cheatsheet.markdown
Created April 13, 2018 01:02 — forked from MohamedAlaa/tmux-cheatsheet.markdown
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@jdrew1303
jdrew1303 / hcr.js
Created April 12, 2018 12:32 — forked from thure/hcr.js
Heliocentric coordinates from Kepler orbit and time
var moment = require('moment');
module.exports = function(/* orbit, [Date, time String], [format String], [etc] */){
// Takes an orbit definition of the kind used in GEOF stellar-objects and
// remaining arguments as moment arguments, returning heliocentric
// rectangular coordinates in α's unit (probably AU).
// TODO: Does not yet take into account changes in orbit over time.
// TODO: Converts between degrees and radians too often, I think.
@jdrew1303
jdrew1303 / Expecations in PEG.js error
Created November 26, 2017 04:07 — forked from mstefaniuk/Expecations in PEG.js error
Prividing expectations in PEG.js generated parser (last lines)
/*
* The parser is now in one of the following three states:
*
* 1. The parser successfully parsed the whole input.
*
* - |result !== null|
* - |pos === input.length|
* - |rightmostMatchFailuresExpected| may or may not contain something
*
* 2. The parser successfully parsed only a part of the input.
@jdrew1303
jdrew1303 / grammar.pegjs
Created November 26, 2017 04:00 — forked from nerevar/grammar.pegjs
PEG.js filters grammar
/*
* PEG.js грамматика парсера фильтра результатов сравнения метрик
* Как обновлять грамматику:
* 1. Скопипастить исходник в http://pegjs.org/online
* 2. Указать Parser variable: window.filter_parser
* 3. Download parser
* 4. Минифицировать скачанный js код через: http://jscompress.com
* 5. Обновить файл filter_parser.pegjs.min.js
* 6. Запустить js unit-тесты: mocha filter_parser.pegjs.test.js
* 7. Обновить исходник в этом файле filter_parser.pegjs
@jdrew1303
jdrew1303 / search.peg
Created November 26, 2017 03:59 — forked from joemfb/search.peg
PEG.js search grammar
{
function joinNested(x) {
return x.map(function(y) {
return Array.isArray(y) ? joinNested(y) : y
}).join('')
}
}
// match all rules, consuming the entire input
root = a:all EOF { return a }
@jdrew1303
jdrew1303 / gist:8b7885a6bf7bcfd7eb1d94cc95485e26
Created November 26, 2017 03:58 — forked from dmajda/gist:1926638
Semantic predicates and label visibility in PEG.js
/*
* Task: Parse even numbers in PEG.js (http://pegjs.majda.cz).
*
* Solution: Let's parse all numbers and reject odd ones using a semantic
* predicate -- an arbitrary piece of code that returns true (meaning "continue
* parsing") or false (meaning "halt parsing").
*
* This solution wouldn't work before commit a2af1fe612 because predicates
* didn't have access to labeled expressions in the grammar as variables
* (without ugly workarounds). But they have the access now and the solution