Skip to content

Instantly share code, notes, and snippets.

@dmitry-vsl
dmitry-vsl / index.html
Created April 1, 2018 00:11
Use mutation observer to build widget reacting on attaching/detaching to DOM
<script>
function DOMAttachWatcher(){
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if([...mutation.addedNodes].includes(el) && onAttachCb){
onAttachCb()
}
if([...mutation.removedNodes].includes(el) && onDetachCb){
function CounterView(data, handlers){
return div(null,
button({onClick: handlers.inc},
'Inc'
)
button({onClick: handlers.decAsync},
'Dec async'
),
span(null,
'Count: ', data.counter
@dmitry-vsl
dmitry-vsl / ballot.sol
Last active October 13, 2017 22:40 — forked from anonymous/ballot.sol
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.17+commit.bdeb9e52.js&optimize=true&gist=
pragma solidity ^0.4.17;
/**
* Allows one to send EIP-20 tokens to multiple addresses cheaply.
* Copyright © 2017 by ABDK Consulting https://abdk.consulting/
* Author: Mikhail Vladimirov <mikhail.vladimirov[at]gmail.com>
*/
contract BatchTokenSender {
event BatchSent();
{
"parserOptions": {
"ecmaVersion": 2017
},
"env": {
"browser": true,
"es6": true
},
"rules": {
"no-unused-vars": "off",
/*
- generate requests on timer
- if timer signals before request completed, then wait until request complete
- if timer signals multiple times before request completed then issue signle
request after getting response
Example timeline
timer: 0 1 2 2
request made: r1 r2 r3
// Весь стейт хранится в глобальной переменной. Можно использовать
// какие-нибудь классы, но мне кажется что хороший js-стиль -
// просто массивы и хэши
function getInitialState(){ return {counter: 0}; }
var AppState = getInitialState();
// Бизнес-логика. Состоит из обычных функций.
// В реальной программе функции засунуты в модули.
function dec(state){
state.counter--;
function flatten(arr){
var result = [];
function doFlatten(arr){
if(Array.isArray(arr)){
arr.forEach(doFlatten);
}else{
result.push(arr);
}
}
doFlatten(arr);
fromCallback = (fn) ->
stream = []
cb = ->
for cb in stream
cb.apply @, arguments
fn cb
stream
map = (fn) -> (stream) ->
result = []
#!/usr/bin/env runhaskell
-- На международном фестивале собрались n участников. Если взять любую подгруппу
-- из m участников, то в этой подгруппе найдутся двое, которые разговаривают на
-- одном языке.
--
-- Проверить следующее утверждение: в любой подгруппе размера k участники могут
-- объясниться друг с другом.
import Data.List
@dmitry-vsl
dmitry-vsl / tree.ts
Last active November 24, 2015 19:02
type Tree<T> = {left: Tree<T>, right: Tree<T>, value: T}
function reduceTree<T,A>(tree: Tree<T>, initialAcc: A, reducer: (A,T) => A): A{
if(tree == null){
return initialAcc;
}
var withNodeValue = reducer(initialAcc, tree.value);
var withLeftBranch = reduceTree(tree.left, withNodeValue, reducer);
var withRightBranch = reduceTree(tree.right, withLeftBranch, reducer);
return withRightBranch;