Skip to content

Instantly share code, notes, and snippets.

object LetterCounter extends App{
val filters = args.lift(1).getOrElse("aeiou").toList
val grouped = args(0).groupBy(l=>l).collect{case (l,g) => (l, g.length)}
val groupCount = filters.map(v => grouped.find(x=> x._1==v).getOrElse((v,0)))
val output = groupCount.foldLeft("")((result, pair) => result+pair._1 + ": "+pair._2+" ")
println(output)
}
@quii
quii / stitcher1.go
Created March 20, 2015 10:03
Sticher first pass
func Stitcher(hello_url string, worldURL string) string {
return ""
}
// Stitcher calls the Hello and World APIs to create a very useful string
func Stitcher(helloURL string, worldURL string) string {
return ""
}
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func Test_it_calls_the_services_and_concatenates_the_results(t *testing.T) {
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
// Stitcher calls the Hello and World APIs to create a very useful string
func Stitcher(helloURL string, worldURL string) string {
func Benchmark_the_stitcher(b *testing.B) {
slowHelloAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(500 * time.Millisecond)
fmt.Fprint(w, "Hello")
}))
slowWorldAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(500 * time.Millisecond)
fmt.Fprint(w, "world")
}))
// Stitcher calls the Hello and World APIs to create a very useful string
func Stitcher(helloURL string, worldURL string) string {
helloChannel := make(chan []byte, 1)
worldChannel := make(chan []byte, 1)
go func() {
helloResponse, _ := http.Get(helloURL)
defer helloResponse.Body.Close()
helloContent, _ := ioutil.ReadAll(helloResponse.Body)
const wordSeparator = ", "
// Stitcher calls the Hello and World APIs to create a very useful string
func Stitcher(helloURL string, worldURL string) string {
helloChannel := make(chan []byte, 1)
worldChannel := make(chan []byte, 1)
go getStringFromAPI(helloChannel, helloURL)
go getStringFromAPI(worldChannel, worldURL)
func Test_it_handles_non_ok_from_hello_api(t *testing.T) {
helloAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "oops", http.StatusInternalServerError)
}))
worldAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "world")
}))
const wordSeparator = ", "
const errorMsg = "ALL OTHER SOFTWARE TEAMS ARE USELESS, OUTSOURCING IS A SHAM"
type apiResult struct {
content string
err error
}
// Stitcher calls the Hello and World APIs to create a very useful string
func Stitcher(helloURL string, worldURL string) string {