Skip to content

Instantly share code, notes, and snippets.

View a-eid's full-sized avatar
🎯
Focusing

Ahmed Eid a-eid

🎯
Focusing
View GitHub Profile
function setObject(str , value){
var arr = str.split('.')
var init = window
for(var i = 0 ; i < arr.length-1 ; i++){
if(!init[arr[i]]) init[arr[i]] = {}
init = init[[arr[i]]]
}
init[arr[arr.length-1]] = value // trick ..
}
@a-eid
a-eid / toggle_touchpad.sh
Created April 6, 2017 19:44
command toggle touchpad ubuntu
#!/bin/bash
read TPdevice <<< $( xinput | sed -nre '/TouchPad/s/.*id=([0-9]*).*/\1/p' )
state=$( xinput list-props "$TPdevice" | grep "Device Enabled" | grep -o "[01]$" )
if [ "$state" -eq '1' ];then
xinput --disable "$TPdevice"
else
xinput --enable "$TPdevice"
fi
@a-eid
a-eid / bsearch.js
Created April 2, 2017 11:36
simple binary search example ...
const bsearch = (arr , start , end , el) => {
if(start > end) return -1
let mid = Math.round((start + end) / 2)
if(mid == el) return mid
else if ( mid < el ) return bsearch(arr , mid + 1 , end , el)
else if ( mid > el ) return bsearch(arr , start , mid - 1 , el)
}
@a-eid
a-eid / counter.js
Created March 30, 2017 17:54
simple counter example ....
import React from 'react';
import {render} from 'react-dom';
import {createStore} from 'redux'
const reducer = (state = 0 , action) =>
(action.type == 'inc' || action.type == 'dec') ? ((action.type == 'inc' ? state + 1 : state - 1) ) : state
const store = createStore(reducer)
const Counter = ({value , onInc , onDec}) => (
@a-eid
a-eid / fac.js
Created March 22, 2017 09:47
implementation of tail recursive and no tail recursive factorial
function fac(n){
if( n == 0) return 1
return n * fac(n-1)
}
function fac2(n){
return (function tmp( n , a){
if(n == 0) return a
return tmp(n-1 , a * n)
})(n , 1)
@a-eid
a-eid / sq.js
Created March 21, 2017 03:24
newton method sqrt recursive
// square root of x in range of e .. g could be much better guessed .
function sq(x , e , g ){
g = g || x / 2
if(Math.abs( g * g - x ) < e )
return g
else
return sq( x , e , ( g + x / g ) / 2 )
}
@a-eid
a-eid / bubblesort.rb
Created March 11, 2017 21:45
bubble sort attempt ruby
def bubble_sort(a)
i = 0
len = a.size - 1
while len >= i
j = 0
while len > j
if a[j] > a[j+1]
@a-eid
a-eid / toBase.js
Last active November 19, 2016 00:39
example base conversion
function toBase(number , base){
var s = ""
do{
// binary reads from right to left
s = String(number % base) + s
number = Math.floor(number / base)
}while (number > 0)
return s
}
@a-eid
a-eid / combineReducers.js
Created November 14, 2016 16:48
difference btw comibineReducers with map and reduce ?
const combineReducers = (reducers)=>{
return (state = {} , action)=>{
Object.keys(reducers).reduce(function( nextState , key ){
nextState[key] = reducers[key](state[key] , action);
return nextState;
} , {})
}
}
@a-eid
a-eid / reducer-decomposition.js
Last active November 14, 2016 16:10
simple reducer decomposition
const todos(state=[] , action){
switch(action.type){
case 'add_todo':
case 'remove_todo':
case 'toggle_todo':
return todo(state , action)
default :
return state
}