Skip to content

Instantly share code, notes, and snippets.

@programaths
programaths / generators-vs-iterator.php
Last active September 21, 2015 12:22
PHP generators
<?php
// Data on which we operate
$tree=[
'+'=>[
'*'=>[1,2],
'-'=>[
'+' => [3,4]
, 5
import java.text.DateFormat
import java.time.LocalDate
import java.util
import java.util.*
/*
Given a list of people with their birth and end years (all between 1900 and 2000),
find the year with the most number of people alive.
See http://qr.ae/RAG2zH
*/
@programaths
programaths / JavaFXBuilder.kt
Created August 13, 2015 14:35
Mimmic FXML in Kotlin using only one extention method
fun main(args:Array<String>){
Application.launch(javaClass<Foo>())
}
fun <T:Any> T.builder(f:T.()->Unit):T{
this.f()
return this
}
class Foo : Application(){
@programaths
programaths / filter.egl
Last active August 29, 2015 14:28
Remove items from a list on specific criterion
// The bad
isFound boolean = true;
while(isFound)
isFound = false;
for(j int from 1 to array.getSize())
if (array[j].attribute == "lookupValue")
array.removeElement(j);
isFound = true;
end
end
@programaths
programaths / GameService.kt
Created April 15, 2016 12:02
Example of formatting
gameService.gamelistArray()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
gameList ->
adapter=GameListAdapter(gameList.toList().map { it.second },ctx)
}
-- V1
[ if x `mod` 3 == 0 then
"Fizz"
else
if x `mod` 5 == 0 then
"Buzz"
else
if x `mod` 15 == 0 then
"FizzBuzz"
else show x
package main
import (
"fmt"
"math/rand"
"math"
"time"
)
@programaths
programaths / LineCount.kt
Created September 15, 2016 19:21
A functional line count.
import java.io.File
fun main(args: Array<String>) {
args.map { name -> File(name) }
.filter { file -> file.isFile }
.map { file -> Pair(file.name, file.readLines().partition { line -> line.trim().isEmpty() }) }
.forEach {
val name = it.first
val (filledLines,emptyLines) = it.second
println("$name: ${filledLines.size+emptyLines.size} total, ${emptyLines.size} empty")
@programaths
programaths / KTGW_VTX.kt
Created March 2, 2017 21:03
This code show a complex stuff done in few lines and 60ms max out of which 20ms come from the origin having hard time serving an HTML file...
import io.netty.buffer.ByteBufInputStream
import io.vertx.core.Vertx
import io.vertx.ext.web.client.WebClient
import org.jsoup.Jsoup
import org.jsoup.nodes.Element
fun main(args: Array<String>) {
val vertx = Vertx.vertx()
val server = vertx.createHttpServer()
val client = WebClient.create(vertx)
@programaths
programaths / deobfuscate.go
Created March 16, 2017 20:32
Generate deobfusacting golang
package main
import (
"fmt"
"math/rand"
)
func Generate(ch chan<- int) {
for i := 2; ; i++ {
ch <- i