Skip to content

Instantly share code, notes, and snippets.

View kachayev's full-sized avatar
🇺🇦
Fighting for freedom

Oleksii Kachaiev kachayev

🇺🇦
Fighting for freedom
View GitHub Profile
@kachayev
kachayev / LCA.py
Created July 17, 2013 19:04
Solve LCA taks with binary path method
## Solve LCA taks with binary path method
## Complexity is <O(NlogN), O(logN)>
tree = {
0: [1,2],
1: [3,4,5],
2: [6,7,8,9],
3: [10,11,12,13],
4: [14,15],
5: [16,17,18,19,20],
@kachayev
kachayev / skew.hs
Last active July 10, 2018 16:52
Skew Heap implementation in Haskell
data Skew a = Empty | Node a (Skew a) (Skew a)
singleton :: Ord a => a -> Skew a
singleton x = Node x Empty Empty
union :: Ord a => Skew a -> Skew a -> Skew a
union t1 Empty = t1
union Emtpy t2 = t2
union t1@(Node x1 l1 r1) t2@(Node x2 l2 r2)
| x1 <= x2 = Node x1 (union t2 r1) l1
@kachayev
kachayev / typed_channel.hs
Created August 1, 2013 10:48
Typed Channels example for Cloud Haskell
channelsDemo :: Process ()
channelsDemo = do
(sp, rp) <- newChan :: Process (SendPort String, ReceivePort String)
-- send on a channel
spawnLocal $ sendChan sp "hello!"
-- receive on a channel
m <- receiveChan rp
say $ show m
@kachayev
kachayev / event_sourced_channel.scala
Last active December 20, 2015 12:19
Eventsourced and reliable channel in Scala
class Sender(receiverPath: String) extends Actor {
val id = 1
val ext = EventsourcingExtension(context.system)
val proxy = context.actorOf(
Props(new DestinationProxy(receiverPath)))
val channel = ext.channelOf(ReliableChannelProps(1, proxy)
.withRedeliveryMax(1000)
.withRedeliveryDelay(1 second)
.withConfirmationTimeout(2 seconds))
var i int
func makeCakeAndSend(cs chan string) {
i = i + 1
cakeName := "Strawberry Cake " + strconv.Itoa(i)
fmt.Println("Making a cake and sending ...", cakeName)
cs <- cakeName //send a strawberry cake
}
func receiveCakeAndPack(cs chan string) {
s := <-cs //get whatever cake is on the channel
(require '[clojure.core.async
:as async
:refer [<! >! <!! timeout chan alt! go]])
(defn fake-search [kind]
(fn [c query]
(go
(<! (timeout (rand-int 100)))
(>! c [kind query]))))
(require '[clojure.core.async
:as async
:refer [<! >! <!! timeout chan alt! go]])
(defn fake-search [kind]
(fn [c query]
(go
(<! (timeout (rand-int 100)))
(>! c [kind query]))))
@kachayev
kachayev / channels_streams.erl
Last active December 20, 2015 13:19
Functional approach to deal with "google search" example from Rob Pike talk
search(Ch, Kind, Query) ->
timer:sleep(random:uniform(100) + 10),
enqueue(Ch, {Kind, Query}).
fake(Kind) ->
Ch = make(),
fun(Query) ->
spawn(?MODULE, search, [Ch, Kind, Query]), Ch
end.
main() ->
Clients = ch:make(),
spawn(clients_generator, [Clients]),
lists:foreach(fun(_) ->
spawn(barber, [Clients])
end, lists:seq(1,10)).
clients_generator(Clients) ->
ch:enqueue(client, Clients),
timer:sleep(random:uniform(100)),
@kachayev
kachayev / auth_file_server.go
Created August 16, 2013 14:27
Basic Authentication for http.FileServer
package main
import (
"fmt"
"net/http"
auth "github.com/abbot/go-http-auth"
)
func Secret(user, realm string) string {
if user == "john" {