Skip to content

Instantly share code, notes, and snippets.

View cy6erGn0m's full-sized avatar
🦎

Sergey Mashkov cy6erGn0m

🦎
View GitHub Profile
@cy6erGn0m
cy6erGn0m / recursion1.kt
Created October 13, 2013 09:13
Kotlin tail recursive foldl demo
fun <T, A> Iterator<T>.foldl(acc : A, foldFunction : (e : T, acc : A) -> A) : A =
if (!hasNext()) acc
else foldl(foldFunction(next(), acc), foldFunction)
val sum = (1..1000000).iterator().foldl(0) { (e : Int, acc : Long) ->
acc + e
}
@cy6erGn0m
cy6erGn0m / myFiles.erl
Created October 2, 2013 19:24
Yet another directory recursive delete function for Erlang
ensureDirSlash(Dir, Child) ->
case lists:suffix("/", Dir) of
true -> Dir ++ Child;
false -> Dir ++ "/" ++ Child
end.
-spec deleteDir(Dir :: nonempty_string()) -> none().
deleteDir(Dir) ->
case {filelib:is_dir(Dir), isFileOrLink(Dir)} of
{_, true} -> throw("The specified path is not a directory: " ++ Dir);
@cy6erGn0m
cy6erGn0m / graham.hs
Last active December 22, 2015 21:38
Graham scan algorithm implementation in Haskell
import Data.List (sortBy, minimumBy, delete)
data Point = Point {x :: Double, y :: Double } deriving (Show, Eq)
add (Point ax ay) (Point bx by) = Point (ax + bx) (ay + by)
sub :: Point -> Point -> Point
sub (Point ax ay) (Point bx by) = Point (ax - bx) (ay - by)
mult :: Point -> Point -> Double
data Node a = Node a (Node a) (Node a) | Empty
treeHeight :: Node a -> Int
treeHeight Empty = 0
treeHeight tree = treeHeightImpl [(1, tree)] 1
where
treeHeightImpl :: [ (Int, (Node a)) ] -> Int -> Int
treeHeightImpl ((currentDepth, (Empty)) : xs) maxDepth = treeHeightImpl xs maxDepth
treeHeightImpl [] maxDepth = maxDepth
treeHeightImpl ((currentDepth, (Node _ left right)) : xs) maxDepth =
package cg;
import java.util.List;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
* @author Sergey Mashkov (cy6erGn0m)
* @since 04.03.13
@cy6erGn0m
cy6erGn0m / JavaRegexpTest.java
Created March 3, 2013 19:09
Regexp performance difference example
package cg;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
@cy6erGn0m
cy6erGn0m / test.groovy
Created August 17, 2012 14:59
Groovy dot "override"
def map = [:]
map.test = 1
println map.test
map.xxx = [ aa : 'aa']
println map.xxx.aa
@cy6erGn0m
cy6erGn0m / Main.java
Created June 21, 2012 14:46
Simple example jansi library
package cg;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole;
/**
* User: Sergey Mashkov
* Date: 6/21/12
*/
public class Main {
@cy6erGn0m
cy6erGn0m / Main.java
Created June 8, 2012 06:44
Simplest HTTP request example
package cg;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author Sergey Mashkov aka cy6erGn0m
* @since 08.06.12