Skip to content

Instantly share code, notes, and snippets.

@Shemeikka
Shemeikka / crc16.go
Created July 10, 2015 08:46
CRC16 in Go
type CRC16 struct {
Tab []uint16
Constant uint16
}
// Init crc16
func (c *CRC16) Init() {
c.Constant = 0xA001
for i := 0; i < 256; i++ {
@Shemeikka
Shemeikka / randomstring.go
Created August 19, 2015 05:42
Generates random n sized hex string
import (
crand "crypto/rand"
"math/big"
)
// Generates random n sized hex string
func RandString(n int) string {
const alphanum = "0123456789ABCDEF"
symbols := big.NewInt(int64(len(alphanum)))
states := big.NewInt(0)
@Shemeikka
Shemeikka / crc16.ex
Last active April 4, 2016 06:29
CRC16 calculation in Elixir using constant 0xA001
defmodule CRC16 do
use Bitwise
@constant 0xA001
def calculate(data) do
crc = 0x0000
table = init_table()
:binary.bin_to_list(data)
|> Enum.reduce(crc, fn(value, crc) ->
(crc >>> 8) ^^^ Enum.at(table, (crc ^^^ value) &&& 0x00ff)
@Shemeikka
Shemeikka / build_go.sh
Last active April 27, 2016 14:29
Building Go 1.6.2 for ARMv5 on ARM using Go 1.4.3
# Guide http://blog.hypriot.com/post/how-to-compile-go-on-arm/
# Todo
# Use named variables for versions
# Take version as a cmd argument
# Error handling
# Download Go 1.4.3 source
wget https://storage.googleapis.com/golang/go1.4.3.src.tar.gz
# Extract it
@Shemeikka
Shemeikka / gist:d4c73bd97080614660036af42b58d605
Created May 20, 2016 10:13
Install react development environment with npm
npm install --save react react-dom
npm install --save-dev babel webpack webpack-dev-server
npm install --save-dev babel-core babel-loader babel-preset-es2015 babel-preset-react
@Shemeikka
Shemeikka / file.js
Created June 14, 2016 08:57
Drag Leaflet.js circle
// From http://codepen.io/kad3nce/pen/bEdwOE
var map = L.map('map').setView(center, 13);
var circle = L.circle(center, 1000).addTo(map);
circle.on('mousedown', function (event) {
map.dragging.disable();
var _circle$_latlng = circle._latlng;
var circleStartingLat = _circle$_latlng.lat;
var circleStartingLng = _circle$_latlng.lng;
var _event$latlng = event.latlng;
@Shemeikka
Shemeikka / notes.js
Last active September 5, 2016 18:16
Javascript notes
// Some stuff requires ES6
// Watch this first
// Philip Roberts: What the heck is the event loop anyway? | JSConf EU 2014
// https://www.youtube.com/watch?v=8aGhZQkoFbQ
// Then read this
// https://rainsoft.io/gentle-explanation-of-this-in-javascript/
// https://strongloop.com/strongblog/higher-order-functions-in-es6easy-as-a-b-c/
@Shemeikka
Shemeikka / file.js
Last active October 4, 2016 12:51
factorial and sum of fibonacci(n) using recursion in JavaScript
const factorial = (n, acc=1) => {
if (n < 1) { return undefined; }
if (n === 1) { return acc; }
return factorial(n-1, n*acc);
};
const sumFibonacci = (n, prev=0, next=1, acc=0) => {
if (n === 0) { return acc; }
@Shemeikka
Shemeikka / bmi.elm
Last active September 25, 2016 09:43
Simple BMI calculator written in Elm
import Html exposing (..)
import Html.App exposing (beginnerProgram)
import Html.Attributes as HA exposing (..)
import Html.Events exposing (onClick, onInput)
import String
main =
beginnerProgram { model = model, view = view, update = update }
@Shemeikka
Shemeikka / gist:11f196884212dc650e828c2f71c4bddf
Created November 8, 2016 18:36
Elixir Genserver callbacks and return values
# GenServer callbacks and return values
## init(args)
{:ok, state}
{:ok, state, timeout}
:ignore
{:stop, reason}
## handle_call(msg, {from, ref}, state)