This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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])))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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])))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
auth "github.com/abbot/go-http-auth" | |
) | |
func Secret(user, realm string) string { | |
if user == "john" { |