Skip to content

Instantly share code, notes, and snippets.

View thiagokokada's full-sized avatar

Thiago Kenji Okada thiagokokada

View GitHub Profile
defmodule Calculator do
def repl(acc \\ 0.0, i \\ 0, io \\ IO) do
interpret(acc, i + 1, io) |> repl(i + 1, io)
end
def interpret(acc, i, io) do
io.puts("accumulator> #{acc}")
[command, value] = io.gets("operation(#{i})> ") |> String.trim() |> String.split()
{value, _} = Float.parse(value) # Convert a string to its float representation
run_command(acc, command, value)
describe "+ 1.0 * 5" do
defmodule FakeIO do
defdelegate puts(message), to: IO
def gets("operation> "), do: "+ 1.0\n"
def gets("operation> "), do: "* 5\n"
end
test "accumulator should be 1.0" do
run = fn -> Calculator.repl(0.0, FakeIO) end
assert capture_io(run) =~ "5.0"
defmodule CalculatorTest do
use ExUnit.Case
import ExUnit.CaptureIO
doctest Calculator
describe "+ 1.0" do
defmodule FakeIO do
defdelegate puts(message), to: IO
def gets("operation> "), do: "+ 1.0"
defmodule Calculator do
def repl(acc \\ 0.0, io \\ IO) do
interpret(acc, io) |> repl(io)
end
def interpret(acc, io) do
io.puts("accumulator> #{acc}")
[command, value] = io.gets("operation> ") |> String.trim() |> String.split()
{value, _} = Float.parse(value) # Convert a string to its float representation
run_command(acc, command, value)
defmodule Calculator do
def repl(acc \\ 0.0) do
IO.puts("accumulator> #{acc}")
[command, value] = IO.gets("operation> ") |> String.trim() |> String.split()
{value, _} = Float.parse(value) # Convert a string to its float representation
acc |> run_command(command, value) |> repl()
end
defp run_command(acc, command, value) do
case command do
@thiagokokada
thiagokokada / lock_screen.py
Last active October 13, 2017 00:05
Lock screen using i3lock with background blur in Python
#!/usr/bin/env python
from subprocess import check_call, CalledProcessError
from tempfile import NamedTemporaryFile
from dpms import DPMS
from mss import mss
from PIL import Image, ImageFilter
GAUSSIAN_BLUR_RADIUS = 5
@thiagokokada
thiagokokada / dynupdate.py
Last active October 31, 2017 02:09
Script to update now-dns.com IPv4/v6 address
#!/usr/bin/env python3
# Script to update now-dns.com IPv4/v6 address
# Based on https://now-dns.com/client/dynupdate.pl
# Depends on python3 and python3-requests installed in the system
import os
import re
import requests
@thiagokokada
thiagokokada / i3lock-xkcd.py
Last active October 13, 2017 22:20
Downloads and show random xkcd comic as i3lock background
#!/usr/bin/env python3
# i3lock-xkcd.py
# Downloads and show random xkcd comic as i3lock background
import os
import sys
from subprocess import run
from tempfile import NamedTemporaryFile
from textwrap import wrap
@thiagokokada
thiagokokada / rotate_desktop.sh
Last active September 18, 2016 03:07 — forked from mildmojo/rotate_desktop.sh
Script to rotate the screen and touch devices on modern Linux desktops. Great for convertible laptops.
#!/bin/bash
#
# rotate_desktop.sh
#
# Rotates modern Linux desktop screen and input devices to match. Handy for
# convertible notebooks. Call this script from panel launchers, keyboard
# shortcuts, or touch gesture bindings (xSwipe, touchegg, etc.).
#
# Using transformation matrix bits taken from:
# https://wiki.ubuntu.com/X/InputCoordinateTransformation
@thiagokokada
thiagokokada / [Arch Linux] auto_upgrade
Last active June 9, 2018 05:33
Get e-mail notifications of updates in Arch Linux
Auto-upgrade Arch Linux or get e-mail in case of failure.