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 / 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]))))
@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 / start_elixir_macos.sh
Created September 1, 2013 18:33
Install/Run Elixir lang on Mac OS
$ mkdir ~/ElixirLang
$ cd ~/ElixirLang
$ brew install kerl
$ kerl build R16B01 r16b01
$ kerl install r16b01 ~/erlang/r16b01
$ . ~/erlang/r16b01/activate
$ erl
Erlang R16B01 (erts-5.10.2) [source] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.2 (abort with ^G)
@kachayev
kachayev / pipes_example.ex
Created September 2, 2013 08:24
Push Notifications sending with #riak_pipe #Elixir #Pipes
import Pipes
# defservice Profile do ... end
# defservice Device do ... end
# defservice Push do ... end
send = pipe do
profile <- Profile.all &1
badge <- Profile.get_badge_size profile
device <- Device.sessions profile
iex(1)> import Scala
nil
iex(2)> s = for do
...(2)> f <- [1,2,3]
...(2)> g <- [1,2,3]
...(2)> if (rem f+g, 2) == 0
...(2)> yield {f, g*2}
...(2)> end
iex(3)> Enum.to_list s
[{1, 2}, {1, 6}, {2, 4}, {3, 2}, {3, 6}]
data Pairing a = Empty | Node a [Pairing a]
singleton :: Ord a => a -> Pairing a
singleton x = Node x []
union :: Ord a => Pairing a -> Pairing a -> Pairing a
union Empty h2 = h2
union h1 Empty = h1
union t1@(Pairing h1 subs1) t2@(Pairing h2 subs)
| h1 < h2 = Pairing h1 t2:subs1