Skip to content

Instantly share code, notes, and snippets.

View raindev's full-sized avatar

Andrew Barchuk raindev

View GitHub Profile
@raindev
raindev / State.java
Created June 2, 2014 07:59
Invalid Java enum declaration shown as good code in Intellij IDEA
package test;
import java.util.Collection;
import java.util.Collections;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableCollection;
public enum State {
STATE1 {
@raindev
raindev / game-of-life.hs
Created November 15, 2014 18:23
Game of life produced on the last coding session of Kyiv Global Day of Coderetreat 2014
import Data.List
-- Transform cells into next iteration
nextTurn :: (Integral a) => [(a,a)] -> [(a,a)]
nextTurn space = [(x, y) | (x,y,_) <- [head xs | xs <- cells space, length xs == 3 || (length xs == 4 && True `elem` ([x | (_,_,x) <- xs]))]]
-- Cells grouped by their coordinates
cells :: (Integral a) => [(a,a)] -> [[(a,a,Bool)]]
cells space = groupBy (\(x1,y1,_) (x2,y2,_) -> x1 == x2 && y1 == y2) (sort (neighbourhood space))
@raindev
raindev / EncodingTest.java
Created December 9, 2014 13:36
Wrong charset (CP1251 encoded as UTF-8) data loss
package test;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import static org.junit.Assert.assertNotEquals;
public class EncodingTest {
@raindev
raindev / Let.java
Last active August 29, 2015 14:19
Java let form
// implementation
<T> void let(T value, Consumer<T> consumer) {
consumer.accept( value );
}
// example usage
let(userService.findUser(), user -> {
// do something to the user
if (!user.isValid()) {
userService.removeUser(user);
@raindev
raindev / ifp.hs
Created April 24, 2015 12:56
Haskell ternary-operator-like function with custom predicate
ifp :: (a -> Bool) -> a -> a -> a
ifp p x y
| p x = x
| otherwise = y
@raindev
raindev / vimo.sh
Created April 24, 2015 13:03
Open terminal Vim on OS X (to be used for "Open in Vim", e.g.)
#! /bin/sh
vim="vim $@"
osascript -e "tell app \"Terminal\"
activate
do script \"$vim\"
end tell"
@raindev
raindev / brew-rehash
Last active August 29, 2015 14:23
Script for changing SHA-1 hashsum of a formula to SHA-256
#!/usr/bin/env bash
# Usage: ./brew-rehash.sh formula-file.rb
if [[ $1 == *".rb" ]]; then
formula=$1
else
formula="$1.rb"
fi
echo "Rehashing $1"
@raindev
raindev / directory-size.rs
Created September 5, 2015 13:20
A simple Rust program to demonstrate loss of cached stdout
use std::fs;
use std::env;
use std::process;
fn main() {
match env::args_os().nth(1) {
None => {
print!("nope");
print!("okay");
// will cause losss of stdout buffered above
@raindev
raindev / keybase.md
Created April 13, 2016 22:29
Keybase identity proof

Keybase proof

I hereby claim:

  • I am raindev on github.
  • I am raindev (https://keybase.io/raindev) on keybase.
  • I have a public key ASBb46e0JuM3Gar31P90FDV9Lk2RvgNZ8MLZUS6Kg-L6Ygo

To claim this, I am signing this object:

@raindev
raindev / external-params.swift
Created August 16, 2016 12:12
External function parameters in Swift
func printMathResult(mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
print(mathFunction(a, b))
}
printMathResult({$0 + $1}, a: 2, b: 3)
printMathResult({$0 + $1}, 2, 3)