Skip to content

Instantly share code, notes, and snippets.

View geoand's full-sized avatar

Georgios Andrianakis geoand

View GitHub Profile
@geoand
geoand / gist:12f1cc17f802f5e6b263
Last active November 10, 2015 16:36
Groovy - Guava classical set operations
@Grapes(
@Grab(group='com.google.guava', module='guava', version='18.0')
)
import static com.google.common.collect.Sets.*
def first = ['a', 'b', 'c'] as Set
def second = ['b', 'c', 'd', 'e'] as Set
@geoand
geoand / gist:79a44ad2128059349ab4
Created November 18, 2015 15:43
Tshark capture local traffic outgoing TCP or UDP traffic with host resolution
tshark -c 50 -NnNC -E header=y -Tfields -e col.Protocol -e ip.dst -e ip.dst_host -e tcp.dstport -e udp.dstport -f "host $(hostname --ip-address) and (tcp or udp) and not (port 53)"
@geoand
geoand / gist:97b40fc0170df387cb03
Created February 12, 2016 14:43
List Subsets in Groovy (inspired from Java 8 in Action#13.2.4)
def list = [1,4,9]
println subsets(list)
List<List<Integer>> subsets(List<Integer> list) {
if(!list) {
return [[]]
}
final List<List> directSubs = list.tail().collect {
tailrec fun <T : Comparable<T>> binarySearch(list: List<T>, item: T, startIndex: Int = 0, endIndex: Int = list.size - 1): Int {
if (endIndex < startIndex ) {
return -1
} else if (startIndex == endIndex) {
return if (item == list[startIndex]) startIndex else -1
} else if (endIndex == startIndex + 1) {
if (list[startIndex] == item) {
return startIndex
}
else if(list[endIndex] == item) {
@geoand
geoand / iddfs.groovy
Created April 28, 2016 12:35
Groovy implementation of a simplistic Iterative deepening depth-first search
import groovy.transform.TailRecursive
import groovy.transform.ToString
@ToString(includePackage = false)
class Node<T> {
T data
List<Node<T>> nodes = []
}
final builder = new ObjectGraphBuilder(classLoader: getClass().classLoader)
/**
* Method extension code
*/
fun <T1, T2> Collection<T1>.combine(other: Iterable<T2>): List<Pair<T1, T2>> {
return combine(other, {thisItem: T1, otherItem: T2 -> Pair(thisItem, otherItem) })
}
fun <T1, T2, R> Collection<T1>.combine(other: Iterable<T2>, transformer: (thisItem: T1, otherItem:T2) -> R): List<R> {
return this.flatMap { thisItem -> other.map { otherItem -> transformer(thisItem, otherItem) }}
fun <T, M> Iterable<T>.countBy(transformer: (T) -> M) : Map<M, Int> {
val emptyMap = mapOf<M, Int>()
return this.fold(emptyMap) {accumulator, item ->
val transformedItem = transformer(item)
accumulator + Pair(transformedItem, accumulator.getOrElse(transformedItem, {0}) + 1)
}
}
fun <T> List<T>.collate(size: Int, step: Int = size): List<List<T>> {
require(size >= 0) {"size must not be negative"}
require(step <= size) {"step must be at most equal to size"}
return collateRec(this, size, step, listOf())
}
private tailrec fun <T> collateRec(list : List<T>, size: Int, step: Int, accumulator: List<List<T>>) : List<List<T>> {
if(list.isEmpty()) {
return accumulator
@geoand
geoand / introrx.md
Created July 28, 2016 11:44 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing
PLAY [Create initial host groups for localhost] ********************************************************************************************
TASK [include_vars] ************************************************************************************************************************
ok: [localhost]
PLAY [Initialization Checkpoint Start] *****************************************************************************************************
skipping: no hosts matched
PLAY [Populate config host groups] *********************************************************************************************************