Most recent activity
-
-
-
First off thanks for the pattern launcher @crazysaem , the title of this thread is very fitting!
I may have some detection improvement, based on some testing in the web ide. At the very least it reduces the code a bit.const CIRCLE_RADIUS = 25; const CIRCLE_RADIUS_2 = Math.pow(CIRCLE_RADIUS, 2); let positions = []; const getPattern = positions => { let circles = [ {x: 25, y: 25, i: 0}, {x: 87, y: 25, i: 1}, {x: 150, y: 25, i: 2}, {x: 25, y: 87, i: 3}, {x: 87, y: 87, i: 4}, {x: 150, y: 87, i: 5}, {x: 25, y: 150, i: 6}, {x: 87, y: 150, i: 7}, {x: 150, y: 150, i: 8} ]; return positions.reduce((pattern, p, i, arr) => { const idx = circles.findIndex(c => { const dx = p.x > c.x ? p.x - c.x : c.x - p.x; if (dx > CIRCLE_RADIUS) { return false; } const dy = p.y > c.y ? p.y - c.y : c.y - p.y; if (dy > CIRCLE_RADIUS) { return false; } if (dx + dy <= CIRCLE_RADIUS) { return true; } return dx * dx + dy * dy <= CIRCLE_RADIUS_2; }); if (idx >= 0) { pattern += circles[idx].i; circles.splice(idx, 1); } if (circles.length === 0) { arr.splice(1); } return pattern; }, ''); }; const dragHandler = position => { positions.push(position); if (position.b === 0 || positions.length >= 200) { console.log('pattern', getPattern(positions)); positions = []; } }; Bangle.on("drag", dragHandler);
-
Here is a semi complete map poly of a older project of mine.
'use strict'; class Map { constructor (keyVal) { this.clear(); if (Array.isArray(keyVal)) { keyVal.forEach(kv => this._keys.push(kv[0]) && this._values.push(kv[1])); } } get size () { return this._keys.length; } has (key) { return this._keys.includes(key); } get (key) { return this._values[this._keys.indexOf(key)]; } set (key, value) { let i = this._keys.indexOf(key); if (i !== -1) { this._values[i] = value; } else { this._keys.push(key); this._values.push(value); } } delete (key) { let i = this._keys.indexOf(key); if (i === -1) { return false; } this._keys.splice(i, 1); this._values.splice(i, 1); } keys () { return this._keys; } values () { return this._values; } clear () { this._keys = []; this._values = []; } entries () { return this._iterator(); } _iterator () { let i = 0; return { next: () => i < this._keys.length ? {value: [this._keys[i], this._values[i++]], done: false} : {done: true} }; } forEach (callback, scope = null) { this._keys.forEach(key => callback.call(scope, this.get(key), key, this)); } }
Amazingly close to what @Gordon already had :D
-
Well sorry.. its a quick hack. Its a function to run promise chains in sequence with optional mapper functions for the resolves. And the resulting resolve returns the changed map (key: original promise, value: resolve result of that promise) so you can reference results by promises you did feed the function.
I guess it doesnt help that code with 120 chars per line (pretty common standard) is getting wrapped in posts here.
--- Little warning ---
The code has bugs.. it was a first draft of something i needed at work the next day.. so dont use it for production :D -
I guess you are right @Gordon in terms of performance impact. But RAM is still an issue i guess because its very precious :)
I have a sweet little example for the Map vs. Object point @opichals made before.
Should be self-explanatory.'use strict'; let sequence = (...map) => (ok, nok) => { map = new Map(map.map(e => Array.isArray(e) ? e : [e])); ok(Array.from(map.keys()).reduce((all, prm) => { return all.then(res => prm.then(data => res.set(prm, (map.get(prm) || (() => data))(data))).catch(nok)); }, Promise.resolve(map))); }; let add = num => num + 1; let mul = num => num * 2; let addPromise = num => Promise.resolve(num); let mulPromise = num => Promise.resolve(num); let adding = addPromise(10); let multing = mulPromise(20); let map = [adding,[multing, mul]]; new Promise(sequence(...map)).then(result => { console.log('result', {addResult: result.get(adding), mulResult: result.get(multing)}); }).catch(console.log.bind(null, 'error'));
-
-
-
Yep you are right.. haven't looked into the code. All ideas that come to mind now are dirty hacks.
Like overwriting Bangle.on to watch for touch event listseners and only if no pattern is matched call the other listeners. But i am not sure if this is even allowed/possible with the API.