Skip to content

Instantly share code, notes, and snippets.

View lbarasti's full-sized avatar

Lorenzo Barasti lbarasti

View GitHub Profile
# in-place shuffle
def wrong_shuffle arr
arr.size.times {
i, j = rand(arr.size), rand(arr.size)
arr[i], arr[j] = arr[j], arr[i]
}
arr
end
# in-place shuffle
// load script from chrome console
var script = document.createElement('script');script.src = "/path/to/script.js";document.getElementsByTagName('head')[0].appendChild(script);
@lbarasti
lbarasti / stack.rb
Created November 15, 2015 23:44
Implements a stack as an immutable data structure using a functional approach
module Stack
include Enumerable
class EmptyStack
include Stack
def empty?; true; end
def pop; raise 'Cannot pop empty stack'; end
def peek; raise 'Cannot peek empty stack' end
@lbarasti
lbarasti / tmux.conf
Last active November 26, 2015 00:54
Tmux configuration file
# enable ctrl-left/right arrow
set-window-option -g xterm-keys on
# set vi bindings
setw -g mode-keys vi
# bind y to copy to clipboard - requires sudo apt-get install xclip
bind -t vi-copy y copy-pipe "xclip -sel clip"
@lbarasti
lbarasti / p.coffee
Last active November 26, 2015 08:48
class P
notify = (d, value) ->
P.run -> d.fulfil(d.onFulfil?(value))
fulfil: (value) ->
@value = value
@dependencies.forEach (d) ->
notify(d, value)
return this
then: (d) ->
wrapped = new P(d)
@lbarasti
lbarasti / add_screen_resolution.sh
Created December 31, 2015 09:24
Ubuntu: add screen resolution for external monitor
xrandr # prints the current setup
sudo cvt 1920 1080 60 # create new resolution mode
# declare new mode
sudo xrandr --newmode "1920x1080_60.00" 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync
# add mode to target screen - e.g. VGA1
sudo xrandr --addmode VGA1 1920x1080_60.00
# reference: http://askubuntu.com/questions/138408/how-to-add-display-resolution-fo-an-lcd-in-ubuntu-12-04-xrandr-problem
module Main where
import Prelude
import Control.Monad.Eff.Console (log)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import TryPureScript
f :: Int -> Int -> Int
f _ 0 = 0
// main.java
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
@lbarasti
lbarasti / weather_station_sim.cr
Created December 10, 2019 08:32
A thread-safe class supporting concurrent reads and writes
# tested on Crystal 0.31.1
class WeatherStation
alias StationState = Hash(Int32, Float64)
private record SetTemperature, id : Int32, temperature : Float64
private record GetTemperatures, return_channel : Channel(StationState)
@requests = Channel(SetTemperature | GetTemperatures).new
def initialize
@current_temperatures = StationState.new