Skip to content

Instantly share code, notes, and snippets.

View anandgeorge's full-sized avatar

Anand George anandgeorge

  • Storm Consulting
  • Mumbai, India
View GitHub Profile
@anandgeorge
anandgeorge / elixir_tips_tricks
Created July 22, 2021 15:23
Elixir Data Types, Maps, Lists, Tuples and Pattern Matching TIps
Access help functions for a module using the h command; so
h String
will give you help related to the String function
#### Data types
iex> 2 # integer
iex> 2.0 # floating point
@anandgeorge
anandgeorge / game.ex
Created July 16, 2021 07:59 — forked from andrewhao/game.ex
Dynamic Supervisors in Elixir
defmodule Game do
use GenServer
def init(game_id) do
{:ok, %{game_id: game_id}}
end
def start_link(game_id) do
GenServer.start_link(__MODULE__, game_id, name: {:global, "game:#{game_id}"})
end
@anandgeorge
anandgeorge / Elixir_Supervision_Trees.md
Created July 15, 2021 16:25 — forked from gkaemmer/Elixir_Supervision_Trees.md
Quick guide to creating Elixir supervision trees from scratch

Elixir Supervision Trees Made Easy

I started with Elixir just a couple weeks after the switch from 1.4 to 1.5, so the bulk of online resources were out of date (or at least resulted in deprecation warnings). This guide is for defining Elixir 1.5 supervised modules.

It's not actually terribly complicated. It's just sometimes unclear from examples what's implemented by the language and what you actually have to implement yourself.

Say we want a supervision tree like this (where each atom is a process):

    :a

/ \

@anandgeorge
anandgeorge / one_mix.exs
Created June 19, 2021 12:48
Genetic algorithms in Elixir basic algorithm explained in detail as comments
population = for _ <- 1..100, do: for _ <- 1..1000, do: Enum.random(0..1)
# evaluate, selection, crossover and algorithm are anonymous functions.
evaluate =
fn population ->
# population is a list of lists [[1,1,0,0,1...1,1,0],[1,1,1,0,0...0,1,1]] 100 lists, 1000 bits each
Enum.sort_by(population, &Enum.sum/1, &>=/2)
# sort the list of lists by sum of bits so lists with higher number of one's are at the top. Mapper sums each row. Sorter is descending &>=/2
# note the use of anonymous functions defined by &.
@anandgeorge
anandgeorge / elixir_notes.md
Created June 14, 2021 07:57 — forked from fschuindt/elixir_notes.md
My personal study notes on Elixir. It's basically a resume of http://elixir-lang.org/getting-started/ (Almost everything is copied). Still working on it!
@anandgeorge
anandgeorge / app.js
Last active December 29, 2015 09:59
Analytics using MongoDB aggregation and Node.js
// The code is based on a MongoDB database myhome with a typical record as below:
// {
// "rec_time" : ISODate("2013-11-25T16:53:43.808Z"),
// "rec_data" : {
// "power" : 372.201,
// "current" : 1.73,
// "voltage" : 221.678,
// },
// "rec_asset" : {
// "name" : "Airconditioner",
@anandgeorge
anandgeorge / app.js
Created May 27, 2012 16:15
Socket.io example with jquery and dom manipulation
var io = require('socket.io').listen(8000),
nicknames = {};
io.sockets.on('connection', function (socket) {
socket.on('user message', function (msg) {
socket.broadcast.emit('user message', socket.nickname, msg);
});
socket.on('nickname', function (nick, fn) {
if (nicknames[nick]) {
@anandgeorge
anandgeorge / README.md
Created March 13, 2012 11:17 — forked from bergie/README.md
Falsy Values tutorials
@anandgeorge
anandgeorge / mouse.js
Created March 12, 2012 14:57 — forked from bfncs/mouse.js
Read Linux mouse(s) in node.js
/**
* Read Linux mouse(s) in node.js
* Author: Marc Loehe (marcloehe@gmail.com)
*
* Adapted from Tim Caswell's nice solution to read a linux joystick
* http://nodebits.org/linux-joystick
* https://github.com/nodebits/linux-joystick
*/
var fs = require('fs'),
@anandgeorge
anandgeorge / impartialoutsider.coffee
Created March 12, 2012 04:10 — forked from chrissharkey/impartialoutsider.coffee
Pizza voting IRC bot in Node.js
#!/usr/bin/env node
CHANNEL = '#bozler'
MINIMUM_HUMAN_REACTION = 2
DIALOGUE =
ACTIONS_IN_REPLY: [
"I vote for pizza",
"Disregard that, I vote for pizza",
"Another vote for pizza!"