Skip to content

Instantly share code, notes, and snippets.

View hannestyden's full-sized avatar

Hannes Tydén hannestyden

  • Stockholm, Sweden
View GitHub Profile
# Emulating `sed` with inplace editing **without** backups for OS X
# Tested with zsh.
sedi () {
local ext='.sedi-bak'
sed -i $ext $@
rm "${@: -1}$ext"
}
class Hash
alias_method :[], :fetch # !> method redefined; discarding old []
end
h = {a: 1}
h[:a] # => 1
h[:b] rescue $! # => #<KeyError: key not found: :b>
h[:b, 2] # => 2
# # Adventures in `flat_map` land
#
# Investigating the conceptual difference between Ruby's `flat_map` and Scala's `flatMap`.
#
# Ruby's `flat_map`
a = [[1], [2], [[3]], [[4], 5], [[[6]]]]
a.flat_map { |e| e.first } # => [1, 2, 3, 4, [6]]
# ... which is the same as ...
a.map { |e| e.first }.flatten(1) # => [1, 2, 3, 4, [6]]
@hannestyden
hannestyden / animal.rb
Last active April 8, 2018 02:48
Inheritence, mix-ins, composotion
# Composition
class Eat
def do_it
puts 'Nom, nom ...'
end
end
class Say
def do_it
@hannestyden
hannestyden / git-lye.sh
Last active April 8, 2018 02:48 — forked from grobie/git-lye.sh
# git lye
# An attempt to make rebasing easy, by draining unnessarcy commit noise.
# No warranty, use at own risk.
# Author Hannes Tydén <hannes@tyden.name>
# Contributor Tobias Schmidt <ts@soundcloud.com>
# TODO
# - Don't sign off all commits are by current user.
@hannestyden
hannestyden / whatIsMyName.scala
Last active April 8, 2018 02:48
What is my name?
scala> def whatIsMyName[A,B](map: Map[A, Seq[B]]): List[(A, B)] =
| map.map { case (k, v) => v.map(k -> _) }.flatten.toList
whatIsMyName: [A, B](map: Map[A,Seq[B]])List[(A, B)]
scala> whatIsMyName(Map('a -> Seq(1,2), 'b -> Seq(3,4)))
res0: List[(Symbol, Int)] = List(('a,1), ('a,2), ('b,3), ('b,4))
class E < StandardError; end
def m(raise_exception)
y do
r(raise_exception)
:nothing_raised
end
rescue E
:rescued
end
@hannestyden
hannestyden / andr
Last active April 8, 2018 01:47 — forked from christoffer/andr
#!/bin/bash
function _andr_push {
echo "Pushing $1"
adb $target_flag push $1 $books_dir
}
function _andr_delete {
echo "Removing $1"
adb $target_flag shell "rm $books_dir/$1"
@hannestyden
hannestyden / Gemfile
Last active April 8, 2018 01:47 — forked from jaredculp/das_download.rb
Destroy All Software downloader
source 'https://rubygems.org' do
gem 'mechanize'
end
@hannestyden
hannestyden / post-checkout
Created October 28, 2010 11:26
Prints a random line from the bridge of Metallica's "Master of Puppets" when checking out the master branch.
#!/bin/sh
# .git/hooks/post-checkout
# chmod +x .git/hooks/post-checkout
if [ $(git symbolic-ref HEAD | cut -d '/' -f 3) == 'master' ]; then
lines[0]="Master, Master, where's the dreams that I've been after?"
lines[1]="Master, Master, you promised only lies"
lines[2]="Laughter, laughter, all I hear or see is laughter"
lines[3]="Laughter, laughter, laughing at my cries"
echo " ${lines[$((RANDOM%${#lines[*]}))]}";