Skip to content

Instantly share code, notes, and snippets.

View neilchaudhuri's full-sized avatar

Neil Chaudhuri neilchaudhuri

View GitHub Profile
@neilchaudhuri
neilchaudhuri / polymorphism.go
Created March 21, 2019 02:10
Example of idiomatic Go polymorphism
package main
import (
"fmt"
)
type Named struct {
name string
}
@neilchaudhuri
neilchaudhuri / Polymorphism.scala
Last active March 29, 2019 23:51
Examples of idiomatic Scala polymorphism
// Runtime polymorphism
trait Closeable {
def name: String
def close: String
}
case class Connection(override val name: String, database: String) extends Closeable {
override def close: String = s"Closing connection $name"
@neilchaudhuri
neilchaudhuri / uuids.go
Last active March 27, 2019 00:44
Example of idiomatic Go concurrency/parallelism
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
type Result struct {
@neilchaudhuri
neilchaudhuri / Uuids.scala
Last active March 20, 2019 17:02
Example of idiomatic Scala parallelism
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws._
import play.api.libs.ws.ahc._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits._
import play.api.libs.json._
implicit val system = ActorSystem()
@neilchaudhuri
neilchaudhuri / word_count.go
Created March 14, 2019 23:48
Example of idiomatic Go for counting words in a collection
package main
import "fmt"
import "strings"
func main() {
dictionary := make(map[string]int)
words := []string{"Fear", "of", "a", "name", "only", "increases", "fear", "of", "the", "thing", "itself"}
for _, word := range words {
word = strings.ToLower(word)
@neilchaudhuri
neilchaudhuri / WordCount.scala
Last active March 20, 2019 17:35
Example of idiomatic Scala for counting words in a collection
val words = List("Fear", "of", "a", "name", "only", "increases", "fear", "of", "the", "thing", "itself")
words
.groupBy(_.toLowerCase)
.mapValues(_.size)
@neilchaudhuri
neilchaudhuri / square_root.go
Last active March 20, 2019 17:02
Idiomatic Go error handling
package main
import "fmt"
import "math"
func squareRoot(value int) (float64, error) {
if value >= 0 {
return math.Sqrt(float64(value)), nil
} else {
return 0, fmt.Errorf("invalid input: %v", value)
@neilchaudhuri
neilchaudhuri / SquareRoot.scala
Last active March 29, 2019 23:52
Idiomatic Scala error handling
import scala.util.{Failure, Success, Try}
def squareRoot(value: Int): Try[Double] = {
if (value >= 0) {
Success(Math.sqrt(value))
} else {
Failure(new IllegalArgumentException("Value cannot be negative"))
}
}
val sum = for {
@neilchaudhuri
neilchaudhuri / Hogwarts.scala
Created March 13, 2019 16:34
Idiomatic retrieval of optional values in Scala
case class Student(name: String, house: String)
def findStudent(key: Int): Option[Student] = {
val students = Map(
1 -> Student("Harry Potter", "Hogwarts"),
3 -> Student("Draco Malfoy", "Slytherin")
)
students.get(key)
}
@neilchaudhuri
neilchaudhuri / hogwarts.go
Created March 13, 2019 16:33
Idiomatic retrieval of optional values in Go
package main
import "fmt"
type Student struct {
name string
house string
}
func findStudent(key int) (Student, bool) {