Skip to content

Instantly share code, notes, and snippets.

@kgadek
kgadek / gist:1119119
Last active September 26, 2015 15:28
Clojure: 99 bottles (II)
(ns org.gadek.fun.bottles)
(defn bottle-cnt [stock]
(str (if (zero? stock) "No more" stock)
" bottle" (when (not (= 1 stock)) "s")
" of beer"))
(defn verses [& [stock prev]]
(lazy-seq (let [stock (or stock 0) ; start from 0
prev (or prev (bottle-cnt 99)) ; even earlier -- 99 bottles
@kgadek
kgadek / gist:1124869
Last active September 26, 2015 16:18
JavaScript: ascii-art parser (Windmobile reports presenter)
// Ta tabela jest rozumiana przez JS -- wszystkie zmiany wprowadzać tutaj.
// | from | to | employees | seta | premia | kanał | prezencja | prefix | agregacja |
// |---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------|
var flds = [ dateFromField , dateToField, includeWorkersCb, setaType , forPremiaCb , accChannel , presence , prefix , agreg ];
var r2fl ={//|---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------|
'R0' : " | | x | | | | | | ",
'R1a' : " x | x | x | | | x | x |
@kgadek
kgadek / gist:2921556
Last active October 6, 2015 02:38
Erlang: extract *.ini file from *.tgx package and parse it in 12 LOC
parse_ini(Filename) ->
Str = string:tokens(os:cmd(["tar JxOf ", Filename, " settings.conf"]), "\n"),
parse_ini(Str, default, []).
parse_ini([], _Group, Res) -> Res;
parse_ini([Head|Tail], Group, Res) ->
[F|R] = Str = string:strip(Head),
case F of
$# -> parse_ini(Tail, Group, Res);
$[ -> parse_ini(Tail, string:strip(R, both, $]), Res);
_ -> [K|V] = string:tokens(Str, "="),
@kgadek
kgadek / gist:3216960
Last active October 7, 2015 19:57
Makefile: for Literate Haskell, Markdown/LaTeX mixed style
# sauce of original Makefile magic:
# http://passingcuriosity.com/2008/literate-haskell-with-markdown-syntax-hightlighting/
# modified by kgadek
# Ok, so I found Markdown+Bird style to be nice BUT editors have problems with Haskell code (indenting & stuff) when
# each line starts with ">".
# On the other hand LaTeX style is ok but LaTeX is often too powerful - distracts me all the time from actual code.
# But don't worry, I'm (nearly) an Engineer. Solution:
# Write Markdown style, enclose code with \begin{code}..\end{code} block and use small sed script to convert it to Markdown+Bird.
@kgadek
kgadek / FunSetSuite.scala
Created October 5, 2012 17:11
Scala: bughunting in assignment from Coursera's course
package funsets
import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
/**
* This class is a test suite for the methods in object FunSets. To run
* the test suite, you can either:
@kgadek
kgadek / gist:4760294
Created February 12, 2013 04:43
Monads somewhat explained w/ dense example. Read more: http://readlists.com/0bca8296 . Kudos to: bartoszmilewski.com .
import Control.Monad
data StackCalc a = StackCalc { runStackCalc :: [Int] -> ([Int], a) }
instance Monad StackCalc where
return x = StackCalc aux
where aux stack = (stack, x)
f >>= e = StackCalc aux
where aux stack = let (newstack, newval) = runStackCalc f stack
@kgadek
kgadek / h.hs
Created March 21, 2013 13:41
(Poorly written) Haskell programme to find out 20 most common words in text
import Text.ParserCombinators.Parsec hiding (spaces)
import System.Environment
import Data.List (foldl')
import qualified Data.Map.Strict as M
main = interact getWords
getWords input =
case parse parseWords "words parser" input of
@kgadek
kgadek / python.py
Created March 21, 2013 13:42
Python programme to find out 20 most common words in text
#!/usr/bin/env python3.2
import re
from operator import itemgetter
word2num = {}
num2word = {}
with open('potop.txt', mode='r', encoding='utf-8') as potop:
for line in potop:
@kgadek
kgadek / gist:5213110
Created March 21, 2013 13:46
Haskell vs Python
## Task: get 20 most common words in given text
## Input: http://home.agh.edu.pl/~dorosz/pjn/lab-01/potop.txt
## take a note this is in ISO-8859-2/CRLF. I converted it to UTF-8/LF:
## $ iconv -f ISO-8859-2 -t UTF-8 potop.txt > potop.tmp
## $ tr -d '\r' < potop.tmp > potop.txt
## Haskell: https://gist.github.com/kgadek/5213089
## uses Parsec
extend amber.DriverMsg {
// DriverMsg.type = DriverMsg.MsgType.PING / DriverMsg.MsgType.PONG
optional KeepaliveMsg keepaliveMsg = 10;
}
message Keepalive {
optional uint32 time = 1; // epoch! czas wysłania
}